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.

Parent topic: Generating SDO from XML Schema

Generating Service Data Objects from PurchaseOrder.xsd

To generate SDOs from PurchaseOrder.xsd, follow the steps below:
  1. Click Window > Open Perspective > Java.
  2. Right-click on PurchaseOrder.xsd from the Package Explorer, and then click Generate > Java to bring up the Java™ generation wizard.
    Image of the Generate Java wizard
  3. From the Generator list, select SDO Generator and click Next.
  4. In the Container field, click Browse to locate the SDOFromXSDExample project in your workspace.
  5. 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

The following Java code example demonstrates how to use the generated code to perform these actions:
  • Create an instance of a PurchaseOrder
  • Serialize a PurchaseOrder into an XML file
  • Load a PurchaseOrder from an XML file
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;

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("2007-03-10");
	    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

To run the Java application:
  1. Create a class named PurchaseOrderSample.java in the default package of the SDOFromXSDExample project and paste the preceding Java code example into the class.
  2. 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.
  3. To view the sample.xml file, right-click the project and click Refresh.
    Image of the sample.xml file

Feedback