You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Ivan Cheung <iv...@pobox.com> on 2010/02/12 01:47:53 UTC
Anyone know how to deserialize xml into java object?
Hi,
I have web service that I need to integrate with, the service is unable and
slow.
I am trying to mock it out (by building a simulator) so in case it is down,
we can use the simulator and our dev is not affected.
I have used wsdl2java to generate the client stub, I was able to serialized
the response from the web service into an xml file using the following code
ImplRetrieveOfferAndProductCon
figurationOutput responseFromServer = <stub.calling web service...>
// serialize object
{
// log the output
java.lang.String mechType = "";
java.lang.Class _javaType =
ImplRetrieveOfferAndProductConfigurationOutput.class;
javax.xml.namespace.QName _xmlType = new
javax.xml.namespace.QName("http://datatypes.ordering.ws.impl.css.amdocs.com",
"ImplRetrieveOfferAndProductConfigurationOutput");
org.apache.axis.encoding.Serializer serializer =
ImplRetrieveOfferAndProductConfigurationOutput.getSerializer(mechType,
_javaType, _xmlType);
BufferedWriter writer = new BufferedWriter ( new
FileWriter("response.xml"));
SerializationContext serializationContext = new
SerializationContext(writer);
long startTime = System.currentTimeMillis();
try
{
serializationContext.setPretty(true);
serializer.serialize(_xmlType, new AttributesImpl(),
responseFromServer, serializationContext);
long endTime = System.currentTimeMillis();
log.debug("serialization done, it takes " +
(endTime-startTime)/1000 + " sec");
writer.flush();
writer.close();
}
catch (final Exception e)
{
long endTime = System.currentTimeMillis();
log.debug("serialization fail, it takes " +
(endTime-startTime)/1000 + " sec");
}
}
The problem I have now is I can't find a way to deserialize the object from
the xml file ( I have to add the soap envelope to the xml by hand since
without that , the DeserializationContext.parse() method is throwing
exception).
If I can overcome the problem then I will be able build a simulator which
should speed up our development.
I try to use the BeanDeserializer and I was not successful, here is what I
have tried, the parse method is parsing the doc but desrializer.getValue()
is returning an object with nothing (the instance variables are all null ,
but the xml file is fully popualted) in it. Here is the sample code that I
come up.
// deserialize object
{
long startTime = System.currentTimeMillis();
org.xml.sax.InputSource is = new org.xml.sax.InputSource(new
FileReader("response.xml"));
String messageType = "";
org.apache.axis.MessageContext messageContext = null;
org.apache.axis.encoding.DeserializationContext dc = new
DeserializationContext(is, messageContext, messageType ) ;
dc.parse();
org.apache.axis.encoding.Deserializer desrializer =
dc.getDeserializerForClass(ImplRetrieveOfferAndProductConfigurationOutput.class);
ImplRetrieveOfferAndProductConfigurationOutput roapcFromFile=
(ImplRetrieveOfferAndProductConfigurationOutput)desrializer.getValue();
long endTime = System.currentTimeMillis();
log.debug("deserialization done, it takes " +
(endTime-startTime)/1000 + " sec");
}
Any help / pointer on how to deserialize object (class generated from
wsdl2java) from xml would be greatly appreciated.
Thanks,
Ivan