输出的结果是:
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_latlong.xsd schema // file. } else { System.out.println("Input xml document " + "doesn't conform to weather_latlong.xsd"); } } catch (Exception e) { e.printStackTrace(); } } }
为了获得输入XML文档的内容,我们先检查XmlObject.Factory.parse()返回的对象的文档类型,然后把返回的对象转化为相应的文档类型,以供稍后处理。另一段有趣的代码是最后的else代码块,它将处理格式良好的XML文档不符合weather_latlong.xsd模式的情况。
创建一个新的XML文档
下面的一小段latlong_marshal.java代码阐述了如何使用XMLBeans生成的类创建一个包含经纬度信息的新xml实例文档。
LatlongDocument latLongDoc; LatlongDocument.Latlong latLongElement; XmlOptions xmlOptions; // LatlongDocument.Factory.newInstance() creates // and returns a LatlongDocument object. latLongDoc= LatlongDocument.Factory.newInstance(); // addNewLatlong() method is called on the // document object to create and add a new // LatLong Element to document. latLongElement = latLongDoc.addNewLatlong();
LatlongDocument.Factory.newInstance()创建了一个LatlongDocument对象并且返回该对象。随后对文档对象调用addNewLatlong()方法创建并向文档增加一个新的LatLong元素。
要向LatLong元素添加数据, 简单调用latLongElement的相应的Set方法即可,它将直接映射模式中定义的的元素和属性名称。
latLongElement.setZipcode("91023"); latLongElement.setLatitude("33.8792"); latLongElement.setLongitude("117.8974");
最后的代码段将LatLong元素的当前状态写到了标准的输出流中。
xmlOptions = new XmlOptions(); // Requests use of whitespace for easier reading xmlOptions.setSavePrettyPrint(); // Requests that nested levels of the xml // document to be indented by multiple of 4 // whitespace characters xmlOptions.setSavePrettyPrintIndent(4); String xmlStr = latLongDoc.xmlText(xmlOptions); // Writes the current state of the LatLong // element to a standard output stream System.out.println("XML Instance Document is : " + " " + xmlStr );
xmlText方法用可选的xmlOptions对象控制它的行为。setSavePrettyPrint()方法要求使用空白符,以便方便阅读,而setSavePrettyPrintIndent(4)方法要求在嵌套的XML文档中首行缩进四的倍数个空白符。
输出的结果是:
XML Instance Document is : <Latlong Zipcode="91023"> <Latitude>33.8792</Latitude> <Longitude>117.8974</Longitude> </Latlong>
性能优势
与DOM的不同之处是,XMLBeans没有采用解除封送整个xml文档和为每个xml文档结点提供一个对象的方法。使用XMLBeans,只在需要时进行封送和解除封送,因此对于你从来没有查看过的代码,它们是不会被封送和解除封送的。这提高了XMLBeans解决方案的性能。
XMLBeans也提供高效的xget版本的函数访问XML模式内置数据类型。
验证
分配给内置XMLBeans java类型的值将按照其表示的模式类型的规则进行验证。例如,如果将一个符合条件的名称分配给一个XmlQName数据类型时,如果该名称的前缀不能解析为任何URI,将会抛出XmlValueOutOfRange异常。
当xml文档第一次被解析时,将根据模式定义验证其中包含的数据。更有意思的是,无论何时通过XMLBeans生成的java类处理xml文档时,XMLBeans系统将确保遵守模式约束条件。
其他特性
XMLBeans对象是可序列化的,因而,可以通过RMI边界传送,也能够很容易的从XML 字符流和字节流中提取,并保存回去。XMLBeans也具有配置功能,因而可以将XML名称映射到Java名称。这样将避免在XML的名称发生变化时重新编写Java代码。由于篇幅限制我们在此不做过多讨论。
结束语
XMLBeans 提出了底层XML数据的对象视图,同时还能访问原始的XML信息集合。通过递增的解除封送xml数据和高效的访问XML模式内置数据类型的方法,XMLBeans交付了较好的性能。下面两种特性几乎百分之百的支持XML 模式,并在操作数据期间定时验证XML数据,从而使XMLBeans非常适用于XML-Java 数据绑定。现在web services、BPEL、BPML、基于规则的XML数据转换引擎等实现,都用到了该数据绑定技术。
(编辑:aniston)
|