Apache sdo学习笔记(四)——通过动态类型创建DataObject

1:java代码

package com.waysoft.intergration.dao;

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

import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelperContext scope = SDOUtil.createHelperContext();

		scope.getTypeHelper().define(getCustomerType());

		DataObject customer = scope.getDataFactory().create(
				"http://example.com/customer", "Customer");

		customer.setInt("custNum", 123);
		customer.setString("firstName", "范德萨");
		customer.setString("lastName", "太热");

		// --DataObject
		try {
			scope.getXMLHelper().save(customer, "http://example.com/customer",
					"customer", System.out);
			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected static DataObject getCustomerType() {
		HelperContext scope = SDOUtil.createHelperContext();
		TypeHelper typeHelper = scope.getTypeHelper();

		Type intType = typeHelper.getType("commonj.sdo", "Int");
		Type stringType = typeHelper.getType("commonj.sdo", "String");

		DataObject customerType = scope.getDataFactory().create("commonj.sdo",
				"Type");
		customerType.set("uri", "http://example.com/customer");
		customerType.set("name", "Customer");

		DataObject custNumType = customerType.createDataObject("property");
		custNumType.set("name", "custNum");
		custNumType.set("type", intType);

		DataObject firstNameType = customerType.createDataObject("property");
		firstNameType.set("name", "firstName");
		firstNameType.set("type", stringType);

		DataObject lastNameType = customerType.createDataObject("property");
		lastNameType.set("name", "lastName");
		lastNameType.set("type", stringType);

		return customerType;
	}

}


2:运行结果

<?xml version="1.0" encoding="UTF-8"?>
<customer:customer xmlns:customer="http://example.com/customer">
  <custNum>123</custNum>
  <firstName>范德萨</firstName>
  <lastName>太热</lastName>
</customer:customer>


 

你可能感兴趣的:(sdo)