快速业务通道

使用XMLBeans绑定XML-java数据 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-18
Stamp); System.out.println("Temperature is " + weatherElement.getTemperature()); System.out.println("Humidity is " + weatherElement.getHumidity()); System.out.println("Visibility is " + weatherElement.getVisibility());

输出的结果是:

Weatherdetailsofzipcode92834-2345at2003-11-13T05:29:27-03:01 Temperatureis85.3 Humidityis50.0 Visibilityis5.5

模式声明多个全局元素时如何解除封送

在上面的例子中,我们假设输入XML文档始终包含天气信息。然而,在实际中,由于weather_latlong.xsd文件通过声明两个全局元素(Weather和Latlong)同时描述了二者的详细信息,因此输入XML文档中可能包含天气信息也可能包含经纬度信息。。

有两种方法可以解析一个xml文档并将其绑定到相应XMLBeans类型的实例。在上述的例子中,我们用WeatherDocument.Factory.parse()方法解析XML文档。另外一种方式是使用XMLBeans内置的XmlObject类。

下面的一小段weather_unmarshal_xmlObject.java代码阐述了怎样使用XmlObject类获取xml实例文档中包含的天气和经纬度信息。

public static void main(String args[]) { try { if (args.length < 1 ) { System.out.println("Usage : java " +"weather_unmarshal_xmlObject <<InputFilePath>>"); return; } String filePath = args[0]; java.io.File inputXMLFile   = new java.io.File(filePath); XmlObject xmlObjExpected = XmlObject.Factory.parse(inputXMLFile); // Check document type of the object returned by // the call to XmlObject.Factory.parse() method. // If type of object returned is of //noNamespace.WeatherDocument, then input xml //document carries weather details of a location. if (xmlObjExpected instanceof noNamespace.WeatherDocument) {   WeatherDocument weatherDoc =    (noNamespace.WeatherDocument)xmlObjExpected;   WeatherDocument.Weather weatherElement =      weatherDoc.getWeather();   Calendar timeStamp =    weatherElement.getDatetime();   System.out.println    ("Weather details of zipcode "    + weatherElement.getZipcode() + " at "    + timeStamp + " : ");   System.out.println("Temperature is "      + weatherElement.getTemperature());   System.out.println("Humidity is "      + weatherElement.getHumidity());   System.out.println("Visibility is "      + weatherElement.getVisibility()); // else if type of object returned is of // noNamespace.LatlongDocument, then input xml //document carries latlong details of a location. } else if(xmlObjExpected instanceof    noNamespace.LatlongDocument) {      LatlongDocument latLongDoc =      (noNamespace.LatlongDocument)xmlObjExpected;      LatlongDocument.Latlong latLongElement =      latLongDoc.getLatlong();    System.out.println    ("Latlong details of zipcode "     + latLongElement.getZipcode() + " : ");    System.out.println("Latitude is "     + latLongElement.getLatitude());    System.out.println("Longitude is "     + latLongElement.getLongitude()); // else input xml document is well formed , but // doesn''t conform to weather_latl

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号