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

1:schema文件company.xsd

<?xml version="1.0" encoding="UTF-8"?>
 <xsd:schema xmlns:company="company.ns" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="company.ns">
    <xsd:element name="company" type="company:CompanyType"/>
    <xsd:complexType name="CompanyType">
	    <xsd:sequence>
		    <xsd:element name="departments" type="company:DepartmentType" />
	    </xsd:sequence>
	    <xsd:attribute name="name" type="xsd:string"/>
	    <xsd:attribute name="employeeOfTheMonth" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="DepartmentType">
	    <xsd:sequence>
    		<xsd:element name="employees" type="company:EmployeeType"   maxOccurs="unbounded"/>
	    </xsd:sequence>
	    <xsd:attribute name="name" type="xsd:string"/>
	    <xsd:attribute name="location" type="xsd:string"/>
    	<xsd:attribute name="number" type="xsd:int"/>
    </xsd:complexType>
    <xsd:complexType name="EmployeeType">
	    <xsd:attribute name="name" type="xsd:string"/>    
	    <xsd:attribute name="SN" type="xsd:ID"/>
	    <xsd:attribute name="manager" type="xsd:boolean"/>
    </xsd:complexType>
 </xsd:schema>


2:java代码

package com.waysoft.intergration.dao;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import commonj.sdo.DataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XSDHelper;
import commonj.sdo.impl.HelperProvider;

public class Test2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HelperContext helperContext = HelperProvider.getDefaultContext();
			XSDHelper xsdHelper = helperContext.getXSDHelper();
			URL url = new File(Test2.class.getResource("/").getPath().substring(1)+"company.xsd").toURI().toURL();
			xsdHelper.define(url.openStream(), url.toString());

			DataGraph graph = SDOUtil.createDataGraph();
			DataObject company = graph.createRootObject("company.ns","CompanyType");
			populateGraph(company);


			Map<String, String> options = new HashMap<String, String>();

			DataObject root = graph.getRootObject();
			System.out.println("company name:"+root.getString("name"));
			System.out.println("company employeeOfTheMonth:"+root.getString("employeeOfTheMonth"));
			
			
			DataObject  departments= root.getDataObject("departments");
			System.out.println("\rdepartments name:"+departments.getString("name"));
			System.out.println("departments location:"+departments.getString("location"));
			System.out.println("departments number:"+departments.getInt("number"));
			
			System.out.println();
			
			@SuppressWarnings("unchecked")
			List<DataObject> employeesList = (List<DataObject>)departments.getList("employees");
			
			for(DataObject employees : employeesList){
				System.out.println("employees name:"+employees.getString("name"));
				System.out.println("employees SN:"+employees.getString("SN"));
				System.out.println("employees manager:"+employees.getString("manager"));
				System.out.println();
			}
			
			
			
			DataObject employeeFromXPathByValue0 = company.getDataObject("departments/employees.0");
			DataObject employeeFromXPathByValue1 = company.getDataObject("departments/employees.1");
			DataObject employeeFromXPathByValue2 = company.getDataObject("departments/employees.2");
			
			System.out.println("employeeFromXPathByValue0 SN:"+employeeFromXPathByValue0.getString("SN"));
			System.out.println("employeeFromXPathByValue1 SN:"+employeeFromXPathByValue1.getString("SN"));
			System.out.println("employeeFromXPathByValue2 SN:"+employeeFromXPathByValue2.getString("SN"));
			
			System.out.println();
			
			DataObject employeeFromXPathByValue_0 = company.getDataObject("departments/employees[SN='E002']");
			DataObject employeeFromXPathByValue_1 = company.getDataObject("departments/employees[SN='E003']");
			DataObject employeeFromXPathByValue_2 = company.getDataObject("departments/employees[SN='E004']");
			
			System.out.println("employeeFromXPathByValue_0 SN:"+employeeFromXPathByValue_0.getString("SN"));
			System.out.println("employeeFromXPathByValue_1 SN:"+employeeFromXPathByValue_1.getString("SN"));
			System.out.println("employeeFromXPathByValue_2 SN:"+employeeFromXPathByValue_2.getString("SN"));
			
			
			
			SDOUtil.saveDataGraph(graph, System.out, options);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private static  void populateGraph(DataObject company) {
		company.setString("name", "ACME");
		company.setString("employeeOfTheMonth", "E001");

		DataObject departments = company.createDataObject("departments");
		departments.setString("name", "Advanced Technologies");
		departments.setString("location", "NS");
		departments.setString("number", "123");
		
		DataObject johnJones = departments.createDataObject("employees");
		johnJones.setString("name", "John Jones");
		johnJones.setString("SN", "E002");

		DataObject janeDoe = departments.createDataObject("employees");
		janeDoe.setString("name", "Jane Doe");
		janeDoe.setString("SN", "E003");

		DataObject fred = departments.createDataObject("employees");
		fred.setString("name", "Fred");
		fred.setString("SN", "E004");
		fred.setString("manager", "true");           
	}

}


3:运行结果

company name:ACME
company employeeOfTheMonth:E001

departments name:Advanced Technologies
departments location:NS
departments number:123

employees name:John Jones
employees SN:E002
employees manager:false

employees name:Jane Doe
employees SN:E003
employees manager:false

employees name:Fred
employees SN:E004
employees manager:true

employeeFromXPathByValue0 SN:E002
employeeFromXPathByValue1 SN:E003
employeeFromXPathByValue2 SN:E004

employeeFromXPathByValue_0 SN:E002
employeeFromXPathByValue_1 SN:E003
employeeFromXPathByValue_2 SN:E004
<?xml version="1.0" encoding="ASCII"?>
<sdo:datagraph xmlns:company="company.ns" xmlns:sdo="commonj.sdo">
  <company:CompanyType name="ACME"
      employeeOfTheMonth="E001">
    <departments name="Advanced Technologies" location="NS" number="123">
      <employees name="John Jones" SN="E002"/>
      <employees name="Jane Doe" SN="E003"/>
      <employees name="Fred" SN="E004" manager="true"/>
    </departments>
  </company:CompanyType>
</sdo:datagraph>


 

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