Java Mailing List Archive

Home » »

Anyone know how to deserialize xml into java object?

Ivan Cheung


Author LoginPost Reply

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

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("", "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();
                serializer.serialize(_xmlType, new AttributesImpl(), responseFromServer, serializationContext);
                long endTime = System.currentTimeMillis();
                log.debug("serialization done, it takes " + (endTime-startTime)/1000 + " sec");
            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 ) ;
            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.

©2008 - Jax Systems, LLC, U.S.A.