以下为引用的内容: <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/PO" targetNamespace="http://www.example.com/PO"> <xsd:import namespace="commonj.sdo/xml" schemaLocation="sdo.xsd"/> <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:element name="comment" type="xsd:string"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="StatusType"> <xsd:sequence> <xsd:element name="status" type="xsd:string"/> <xsd:element name="contact" type="xsd:string"/> <xsd:element name="changeDate" type="xsd:date"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/> </xsd:complexType> <xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="price" type="xsd:decimal"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
以下为引用的内容: package com.company.sdo.po; import commonj.sdo.DataObject; import commonj.sdo.helper.DataFactory; public class CreatePO { public static void main(String[] args) throws Exception { //1. 使用XSD定义类型和属性 Util.definePOTypes(); //2. 建立根DataObject DataObject purchaseOrder = DataFactory.INSTANCE.create(Constants.PO_NAMESPACE, "PurchaseOrderType"); //3. 设置根DataObject的数据类型属性 purchaseOrder.setString("orderDate", "1999-10-20"); //4. 建立子DataObject DataObject shipTo = purchaseOrder.createDataObject("shipTo"); //5. 设置子DataObject的数据类型属性 shipTo.set("country", "US"); shipTo.set("name", "Alice Smith"); shipTo.set("street", "123 Maple Street"); shipTo.set("city", "Mill Valley"); shipTo.set("state", "CA"); shipTo.setString("zip", "90952"); DataObject billTo = purchaseOrder.createDataObject("billTo"); billTo.set("country", "US"); billTo.set("name", "Robert Smith"); billTo.set("street", "8 Oak Avenue"); billTo.set("city", "Mill Valley"); billTo.set("state", "PA"); billTo.setString("zip", "95819"); purchaseOrder.set("comment", "Hurry, my lawn is going wild!"); DataObject items = purchaseOrder.createDataObject("items"); //6. 为子DataObject “item”建立一个子DataObject DataObject item1 = items.createDataObject("item"); item1.set("partNum", "872-AA"); item1.set("productName", "Lawnmower"); item1.setInt("quantity", 1); item1.setString("price", "148.95"); item1.set("comment", "Confirm this is electric"); DataObject item2 = items.createDataObject("item"); item2.set("partNum", "926-AA"); item2.set("productName", "Baby Monitor"); item2.setInt("quantity", 1); item2.setString("price", "39.98"); item2.setString("shipDate", "2007-11-21"); DataObject item3 = items.createDataObject("item"); item3.set("partNum", "998-AA"); item3.set("productName", "Carpet"); item3.setInt("quantity", 1); item3.setString("price", "439.98"); item3.setString("shipDate", "2007-12-01"); //7. 将XML数据保存在一个XML文件中 Util.storeXML(purchaseOrder,"purchaseOrder", Constants.PO_XML); } } |