Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use XMLBeans to Manage Multi-version XML : Page 2

XML version management can be a real pain point in integrated Web services applications. Learn how to handle it with XMLBeans.


advertisement

Real World Example: Purchase Order Application
The sample codedemonstrates a purchase order (PO) processing application. This application takes XML file references, but you can modify the code with server-side EJBs to process incoming XML. For this example, you published PO.xsd and all the client applications send their purchase orders in an XML format compliant to the published XSD. In the initial version, customer name and order date constitute the key to uniquely identifying each PO:


<xs:schema targetNamespace="http://openuri.org/v1/easypo"
    xmlns:po="http://openuri.org/v1/easypo"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" version="1.0">
<xs:element name="purchase-order">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="customer" type="po:customer"/>
   <xs:element name="date" type="xs:dateTime"/>
   <xs:element name="line-item" type="po:line-item" minOccurs="0"
maxOccurs="unbounded"/> <xs:element name="shipper" type="po:shipper" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>

XMLBeans provides a batch file (scomp) to compile the XSD files. In this example, all the compiled classes have the package name openuri.org.v1.easypo. This name corresponds to the targetNamespace in the published XSD. Here is a simple PO XML that is compliant with the published XSD:




<po:purchase-order xmlns:po="http://openuri.org/v1/easypo">
 <po:customer>
  <po:name>Foo Bar</po:name>
  <po:address>Anytown, PA</po:address>
 </po:customer>
 <po:date>2004-11-05</po:date>
  <po:line-item>
   <po:description>Software testing tool</po:description>
   <po:per-unit-ounces>5</po:per-unit-ounces>
   <po:price>1000.00</po:price>
   <po:quantity>2</po:quantity>
  </po:line-item>   
  <po:shipper>
    <po:name>ZipShip</po:name>
    <po:per-ounce-rate>1</po:per-ounce-rate>
  </po:shipper>
</po:purchase-order>

The server-side code uses XMLBeans to parse the incoming XML. The MultiVersion.jar contains all the XMLBean classes generated when the XSD is compiled. XMLBeans provides a factory class to parse the XML and create an instance of the PurchaseOrderDocument class:


org.openuri.v1.easypo.PurchaseOrderDocument poDoc = 
  org.openuri.v1.easypo.PurchaseOrderDocument.Factory.parse(File po);

All the data elements will be extracted from the PurchaseOrderDocument instance. The sample application prints all the extracted data elements.

Modify Purchase Order XSD
For faster searches, you decide to add a PO number to uniquely identify each purchase order. Requesting all clients to modify their software to accommodate this change would be difficult. You would have to publish both versions of the XSD and process both XML versions transparently in your application. Handling multiple XML versions will result in messy code. If the design is not simple, further enhancements will be hindered. XMLBeans comes in very handy here.

The first step is to create a new version of the XSD with a new element poID:


<xs:schema targetNamespace="http://openuri.org/v2/easypo"
    xmlns:po="http://openuri.org/v2/easypo"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" version="1.1">	
    <xs:element name="purchase-order">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="poId" type="xs:int"/>
                <xs:element name="customer" type="po:customer"/>
                <xs:element name="date" type="xs:dateTime"/>
                <xs:element name="line-item" type="po:line-item" minOccurs="0" 

maxOccurs="unbounded"/> <xs:element name="shipper" type="po:shipper" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>

The target namespace is modified to include the version number. As mentioned earlier, when the XSD is compiled, XMLBeans generates the same class PurchaseOrderDocument but with a different package name. This provides a separate class for each version, which is very useful. This is the revised XML:


<po:purchase-order xmlns:po="http://openuri.org/v2/easypo">
    <po:poId> 100025
    </po:poId><po:name>Foo Bar</po:name>
  <po:address>Anytown, PA</po:address>
 </po:customer>
 <po:date>2004-11-05</po:date>
  <po:line-item>
   <po:description>Software testing tool</po:description>
   <po:per-unit-ounces>5</po:per-unit-ounces>
   <po:price>1000.00</po:price>
   <po:quantity>2</po:quantity>
  </po:line-item>   
  <po:shipper>
    <po:name>ZipShip</po:name>
    <po:per-ounce-rate>1</po:per-ounce-rate>
  </po:shipper>
</po:purchase-order>

Each PurchaseOrderDocument class generated by XMLBeans can parse only one particular version of XML. It throws an exception when you try to parse a different version of XML. This makes it easy to identify the version of the incoming XML:


try{
   org.openuri.v1.easypo.PurchaseOrderDocument poDoc_v1 =  
org.openuri.v1.easypo.PurchaseOrderDocument.Factory.parse(po); }catch(Exception e) { org.openuri.v2.easypo.PurchaseOrderDocument poDoc_v2 =
org.openuri.v2.easypo.PurchaseOrderDocument.Factory.parse(po); }

Once the Purchase Order version is identified, the rest of the logic remains the same. Since XMLBeans provides a flexible way to convert XML-java-XML, the backend code can be simplified to handle multiple versions of XML.

Make Your Clients Happy
When you handle multiple versions of XML with XMLBeans, you will not only have a lot of happy clients talking to your application but your managers will praise you for your smarts.



Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap