xml parsing - Android: How to parse this kind of XML -


i have xml document, , need parse in order extract, each item, value of:

<title> <description> <articledate> <story> <author> <photo> <caption> 

i trying dom, lost because each node

<item> 

of root

<channel>  

has 3 children:

<article> <media>  <link> 

i've had lot of success using sax, it's fast , simple. got idea simple , 'recursive' method show below websites demonstrated template based parser generator. hand, extend 'defaulthandler' class.

the real trick here, each object type gets it's own 'defaulthandler' extension knows how parse type , type alone. makes refactoring , reusing parsers xml , parsers possible. then, whenever see start of new child 'element' of type, create empty child container , it's extension of 'defaulthandler'. set current handler 1 type , let recursively chomp away.

there improvements make too, pushing of common methods own base class extends 'defaulthandler'. created 'ixmlserializer' interface, write these out. there may better way too.

taking sting out of sax

based on this, came strategy start creating objects each of element types...

public class collectiontype implements ixmlserializer{  public static final string  type_tag = "collection";  protected identitytype identity; protected list<propertytype> property; protected list<itemreferencetype> itemreference; protected list<sporetype> spore; protected list<regiontype> region; protected list<backpackitemtype> backpackitem;   /**  * gets value of identity property.  *   * @return  *     possible object  *     {@link identitytype }  *       */ public identitytype getidentity() {     return identity; }  /**  * sets value of identity property.  *   * @param value  *     allowed object  *     {@link identitytype }  *       */ public void setidentity(identitytype value) {     this.identity = value; }   /**  * gets value of property property.  *   * <p>  * accessor method returns reference live list,  * not snapshot. therefore modification make  * returned list present inside jaxb object.  * why there not <code>set</code> method property property.  *   * <p>  * example, add new item, follows:  * <pre>  *    getproperty().add(newitem);  * </pre>  *   *   * <p>  * objects of following type(s) allowed in list  * {@link propertytype }  *   *   */ public list<propertytype> getproperty() {     if (property == null) {         property = new arraylist<propertytype>();     }     return this.property; }  /**  * gets value of itemreference property.  *   * <p>  * accessor method returns reference live list,  * not snapshot. therefore modification make  * returned list present inside jaxb object.  * why there not <code>set</code> method itemreference property.  *   * <p>  * example, add new item, follows:  * <pre>  *    getitemreference().add(newitem);  * </pre>  *   *   * <p>  * objects of following type(s) allowed in list  * {@link itemreferencetype }  *   *   */ public list<itemreferencetype> getitemreference() {     if (itemreference == null) {         itemreference = new arraylist<itemreferencetype>();     }     return this.itemreference; }  /**  * gets value of spore property.  *   * <p>  * accessor method returns reference live list,  * not snapshot. therefore modification make  * returned list present inside jaxb object.  * why there not <code>set</code> method spore property.  *   * <p>  * example, add new item, follows:  * <pre>  *    getspore().add(newitem);  * </pre>  *   *   * <p>  * objects of following type(s) allowed in list  * {@link sporetype }  *   *   */ public list<sporetype> getspore() {     if (spore == null) {         spore = new arraylist<sporetype>();     }     return this.spore; }  /**  * gets value of region property.  *   * <p>  * accessor method returns reference live list,  * not snapshot. therefore modification make  * returned list present inside jaxb object.  * why there not <code>set</code> method region property.  *   * <p>  * example, add new item, follows:  * <pre>  *    getregion().add(newitem);  * </pre>  *   *   * <p>  * objects of following type(s) allowed in list  * {@link regiontype }  *   *   */ public list<regiontype> getregion() {     if (region == null) {         region = new arraylist<regiontype>();     }     return this.region; }  /**  * gets value of backpackitem property.  *   * <p>  * accessor method returns reference live list,  * not snapshot. therefore modification make  * returned list present inside jaxb object.  * why there not <code>set</code> method backpackitem property.  *   * <p>  * example, add new item, follows:  * <pre>  *    getbackpackitem().add(newitem);  * </pre>  *   *   * <p>  * objects of following type(s) allowed in list  * {@link backpackitemtype }  *   *   */ public list<backpackitemtype> getbackpackitem() {     if (backpackitem == null) {         backpackitem = new arraylist<backpackitemtype>();     }     return this.backpackitem; }  @override public void serializetype(xmlserializer serializer, string namespace)         throws illegalargumentexception, illegalstateexception, ioexception {      serializer.starttag(namespace, type_tag);     getidentity().serializetype(serializer, namespace);      iterator<propertytype> pitor = getproperty().iterator();     while (pitor.hasnext()){         pitor.next().serializetype(serializer, namespace);     }      iterator<itemreferencetype> iitor = getitemreference().iterator();     while (iitor.hasnext()){         iitor.next().serializetype(serializer, namespace);     }      iterator<sporetype> sitor = getspore().iterator();     while (sitor.hasnext()){         sitor.next().serializetype(serializer, namespace);     }      iterator<regiontype> ritor = getregion().iterator();     while (ritor.hasnext()){         ritor.next().serializetype(serializer, namespace);     }      iterator<backpackitemtype> bitor = getbackpackitem().iterator();     while (bitor.hasnext()){         bitor.next().serializetype(serializer, namespace);     }      serializer.endtag(namespace, type_tag); } 

}

then, create top level document handler....

public class documenthandler extends defaulthandler { public static final string      tag = documenthandler.class.getsimplename(); private static final boolean    mdebuglogging = true;  saxparserfactory        msaxparserfactory; saxparser               msaxparser; xmlreader               mxmlreader; stack<string>           melementstack = new stack<string>();  private collectiontype  mcollection = null; private requesttype     mrequest = null; private responsetype    mresponse = null;     public documenthandler() throws saxexception, parserconfigurationexception{     msaxparserfactory = saxparserfactory.newinstance();     msaxparserfactory.setnamespaceaware(true);      //  must set true, otherwise no attributes!     //m_saxparserfactory.setfeature("http://xml.org/sax/features/namespaces", true);     //m_saxparserfactory.setfeature("http://xml.org/sax/features/namespace-prefixes", true);     msaxparser = msaxparserfactory.newsaxparser();      if (mdebuglogging) {         log.d(tag, "sax parser namespace aware? " + msaxparser.isnamespaceaware());         log.d(tag, "sax parser validating? " + msaxparser.isvalidating());     }     mxmlreader = msaxparser.getxmlreader(); }  @override public void enddocument() throws saxexception {     if (mdebuglogging) {         log.d(tag, "beginning document parse");     }     super.enddocument(); }     @override public void startdocument() throws saxexception {     if (mdebuglogging) {         log.d(tag, "ending document parse");     }     super.startdocument(); }   @override public void startelement(java.lang.string uri, java.lang.string localname, java.lang.string qname, attributes attributes) throws saxexception {     if (qname.equals("collection")) {         mcollection = new collectiontype();         final defaulthandler handler = new collectionhandler(melementstack, mcollection, attributes, mxmlreader, this);         melementstack.push ("serverresponse");         mxmlreader.setcontenthandler (handler);     }     else if (qname.equals("request")){         mrequest = new requesttype();         final defaulthandler handler = new requesthandler(melementstack, mrequest, attributes, mxmlreader, this);         melementstack.push("request");         mxmlreader.setcontenthandler(handler);     }     else if (qname.equals("response")){         mresponse = new responsetype();         final defaulthandler handler = new responsehandler(melementstack, mresponse, attributes, mxmlreader, this);         melementstack.push("response");         mxmlreader.setcontenthandler(handler);     } }   @override public void endelement(java.lang.string uri, java.lang.string localname, java.lang.string qname) throws saxexception {}   public void parse(inputsource is) throws saxexception, ioexception {     mxmlreader.setcontenthandler(this);     mxmlreader.parse(is); }   public requesttype parserequest(inputstream instream) throws saxexception, ioexception {     final inputsource insource = new inputsource(instream);     parse(insource);     return mrequest; }  public collectiontype parsecollection(inputstream instream) throws saxexception, ioexception {     final inputsource insource = new inputsource(instream);     parse(insource);     return mcollection; }  public responsetype parseresponse(inputstream instream) throws saxexception, ioexception {     final inputsource insource = new inputsource(instream);     parse(insource);     return mresponse; } 

}

then extend 'defaulthandler' each of types this:

public class collectionhandler extends defaulthandler { private final chararraywriter m_textbuffer = new chararraywriter (); private final stack m_elementstack; private final defaulthandler m_parent; private final xmlreader m_parser;

private final collectiontype            m_collection;  public collectionhandler(stack<string> path, collectiontype collection, attributes attributes, xmlreader parser, defaulthandler parent) throws saxexception{     m_elementstack = path;     m_collection = collection;     m_parser = parser;     m_parent = parent;     start(attributes); }  private void start(attributes attributes){}   @override public void startelement(java.lang.string uri, java.lang.string localname, java.lang.string qname, attributes attributes) throws saxexception {     m_textbuffer.reset();      if (qname.equals("identity")){         final identitytype identity = new identitytype();         m_collection.setidentity(identity);         final defaulthandler handler = new identityhandler(m_elementstack, identity, attributes, m_parser, this);         m_elementstack.push("identity");         m_parser.setcontenthandler(handler);     }      if (qname.equals("backpackitem")){         final backpackitemtype backpackitem = new backpackitemtype();         m_collection.getbackpackitem().add(backpackitem);         final defaulthandler handler = new backpackitemhandler(m_elementstack, backpackitem, attributes, m_parser, this);         m_elementstack.push("backpackitem");         m_parser.setcontenthandler(handler);     }      else if (qname.equals("itemreference")){         final itemreferencetype itemreference = new itemreferencetype();         m_collection.getitemreference().add(itemreference);         final defaulthandler handler = new itemreferencehandler(m_elementstack, itemreference, attributes, m_parser, this);         m_elementstack.push("itemreference");         m_parser.setcontenthandler(handler);     }      else if (qname.equals("property")){         final propertytype property = new propertytype();         m_collection.getproperty().add(property);         final defaulthandler handler = new propertyhandler(m_elementstack, property, attributes, m_parser, this);         m_elementstack.push("property");         m_parser.setcontenthandler(handler);     }      else if (qname.equals("region")){         final regiontype toinsert = new regiontype();         m_collection.getregion().add(toinsert);         final defaulthandler handler = new regionhandler(m_elementstack, toinsert, attributes, m_parser, this);         m_elementstack.push("region");         m_parser.setcontenthandler(handler);     }      else if (qname.equals("spore")){         final sporetype toinsert = new sporetype();         m_collection.getspore().add(toinsert);         final defaulthandler handler = new sporehandler(m_elementstack, toinsert, attributes, m_parser, this);         m_elementstack.push ("spore");         m_parser.setcontenthandler (handler);     } }   @override public void endelement(java.lang.string uri, java.lang.string localname, java.lang.string qname) throws saxexception {     if (qname.equals("collection")){         m_elementstack.pop();         m_parser.setcontenthandler(m_parent);     } } 

}


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -