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

来源:互联网 发布:詹姆斯打球特点 知乎 编辑:程序博客网 时间:2024/05/01 20:44

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", "太热");// --DataObjecttry {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>


 

原创粉丝点击