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 Michael Binz <mi...@gmx.de> on 2004/07/14 12:44:16 UTC

How to (de)serialize WSDL types?

Hi,

I'm looking for a possibility to serialise an Axis wsdl2java-generated type
to XML and back into its Java object representation.  This would allow a
simple file-based management of test cases.

I managed the first step, transforming the object into XML, by using the
Axis BeanSerializer and a SerialisationContext (see the code below), but I
suspect that there must be a simpler way.  Can the serialiser (and
deserialiser) that is available from the generated class be used and how?

Is there somewhere a description how this can be done?

Thanks a lot,
Michael.



    /**
     * Serialise the passed object into a stringified XML structure.  The
     * necessary QName can be accessed via the static operation
     * getTypeDesc().getXmlType() on p0's class.
     */
    public static String instanceToXml( Object pO, QName pQname )
    {
        // Since we are not in a message call context, we have to create a
        // message context ourselves.
        MessageContext mc = new MessageContext( new AxisClient() );
        // This means that the XSI type attributes are not generated.
        mc.setProperty( Call.SEND_TYPE_ATTR, "false" );

        // This writer will ultimately receive the generated XML...
        StringWriter result = new StringWriter();
        // ...and is connected to the serialisation context.
        SerializationContextImpl sci = new SerializationContextImpl(
            result,
            mc );
        // Pretty print the result for convenience.
        sci.setPretty(  true );

        // MultiRef'ing means that when object graphs are serialized, the
        // identity of objects is kept, i.e. first the first level objects
are
        // serialized with hrefs to the nested objects.  After that the
nested
        // objects are serialised and so on.
        // This is not what we want.  We want instead an in-place expansion
of
        // nested objects without the hassle of object decomposition, we
have
        // only plain vanilla data structures in the end.
        sci.setDoMultiRefs( false );

        // Create a serializer for the passed object's class...
        BeanSerializer bs = new BeanSerializer(
            pO.getClass(),
            pQname );
        // ...and perform the serialization.
        try
        {
            bs.serialize(
                pQname,
                new AttributesImpl(),
                pO,
                sci );
        }
        catch ( IOException e )
        {
            _logger.warn( "java.io.StringWriter threw IOException.", e );
        }

        return result.toString();
    }