Generating SDOs from an XML Schema sample details
This sample demonstrates how to generate Service Data Objects from an XML Schema, and how to use the generated SDOs. The example includes an XML Schema file named PurchaseOrder.xsd.
Generating Service Data Objects from PurchaseOrder.xsd
About this task
Procedure
- Click .
- Right-click on PurchaseOrder.xsd from the Package Explorer, and
then click to bring up the Java™ generation wizard.

- From the Generator list, select SDO Generator and click Next.
- In the Container field, click Browse to locate the SDOFromXSDExample project in your workspace.
- Click Finish to generate the Java classes. You can view the generated Java classes in the Package Explorer.
Using the generated SDOs in a Java application
About this task
- Create an instance of a PurchaseOrder
- Serialize a PurchaseOrder into an XML file
- Load a PurchaseOrder from an XML file
Example
import java.math.BigDecimal;
import java.math.BigInteger;
import com.ibm.DocumentRoot;
import com.ibm.IbmFactory;
import com.ibm.ItemType;
import com.ibm.Items;
import com.ibm.PurchaseOrderType;
import com.ibm.USAddress;
import com.ibm.USState;
import com.ibm.util.IbmResourceUtil;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
public class PurchaseOrderSample {
public static void main(String[] args) throws Exception {
/******************************************************************************/
/******************* CREATE AN INSTANCE OF A PURCHASE ORDER *******************/
/******************************************************************************/
// Create a purchase order
PurchaseOrderType purchaseOrder = IbmFactory.eINSTANCE.createPurchaseOrderType();
// Create a product item for the purchase order
ItemType item = IbmFactory.eINSTANCE.createItemType();
item.setProductName("DVD Player");
item.setQuantity(BigInteger.valueOf(1));
item.setUSPrice(BigDecimal.valueOf(14895,2));
// Add the product item to a collection of items
Items items = IbmFactory.eINSTANCE.createItems();
items.getItem().add(item);
// Incorporate the collection of items into the purchase order
purchaseOrder.setItems(items);
// Set the purchase order shipping address
USAddress shippingAddress = IbmFactory.eINSTANCE.createUSAddress();
shippingAddress.setName("Alice Smith");
shippingAddress.setStreet("3465 Maple Street");
shippingAddress.setCity("Mill Valley");
shippingAddress.setState(USState.get("CA"));
shippingAddress.setZip(BigDecimal.valueOf(90952));
shippingAddress.setCountry("US");
purchaseOrder.setShipTo(shippingAddress);
//Set the purchase order date and comment
purchaseOrder.setOrderDate(DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2007, 3, 10, DatatypeConstants.FIELD_UNDEFINED));
purchaseOrder.setComment("Overnight shipping");
/******************************************************************************/
/******************* SAVE A PURCHASE ORDER INTO AN XML FILE *******************/
/******************************************************************************/
// Create and set the XML document root to the purchase order
DocumentRoot documentRoot = IbmFactory.eINSTANCE.createDocumentRoot();
documentRoot.setPurchaseOrder(purchaseOrder);
// Save the XML Document into a file
IbmResourceUtil.getInstance().save(documentRoot,"sample.xml");
/******************************************************************************/
/******************* LOAD A PURCHASE ORDER FROM AN XML FILE *******************/
/******************************************************************************/
// Load the XML document file
documentRoot = IbmResourceUtil.getInstance().load("sample.xml");
// Print a summary of the purchase order obtained from the loaded XML document
System.out.println("Purchase order summary");
System.out.println(" Date: " + documentRoot.getPurchaseOrder().getOrderDate());
System.out.println(" Customer: " + documentRoot.getPurchaseOrder().getShipTo().getName());
System.out.println(" Product sold: "
+ ((ItemType)documentRoot.getPurchaseOrder().getItems().getItem().get(0)).getProductName());
System.out.println(" Amount paid: "
+ ((ItemType)documentRoot.getPurchaseOrder().getItems().getItem().get(0)).getUSPrice());
System.out.println(" Comments: " + documentRoot.getPurchaseOrder().getComment());
}
}
Running the Java application
About this task
Procedure
- Create a class named PurchaseOrderSample.java in the default package of the SDOFromXSDExample project and paste the preceding Java code example into the class.
- In the Package Explorer, right-click on PurchaseOrderSample.java and click Run As > Java Application. The serialized file sample.xml is created. You can view the reconstructed Java instances in the Console view.
- To view the sample.xml file, right-click the project and click Refresh.
