Apache Tuscany SDO 用法

Altova XMLSpy 工具,方便xml 转schema

package org.test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

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

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

public class Demo {
	
	String test="<?xml version=\"1.0\" encoding=\"UTF-8\"?> "+
				"	<SERVICE xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >"+
				"		<SYS_HEAD>"+
				"			<TRAN_CODE id=\"TRAN_TEST\">TBUT0001</TRAN_CODE>"+
				"			<CHNL_ID>05</CHNL_ID>"+
				"			<USER_NAME/>"+
				"			<CHNL_DATE>20130308</CHNL_DATE>"+
				"			<CHNL_TIME>120911</CHNL_TIME>"+
				"			<CHNL_SEQ/>"+
				"			<IP_ADDR>127.0.0.1</IP_ADDR>"+
				"			<MAC_DATA/>"+
				"		</SYS_HEAD>"+
				"		<APP_HEAD>"+
				"			<PAGE_NO/>"+
				"			<PAGE_SIZE/>"+
				"		</APP_HEAD>"+
				"		<BODY>"+
				"			<OUT_ACCT>66660000000001182</OUT_ACCT>"+
				"			<PWD>234</PWD>"+
				"			<IN_ACCT>66660000000001802</IN_ACCT>"+
				"			<IN_ACCT_NAME/>"+
				"			<TXN_AMT>100</TXN_AMT>"+
				"			<REL_INPUT_FLAG>6</REL_INPUT_FLAG>"+
				"			<LOGIN_NAME/>"+
				"			<CARD_NO/>"+
				"		</BODY>"+
				"	</SERVICE>";

	public static void main(String[] args) {
		Demo demo = new Demo();
		try {
//			demo.runSample();
			demo.runInsertMapToDataObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 取值
	 * @throws IOException
	 */
	public void runSample() throws IOException{
		HelperContext scope = SDOUtil.createHelperContext();
		loadTypesFromXMLSchemaFile(scope, "test.xsd");
		//也可以使用getXMLDocumentFromFile ,从文件中取得
		XMLDocument xmlDoc = getXMLDocumentFromString(scope, test);
		//取得根节点
		DataObject purchaseOrder = xmlDoc.getRootObject();
		//根节点必须取得为对象.
		DataObject dataObject = purchaseOrder.getDataObject("SYS_HEAD");
		//删除根节点
//		dataObject.delete();
		String id = dataObject.getString("CHNL_ID");
		System.out.println(id);
		//取根节点下的子节点,可使用xPath语法
		String chnlId = purchaseOrder.getString("SYS_HEAD/CHNL_ID");
		System.out.println(chnlId);
	}
	/**
	 * 插入map到标准的DataObject作为附加信息
	 * @throws Exception 
	 */
	public void runInsertMapToDataObject() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("test", "test");
		map.put("test1", "test1");
		map.put("test2", "test2");
		map.put("test3", "test3");
		map.put("test4", "test44");
		HelperContext helperContext = SDOUtil.createHelperContext();
		loadTypesFromXMLSchemaFile(helperContext, "test.xsd");
		XMLDocument xmlDocument  = getXMLDocumentFromFile(helperContext, "test.xml");
		//类型转换为DynamicDataObjectImpl
		DynamicDataObjectImpl dataObject = (DynamicDataObjectImpl) xmlDocument.getRootObject();
		//赋值,理论上只要是对象就可以放进去。
		dataObject.dynamicSet(1, map);
		//取根据定义的map的key 取value
		Map<String,Object> map2 = (Map<String, Object>) dataObject.dynamicGet(1);
		System.out.println("-----------------------------"+map2.get("test1"));
		//根据key删除value
		dataObject.dynamicUnset(1);
		String xml = helperContext.getXMLHelper().save(dataObject, "", "");
		System.out.println(xml);
	}
	
	 public void loadTypesFromXMLSchemaFile(HelperContext scope, String fileName) {
	    XSDHelper xsdHelper = scope.getXSDHelper();
	    InputStream is = null;
	    try {
	    	
	        URL url = getClass().getResource("/"+fileName);
	        is = url.openStream();
	        xsdHelper.define(is, url.toString()); 
	    } catch (Exception e) {
	    	e.printStackTrace();
	    } finally {
	       try {
	         is.close();
	       } catch (Exception e) {
	    	   e.printStackTrace();
	       }
	   }
	 }
	 
	 public XMLDocument getXMLDocumentFromFile(HelperContext scope,String filename) throws Exception {
	    XMLDocument result = null;
	    InputStream is = null;
	    try {
	      is = ClassLoader.getSystemResourceAsStream(filename);
	      result = scope.getXMLHelper().load(is);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    } finally {
	      try {
	        is.close();
	      } catch (Exception e) {
	    	  e.printStackTrace();
	      }
	    }
	    return result;
	  }
	 
	 protected XMLDocument getXMLDocumentFromString(HelperContext scope, String xmlDoc) throws IOException {
	    XMLDocument result = null;
	    InputStream is = null;
	    is = new ByteArrayInputStream(xmlDoc.getBytes());
	    result = scope.getXMLHelper().load(is);
	    return result;
	  }
}

tuscany 官网:http://tuscany.apache.org/

tuscany sdo 地址:http://tuscany.apache.org/sdo-overview.html

tuscany sdo 最后一个版本为2008年,貌似已经停止更新了。

你可能感兴趣的:(apache,tuscany,sdo)