在从一个schema实例中构造一个DO的时候,发现了一个有趣的问题,代码如下:
String str = "<m:receiveResult xmlns:m=\"http://ordertomanufacturingservice.services.starflow.ro.icss.com/wsdl\">" +
"<orderId>rrrrr4</orderId>" +
"<customerName>String</customerName>" +
"<orderState>ORDER-FINISHED</orderState>" +
"<orderItems>" +
"<orderItem> " +
"<productName>String</productName>" +
"<price>3.1415926535897932384626433832795</price> " +
"<quantity>String</quantity> " +
"<comment>String</comment> " +
"<shipDate>String</shipDate> " +
"</orderItem>" +
"</orderItems>" +
"</m:receiveResult>";
//从字符串创造DataObject
String str2 =
"<m:ReceiveOrder xmlns:m=\"http://ordertomanufacturingservice.services.starflow.ro.icss.com/wsdl/\">" +
"<orderId>testreciiveServcice11111</orderId>" +
"<customerId>String</customerId>" +
"<customerName>String</customerName>" +
"<orderItems>" +
"<orderItem> " +
"<productName>String</productName>" +
"<price>3.1415926535897932384626433832795</price>" +
"<quantity>String</quantity>" +
"<comment>String</comment>" +
"<shipDate>String</shipDate> " +
"</orderItem>" +
"</orderItems>" +
"</m:ReceiveOrder>";
//创造一个DataObject
HelperContext scope = SDOUtil.createHelperContext();
loadTypesFromXMLSchemaStr(scope,schema3);
XMLDocument xmlDoc = getXMLDocumentFromString(scope,str2);
DataObject root = xmlDoc.getRootObject();
//从创建的DO中取值
String orderId = root.get("orderId").toString();
String customerName = root.get("customerName").toString();
System.out.println("rootType:"+root.getType().getName());
System.out.println("orderId: "+orderId);
System.out.println("customerName: "+customerName);
打印的结果为:
rootType:AnyTypeDataObject
orderId: [org.apache.tuscany.sdo.impl.AnyTypeDataObjectImpl@10ffb38
(mixed: [xml.type:text=testreciiveServcice11111], anyAttribute: null)]
customerName: [org.apache.tuscany.sdo.impl.AnyTypeDataObjectImpl@36527f
(mixed: [xml.type:text=String], anyAttribute: null)]
为什么呢?明明我创建的是ReceiveOrder类型,得出的确实AnyTypeDataObjec,AnyTypeDataObjec又是什么意思呢?找不到类型?对了!为什么找不到类型呢,明明指定了相应的namespace,莫非。。。?还是检验下为好,额。。。
就多出一个"/"。。。
通过这个例子,发觉创建的DO是通过namespace来定位对象的类型,而且xsd中的tns和targetnamespace的值是相同的,当namespace写错了,找不到类型也就不足为怪了!