Axis2中的对象和字符串相互转化
时间:2009-12-13 BlogJava 夜露死苦
在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());
|