axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Schuerig <mich...@schuerig.de>
Subject Deserializing with a digester
Date Fri, 19 Nov 2004 01:04:22 GMT

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

Mime
View raw message