使用XMLBeans绑定XML-java数据

http://tech.ddvip.com   2007年09月02日    社区交流

内容摘要:XMLBeans提供了底层XML数据的对象视图,同时还能访问原始的XML信息集合。通过递增的解除封送xml数据和高效的访问XML 模式内置数据类型的方法,XMLBeans交付了较好的性能……

  使用XMLBeans类解除封送XML文件

  下面的一小段weather_unmarshal.java代码阐明了怎样使用XMLBeans类从weatherInput.xml.文件的XML文档中获取天气信息。

String filePath = "weatherInput.xml";
java.io.File inputXMLFile =
new java.io.File(filePath);
// Parse XML Document.
WeatherDocument weatherDoc =
WeatherDocument.Factory.parse(inputXMLFile);
// Get object reference of root element Weather.
WeatherDocument.Weather weatherElement =
weatherDoc.getWeather();

  通过调用WeatherDocument.Factory.parse(File)方法来解析XML文件,该方法返回一个WeatherDocument对象。随后对weatherDocument对象调用getWeather()方法来获取根元素Weather的对象引用。

  要获得Weather元素的内容,简单调用weatherElement的相应的get方法,它将直接映射模式定义的元素和属性名称:

// Call the appropriate 'get' methods of
// weatherElement that
// directly map to the element and attribute names
// defined in the schema.
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());

来源:Dev2Dev    作者:Hetal C. Shah    责编:豆豆技术应用

正在加载评论...