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.
by Young Yang
Jan 17, 2008
Page 2 of 4
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:
Define Types and Properties with XSDSince 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);
fis.close();
}
(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.).
Create the root data objectThe 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.
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.
Create child data objectsThe 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:
Set data type Property for the child data objectBased on the USAddress type definition, the shipTo data object has various data type Properties. The lines below comment 5 create these properties.
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.
Persist the XML data to an XML fileWith 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: