Apache SDO学习笔记(一)——通过xml字符串创建DataObject对象

 
 
package com.waysoft.intergration.dao;

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

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

废话不说,直接上代码。。
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelperContext scope = SDOUtil.createHelperContext();
		XMLDocument document = scope.getXMLHelper().load(XML_STRING);
		DataObject root = document.getRootObject();
		String xml = scope.getXMLHelper().save(root,"http://www.example.com/PO","purchaseOrder");
		System.out.println(xml);
	}
	public static String XML_STRING = 
		    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
		    "<po:purchaseOrder xmlns:po=\"http://www.example.com/PO\" orderDate=\"2010-07-07\">\n"+
		    "  <shipTo country=\"US\">\n"+
		    "    <name>Alice Smith</name>\n"+
		    "    <street>123 Maple Street</street>\n"+
		    "    <city>Mill Valley</city>\n"+
		    "    <state>CA</state>\n"+
		    "    <zip>90952</zip>\n"+
		    "   </shipTo>\n"+
		    "  <billTo country=\"US\">\n"+
		    "    <name>Robert Smith</name>\n"+
		    "    <street>8 Oak Avenue</street>\n"+
		    "    <city>Mill Valley</city>\n"+
		    "    <state>PA</state>\n"+
		    "    <zip>95819</zip>\n"+
		    "  </billTo>\n"+
		    "  <po:comment>Hurry, my lawn is going wild!</po:comment>\n"+
		    "  <items>\n"+
		    "    <item partNum=\"872-AA\">\n"+
		    "      <productName>Lawnmower</productName>\n"+
		    "      <price>148.95</price>\n"+
		    "      <quantity>1</quantity>\n"+
		    "      <po:comment>Confirm this is electric</po:comment>\n"+
		    "    </item>\n"+
		    "    <item partNum=\"926-AA\">\n"+
		    "      <productName>Baby Monitor</productName>\n"+
		    "      <price>39.98</price>\n"+
		    "      <quantity>1</quantity>\n"+
		    "      <shipDate>1999-05-21</shipDate>\n"+
		    "    </item>\n"+
		    "  </items>\n"+
		    "</po:purchaseOrder>\n";

}
运行结果如下:
 
<?xml version="1.0" encoding="UTF-8"?>
<PO:purchaseOrder xmlns:PO="http://www.example.com/PO" orderDate="2010-07-07">
  <shipTo country="US">
    <name>Alice Smith</name>
    <street>123 Maple Street</street>
    <city>Mill Valley</city>
    <state>CA</state>
    <zip>90952</zip>
   </shipTo>
  <billTo country="US">
    <name>Robert Smith</name>
    <street>8 Oak Avenue</street>
    <city>Mill Valley</city>
    <state>PA</state>
    <zip>95819</zip>
  </billTo>
  <PO:comment>Hurry, my lawn is going wild!</PO:comment>
  <items>
    <item partNum="872-AA">
      <productName>Lawnmower</productName>
      <price>148.95</price>
      <quantity>1</quantity>
      <PO:comment>Confirm this is electric</PO:comment>
    </item>
    <item partNum="926-AA">
      <productName>Baby Monitor</productName>
      <price>39.98</price>
      <quantity>1</quantity>
      <shipDate>1999-05-21</shipDate>
    </item>
  </items>
</PO:purchaseOrder>




你可能感兴趣的:(Apache SDO学习笔记(一)——通过xml字符串创建DataObject对象)