RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Tracking XML Data Changes Easily with SDO : Page 2

Service Data Object (SDO) provides developers with an easy-to-implement mechanism for tracking data history at the system level. Learn how to use a Java implementation of SDO to track changes made to XML data.

Create and Persist XML with SDO
The CreatePO.java class (see Listing 2) completes the creation phase of the XML processing in this example. You should pay particularly close attention to the seven commented areas of this class:
  1. Define Types and Properties with XSD—Since your data are modeled by an XML schema, you first need to define the SDO Types and Properties in the runtime based on the schema. This is accomplished with XSDHelper through the definePOTypes() method in the Util class:
         public static void definePOTypes() throws Exception {
              FileInputStream fis = new FileInputStream(PO_MODEL_ORIGINAL);
              XSDHelper.INSTANCE.define(fis, null);

    (Section 9 of the SDO specification governs the actual mapping from XML schema entities to SDO types and properties. Refer to this section for more detail.).

  2. Create the root data object—The dynamic API of SDO represents structured data by either a hierarchy of data objects, each with its own properties, or a DataGraph, which packages a graph of data objects with their metadata. SDO provides the DataFactory interface for creating unconnected data objects and is therefore the appliance you need to create the root data object of the purchase order.
  3. Set a data type Property for the root data object —Purchase order is a Type in SDO, and according to the schema, it has a data type property called orderDate, which is a date type. The line under comment 3 sets the orderDate with a date string. Alternatively, you could create a Java Date object, and use the setDate() method of DataObject.
  4. Create child data objects—The data object purchaseOrder has several child data objects. As an example, the line under comment 4 creates the shipTo child directly from purchaseOrder with the name "shipTo." Alternatively, you could use DataFactory to create an unconnected shipTo data object and set it as a child of purchaseOrder with one of the setDataObject() methods of DataObject:
         DataObject shipTo = DataFactory.INSTANCE.create(CONSTANTS.PO_NAMESPACE, "USAddress");
         PurchaseOrder.setDataObject("shipTo", shipTo);
  5. Set data type Property for the child data object—Based on the USAddress type definition, the shipTo data object has various data type Properties. The lines below comment 5 create these properties.
  6. Create a child data object for the child data object "items"—This section shows the hierarchical nature of the SDO data model when modeling XML. Items is a child of the root data object purchaseOrder, and it contains several children of the item data object.
  7. Persist the XML data to an XML file—With the dynamic SDO API, you can use the XMLHelper interface to persist XML data to an XML file. The code under comment 7 calls the Util class for help, and defines the method as follows:
         public static void storeXML(DataObject data, String rootElementName,
              String xmlFile) throws Exception {
              OutputStream stream = new FileOutputStream(xmlFile);
              XMLHelper.INSTANCE.save(data, PO_NAMESPACE, rootElementName, stream);

The persisted XML file for the purchase order is po_original.xml.

Working with the dynamic SDO API for XML can be so simple that this author believes it is much more convenient than the comparable DOM API.

The next section discusses how, with SDO's dynamic API, you can modify the purchase order and track your changes at the same time.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date