邢台网站建设 冀icp备,网站技术外包公司,规范网站建设情况的报告,网站备案拍照是什么我已经在EclipseLink JAXB#xff08;MOXy#xff09;的XmlPath和外部绑定文件扩展中撰写了以前的文章。 在本文中#xff0c;我将通过将单个对象模型映射到两个不同的XML模式来演示这些扩展的功能。 为了使示例更加“真实”#xff0c;XML数据将来自提供天气信息的两种不同… 我已经在EclipseLink JAXBMOXy的XmlPath和外部绑定文件扩展中撰写了以前的文章。 在本文中我将通过将单个对象模型映射到两个不同的XML模式来演示这些扩展的功能。 为了使示例更加“真实”XML数据将来自提供天气信息的两种不同服务Google和Yahoo。 Java模型 以下域模型将用于此帖子 天气预报 package blog.weather;import java.util.List;public class WeatherReport {private String location;private int currentTemperature;private String currentCondition;private List forecast;} 预测 package blog.weather;public class Forecast {private String dayOfTheWeek;private int low;private int high;private String condition;} Google Weather API 首先我们将利用Google的Weather API。 以下URL将用于访问加拿大渥太华的天气数据 http://www.google.com/ig/api?weather渥太华 以下是在我撰写本文时执行上述查询的结果。 我已经突出显示了我们将映射到XML文档的部分 xml_api_reply version1weather module_id0 tab_id0 mobile_row0 mobile_zipped1row0 section0forecast_informationcity dataOttawa, ON /postal_code dataOttawa /latitude_e6 data /longitude_e6 data /forecast_date data2011-09-08 /current_date_time data2011-09-08 14:00:00 0000 /unit_system dataUS //forecast_informationcurrent_conditionscondition dataMostly Cloudy /temp_f data66 /temp_c data19 /humidity dataHumidity: 73% /icon data/ig/images/weather/mostly_cloudy.gif /wind_condition dataWind: NE at 13 mph //current_conditionsforecast_conditionsday_of_week dataThu /low data55 /high data75 /icon data/ig/images/weather/cloudy.gif /condition dataCloudy //forecast_conditionsforecast_conditionsday_of_week dataFri /low data46 /high data77 /icon data/ig/images/weather/mostly_sunny.gif /condition dataPartly Sunny //forecast_conditionsforecast_conditionsday_of_week dataSat /low data43 /high data68 /icon data/ig/images/weather/sunny.gif /condition dataClear //forecast_conditionsforecast_conditionsday_of_week dataSun /low data55 /high data75 /icon data/ig/images/weather/sunny.gif /condition dataClear //forecast_conditions/weather
/xml_api_reply Java模型-通过注释映射到Google的XML模式 我们将通过标准JAXB和MOXy扩展注释的组合来映射Google weather API的结果。 天气预报 package blog.weather;import java.util.List;import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;import org.eclipse.persistence.oxm.annotations.XmlPath;XmlRootElement(namexml_api_reply)
XmlType(propOrder{location, currentCondition, currentTemperature, forecast})
XmlAccessorType(XmlAccessType.FIELD)
public class WeatherReport {XmlPath(weather/forecast_information/city/data)private String location;XmlPath(weather/current_conditions/temp_f/data)private int currentTemperature;XmlPath(weather/current_conditions/condition/data)private String currentCondition;XmlPath(weather/forecast_conditions)private ListForecast forecast;} 预测 package blog.weather;import org.eclipse.persistence.oxm.annotations.XmlPath;public class Forecast {XmlPath(day_of_week/data)private String dayOfTheWeek;XmlPath(low/data)private int low;XmlPath(high/data)private int high;XmlPath(condition/data)private String condition;} 演示版 以下演示代码将读取Google气象服务的XML数据并将对象编组回XML package blog.weather;import java.net.URL;
import javax.xml.bind.*;public class GoogleDemo {public static void main(String[] args) throws Exception {JAXBContext jc JAXBContext.newInstance(WeatherReport.class);Unmarshaller unmarshaller jc.createUnmarshaller();URL url new URL(http://www.google.com/ig/api?weatherOttawa);WeatherReport weatherReport (WeatherReport) unmarshaller.unmarshal(url);Marshaller marshaller jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(weatherReport, System.out);}} 输出量 以下是运行演示代码的结果。 输出代表我们映射到的XML文档的一部分 ?xml version1.0 encodingUTF-8?
xml_api_replyweatherforecast_informationcity dataOttawa, ON//forecast_informationcurrent_conditionscondition dataMostly Cloudy/temp_f data68//current_conditionsforecast_conditionsday_of_week dataThu/low data55/high data75/condition dataCloudy//forecast_conditionsforecast_conditionsday_of_week dataFri/low data46/high data77/condition dataPartly Sunny//forecast_conditionsforecast_conditionsday_of_week dataSat/low data43/high data68/condition dataClear//forecast_conditionsforecast_conditionsday_of_week dataSun/low data55/high data75/condition dataClear//forecast_conditions/weather
/xml_api_reply 雅虎天气API 以下URL将用于使用Yahoo Weather API3369是渥太华的WOEID访问渥太华的天气数据 http://weather.yahooapis.com/forecastrss?w3369 以下是在我撰写本文时执行上述查询的结果 ?xml version1.0 encodingUTF-8?
rss version2.0 xmlns:yweatherhttp://xml.weather.yahoo.com/ns/rss/1.0xmlns:geohttp://www.w3.org/2003/01/geo/wgs84_pos#channeltitleYahoo! Weather - Ottawa, CA/titlelinkhttp://us.rd.yahoo.com/dailynews/rss/weather/Ottawa__CA/*http://weather.yahoo.com/forecast/CAXX0343_f.html/linkdescriptionYahoo! Weather for Ottawa, CA/descriptionlanguageen-us/languagelastBuildDateThu, 08 Sep 2011 10:58 am EDT/lastBuildDatettl60/ttlyweather:location cityOttawa regionONcountryCanada /yweather:units temperatureF distancemi pressureinspeedmph /yweather:wind chill66 direction40 speed12 /yweather:atmosphere humidity73 visibilitypressure30.14 rising0 /yweather:astronomy sunrise6:31 am sunset7:25 pm /imagetitleYahoo! Weather/titlewidth142/widthheight18/heightlinkhttp://weather.yahoo.com/linkurlhttp://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif/url/imageitemtitleConditions for Ottawa, CA at 10:58 am EDT/titlegeo:lat45.42/geo:latgeo:long-75.69/geo:longlinkhttp://us.rd.yahoo.com/dailynews/rss/weather/Ottawa__CA/*http://weather.yahoo.com/forecast/CAXX0343_f.html/linkpubDateThu, 08 Sep 2011 10:58 am EDT/pubDateyweather:condition textMostly Cloudy code28temp66 dateThu, 08 Sep 2011 10:58 am EDT /description![CDATA[
img srchttp://l.yimg.com/a/i/us/we/52/28.gif/bCurrent Conditions:/bMostly Cloudy, 66 FbForecast:/bThu - Partly Cloudy. High: 75 Low: 57Fri - Partly Cloudy. High: 79 Low: 53a hrefhttp://us.rd.yahoo.com/dailynews/rss/weather/Ottawa__CA/*http://weather.yahoo.com/forecast/CAXX0343_f.htmlFull Forecast at Yahoo! Weather/a(provided by a hrefhttp://www.weather.com The Weather Channel/a)]]/descriptionyweather:forecast dayThu date8 Sep 2011 low57high75 textPartly Cloudy code30 /yweather:forecast dayFri date9 Sep 2011 low53high79 textPartly Cloudy code30 /guid isPermaLinkfalseCAXX0343_2011_09_09_7_00_EDT/guid/item/channel
/rss!-- api4.weather.sp2.yahoo.com uncompressed/chunked Thu Sep 8 08:32:54PDT 2011 -- Java模型–通过XML元数据映射到Yahoo的XML模式 由于我们无法通过注释提供到对象模型的第二套映射因此我们必须利用MOXy的XML元数据提供后续的映射。 默认情况下MOXy的映射文档用于补充模型上指定的所有注释。 但是如果设置了xml-mapping-metadata-complete标志则XML元数据将完全替换注释提供的元数据用于Google映射的注释将保留在POJO上但是xml-mapping-metadata-complete标志告诉MOXy忽略它们。 ?xml version1.0?
xml-bindingsxmlnshttp://www.eclipse.org/eclipselink/xsds/persistence/oxmpackage-nameblog.weatherxml-mapping-metadata-completetruexml-schema element-form-defaultQUALIFIEDxml-ns prefixyweather namespace-urihttp://xml.weather.yahoo.com/ns/rss/1.0//xml-schemajava-typesjava-type nameWeatherReport xml-accessor-typeFIELDxml-root-element namerss/xml-type prop-orderlocation currentTemperature currentCondition forecast/java-attributesxml-attribute java-attributelocation xml-pathchannel/yweather:location/city/xml-attribute java-attributecurrentTemperature namechannel/item/yweather:condition/temp/xml-attribute java-attributecurrentCondition namechannel/item/yweather:condition/text/xml-element java-attributeforecast namechannel/item/yweather:forecast//java-attributes/java-typejava-type nameForecast xml-accessor-typeFIELDjava-attributesxml-attribute java-attributedayOfTheWeek nameday/xml-attribute java-attributelow/xml-attribute java-attributehigh/xml-attribute java-attributecondition nametext//java-attributes/java-type/java-types
/xml-bindings 演示版 以下演示代码将读取Yahoo气象服务的XML数据并将对象编组回XML。 由于与未映射的CDATA部分有关的MOXy错误https://bugs.eclipse.org/357145使用了经过过滤的XMLStreamReader将其从XML输入中删除 package blog.weather;import java.util.HashMap;
import java.util.Map;import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;import org.eclipse.persistence.jaxb.JAXBContextFactory;public class YahooDemo {public static void main(String[] args) throws Exception {MapString, Object properties new HashMapString, Object(1);properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, blog/weather/yahoo-binding.xml);JAXBContext jc JAXBContext.newInstance(new Class[] {WeatherReport.class}, properties);XMLInputFactory xif XMLInputFactory.newFactory();StreamSource xml new StreamSource(http://weather.yahooapis.com/forecastrss?w3369);XMLStreamReader xsr xif.createXMLStreamReader(xml);xsr xif.createFilteredReader(xsr, new CDATAFilter());Unmarshaller unmarshaller jc.createUnmarshaller();WeatherReport weatherReport (WeatherReport) unmarshaller.unmarshal(xsr);Marshaller marshaller jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(weatherReport, System.out);}private static class CDATAFilter implements StreamFilter {public boolean accept(XMLStreamReader xsr) {return XMLStreamReader.CDATA ! xsr.getEventType();}}} 输出量 以下是运行演示代码的结果。 输出代表我们映射到的XML文档的一部分 ?xml version1.0 encodingUTF-8?
rss xmlns:yweatherhttp://xml.weather.yahoo.com/ns/rss/1.0channelyweather:location cityOttawa/itemyweather:forecast dayThu low57 high74 textPartly Cloudy/yweather:forecast dayFri low53 high79 textPartly Cloudy//item/channel
/rss 参考 Java XML和JSON绑定博客上的 JCG合作伙伴 Blaise Doughan的参考将 对象映射到多个XML模式-天气示例 。 相关文章 使用JAXB从XSD生成XML 具有简单框架教程的Android XML绑定 使用XML Pull增强Android XML解析 使用Gson教程进行Android JSON解析 用Jackson进行Java JSON处理 Java教程和Android教程列表 翻译自: https://www.javacodegeeks.com/2011/09/mapping-objects-to-multiple-xml-schemas.html