Hi all,

I think I found a way to use the de-/serialization methods outside of
SOAPEnvelopes.  Can somebody validate?

Suppose the base xsd defined type is someXmlType and axis generated a
class named SomeXmlType.java.

To serialize into a Writer do this:

public void print( SomeXmlType x, Writer w )
{
  SerializationContext ctx = new SerializationContext(w);
  ctx.setPretty( true );
  QName rootName = SomeXmlType.getTypeDesc().getXmlType();
  // This attribute adds a default namespace.
  AttributesImpl a = new AttributesImpl();
  a.addAttribute( "", "xmlns", "xmlns", "xsd:string",
rootName.getNamespaceURI() );
  ctx.serialize( new QName( "", "nodename"), a, x, null, new
Boolean(true), new Boolean(false) );
}

The magic string "nodename" must be the name of the root node.

To deserialize, you need a supporting class:

class SomeXmlTypeBuilder extends SOAPHandler
{
  private BeanDeserializer handler;
  public SomeXmlTypeBuilder() {};

  public SOAPHandler onStartChild( String ns, String ln, String pf,
Attributes a, DeserializationContext ctx ) throws SAXException
  {
    handler = (BeanDeserializer) someXmlType.getDeserializer("",
SomeXmlType.class, SomeXmlType.getTypeDesc().getXmlType() );
    return handler;
  }

  public SomeXmlType getValue()
  {
    return (SomeXmlType) handler.getValue();
  }
}

Use it like this:

public SomeXmlType parse( InputSource is )
{
  MessageContext mctx = new MessageContext( new AxisClient() );
  DeserializationContext dctx = new DeserializationContext( is, mctx,
"nodename");
  SomeXmlTypeBuilder b = new SomeXmlTypeBuilder();
  dctx.replaceElementHandler( b );
  dctx.parse();
  return b.getValue();
}
 

Kevin Ruland wrote:

>Hi all,
>
>I'd like to use the generated xsd bindings to serialize/deserialize my
>classes to regular old xml files.  I've tried hacking this together in
>the past but never really got very far.  Does anybody have some sample
>code for me?
>
>Thanks
>
>Kevin
>  
>

Reply via email to