/** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.tuscany.samples.sdo.basic; import java.util.List; import org.apache.tuscany.samples.sdo.SampleBase; import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure; import commonj.sdo.DataObject; import commonj.sdo.helper.HelperContext; import commonj.sdo.helper.XMLDocument; /** * Reads purchase order DataObject from XML, based upon Fuhwei Lwo's paper * Create and read an XML document based on XML Schema. *

*

Running this Sample

See the main overview for instructions on how to run this * sample. * */ public class ReadPurchaseOrder extends SampleBase { public static String purchaseOrderDoc = "\n"+ " \n"+ " \n"+ " Alice Smith\n"+ " 123 Maple Street\n"+ " Mill Valley\n"+ " CA\n"+ " 90952\n"+ " \n"+ " \n"+ " Robert Smith\n"+ " 8 Oak Avenue\n"+ " Mill Valley\n"+ " PA\n"+ " 95819\n"+ " \n"+ " Hurry, my lawn is going wild!\n"+ " \n"+ " \n"+ " Lawnmower\n"+ " 148.95\n"+ " 1\n"+ " Confirm this is electric\n"+ " \n"+ " \n"+ " Baby Monitor\n"+ " 39.98\n"+ " 1\n"+ " 1999-05-21\n"+ " \n"+ " \n"+ " \n"; public ReadPurchaseOrder(Integer commentaryLevel) { super(commentaryLevel, SampleInfrastructure.SAMPLE_LEVEL_BASIC); } public static void main(String[] args) { ReadPurchaseOrder sample = new ReadPurchaseOrder(COMMENTARY_FOR_NOVICE); sample.run(); } /* * metadata for the sample documenting the areas of SDO that are explored */ public static int [] CORE_FUNCTION = { SDOFacets.LOADING_DATA_FROM_XML, SDOFacets.SAVING_DATA_TO_XML }; public static int [] SIGNIFICANT_FUNCTION = { SDOFacets.GET_PROPERTIES_OF_DATAOBJECT_BY_NAME }; public void runSample () throws Exception { commentary(COMMENTARY_ALWAYS, "This sample is based upon Fuhwei Lwo's paper \n"+ "http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/\n"); HelperContext scope = createScopeForTypes(); loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.PO_XSD_RESOURCE); commentary( "We load the purchase order document into an instance of XMLDocument\n\n"+ "XMLDocument xmlDoc = getXMLDocumentFromString(scope, purchaseOrderDoc);\n"); XMLDocument xmlDoc = getXMLDocumentFromString(scope, purchaseOrderDoc); commentary( "We then retrieve the root DataObject from the XMLDocument\n"+ "then print out some of the details\n\n"+ "DataObject purchaseOrder = xmlDoc.getRootObject();"); DataObject purchaseOrder = xmlDoc.getRootObject(); System.out.println("Order date: " + purchaseOrder.get("orderDate")); System.out.println("Comment: " + purchaseOrder.get("comment")); DataObject shipTo = purchaseOrder.getDataObject("shipTo"); System.out.println("Ship to name: " + shipTo.get("name")); DataObject billTo = purchaseOrder.getDataObject("billTo"); System.out.println("Bill to name: " + billTo.get("name")); System.out.println(); DataObject items = purchaseOrder.getDataObject("items"); List itemList = items.getList("item"); for (int i = 0; i < itemList.size(); i++) { DataObject item = (DataObject) itemList.get(i); System.out.println("Part num: " + item.get("partNum")); System.out.println("Product name: " + item.get("productName")); } } }