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 Schuerig <mi...@schuerig.de> on 2004/11/19 02:04:22 UTC

Deserializing with a digester

Lest anyone spends entirely too much time on this stuff, as I just did, 
here's how to use a Commons Digester to implement a Deserializer.

Michael


import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.DeserializerImpl;
import org.apache.axis.message.SOAPHandler;
import org.apache.commons.digester.Digester;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class DocDeserializer extends DeserializerImpl {
    private final Digester _dig;
    
    public DocDeserializer() {
        _dig = new Digester();

        // Your digester rules go here
        _dig.addObjectCreate("doc", de.schuerig.doclittest.Doc.class);
        _dig.addBeanPropertySetter("doc/child");
        _dig.addObjectCreate("doc/list/item",
                             de.schuerig.doclittest.Item.class);
        _dig.addBeanPropertySetter("doc/list/item", "text");
        _dig.addSetNext("doc/list/item", "addItem");
    }
    
        
    public void onStartElement(
            String namespace,
            String localName,
            String prefix,
            Attributes attributes,
            DeserializationContext context)
       throws SAXException {
        _dig.startElement(namespace, localName, "", attributes);
    }

    
    public void onEndElement(
            String namespace,
            String localName,
            DeserializationContext context)
       throws SAXException {
        _dig.endElement(namespace, localName, "");
        
        setValue(_dig.getRoot());
    }

    
    public SOAPHandler onStartChild(
            String namespace,
            String localName,
            String prefix,
            Attributes attributes,
            DeserializationContext context)
       throws SAXException {
        return this;
    }
    
    
    public void characters( char[] chars, int start, int end )
            throws SAXException {
        _dig.characters(chars, start, end);
    }
}


-- 
Michael Schuerig                            You can twist perceptions
mailto:michael@schuerig.de                        Reality won't budge
http://www.schuerig.de/michael/               --Rush, Show Don't Tell