在Web Service中定義了復雜類型之後,Axis2會生成對應的類對象。這些類都是可以序列化的。
可以把這些類的實例和字符串之間做相互轉化。
// 對象轉換成字符串
StringWriter stringWriter = new StringWriter();
XMLStreamWriter xmlStreamWriter = StAXUtils
.createXMLStreamWriter(stringWriter);
MTOMAwareXMLStreamWriter mtomAwareXMLStreamWriter = new MTOMAwareXMLSerializer(
xmlStreamWriter);
userCredentialsType.serialize(new QName(
"http://newautovideo.com/siteengine/ws/types", "ns1"),
OMAbstractFactory.getSOAP11Factory(), mtomAwareXMLStreamWriter);
xmlStreamWriter.flush();
xmlStreamWriter.close();
String omElementString = stringWriter.toString();
System.out.println("OM String ==> " + omElementString);
// 字符串轉換成對象
XMLStreamReader xmlReader = StAXUtils
.createXMLStreamReader(new ByteArrayInputStream(omElementString
.getBytes()));
UserCredentialsType result = UserCredentialsType.Factory
.parse(xmlReader);
System.out.println("OM Object==> " + result.getUserID());