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.
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()); } }