Apache sdo学习笔记(二)——通过Shema创建DataObject对象

1:schema文件po.xsd

<?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: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="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>


2:java代码

package com.waysoft.intergration.dao;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.tuscany.sdo.api.SDOUtil;

import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XSDHelper;

public class Test1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			// create HelperContext
			HelperContext scope = SDOUtil.createHelperContext();
			// load .xsd document
			loadTypesFromXMLSchemaFile(scope, Test1.class.getResource("/").getPath().substring(1)+"po.xsd");
			// create DataObject
			DataObject po = scope.getDataFactory().create("http://www.example.com/PO","PurchaseOrderType");
			// 设置属性值
			po.setString("orderDate", "2010-07-07");
			
		
			
			DataObject shipTo = po.createDataObject("shipTo");
             // 设置属性值
			shipTo.setString("country", "中国");
            // 设置简易子元素的值 [与设置属性值的方式相同,二者的区别由类型决定]
			shipTo.setString("name", "张三");
			shipTo.setString("street", "东岳大街");
			shipTo.setString("city", "泰安市");
			shipTo.setString("state", "山东省");
			shipTo.setString("zip", "27100");
			
			DataObject billTo = po.createDataObject("billTo");
			billTo.setString("country", "中国");
			billTo.setString("name", "李四");
			billTo.setString("street", "长乐街");
			billTo.setString("city", "杭州");
			billTo.setString("state", "浙江省");
			billTo.setString("zip", "310000");
			
			po.set("comment", "This is a test comment.");
			
			DataObject items = po.createDataObject("items");
			
			DataObject item1 = items.createDataObject("item");
			item1.setString("partNum", "123-AB");
			item1.setString("productName", "apple");
			item1.setString("price","1.2");
			item1.setString("quantity", "12");
			item1.setString("comment", "empty comment.");
			item1.setString("shipDate", "2010-06-30");
			
			DataObject item2 = items.createDataObject("item");
			item2.setString("partNum", "456-CD");
			item2.setString("productName", "pear");
			item2.setString("price","2.1");
			item2.setString("quantity", "21");
			item2.set("comment", "empty comment.");
			
			
			// -- save
			scope.getXMLHelper().save(po, "http://www.example.com/PO", "purchaseOrder",System.out);
		} catch(Exception e){
			e.printStackTrace();
		}

	}
	

	
	public static void loadTypesFromXMLSchemaFile(HelperContext helperContext,String filename)throws IOException{
		XSDHelper xsdHelper = helperContext.getXSDHelper();
		InputStream in = null;
		try {
			URL url = new File(filename).toURI().toURL();
			in = url.openStream();
			xsdHelper.define(in, url.toString());
		} catch (IOException e){
			System.err.print("[错误]:");
			System.out.println("无法加载 " + filename + " .");
			throw e;
		} finally {
			try { if (in != null) in.close();} catch (Exception e){}
		}
	}

}


3:运行结果

<?xml version="1.0" encoding="UTF-8"?>
<po:purchaseOrder xmlns:po="http://www.example.com/PO" orderDate="2010-07-07">
  <shipTo country="中国">
    <name>张三</name>
    <street>东岳大街</street>
    <city>泰安市</city>
    <state>山东省</state>
    <zip>27100</zip>
  </shipTo>
  <billTo country="中国">
    <name>李四</name>
    <street>长乐街</street>
    <city>杭州</city>
    <state>浙江省</state>
    <zip>310000</zip>
  </billTo>
  <po:comment>This is a test comment.</po:comment>
  <items>
    <item partNum="123-AB">
      <productName>apple</productName>
      <price>1.2</price>
      <quantity>12</quantity>
      <po:comment>empty comment.</po:comment>
      <shipDate>2010-06-30</shipDate>
    </item>
    <item partNum="456-CD">
      <productName>pear</productName>
      <price>2.1</price>
      <quantity>21</quantity>
      <po:comment>empty comment.</po:comment>
    </item>
  </items>
</po:purchaseOrder>


 

你可能感兴趣的:(Apache sdo学习笔记(二)——通过Shema创建DataObject对象)