parse XML using Sax parser for android
<p>how to parse xml using sax parser? i struggle to parse the author section part into array. i follow example from here <a href="http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/xml-parsing-using-saxparser-with.html" rel="nofollow">http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/xml-parsing-using-saxparser-with.html</a></p>

<p>XML</p>

<pre><code>&lt;catalog&gt;
    &lt;book id="001" lang="ENG"&gt;
        &lt;isbn&gt;23-34-42-3&lt;/isbn&gt;
        &lt;regDate&gt;1990-05-24&lt;/regDate&gt;
        &lt;title&gt;Operating Systems&lt;/title&gt;
        &lt;publisher country="USA"&gt;Pearson&lt;/publisher&gt;
        &lt;price&gt;400&lt;/price&gt;
        &lt;authors&gt;
            &lt;author&gt;
                &lt;name&gt;Ganesh Tiwari&lt;/name&gt;
                &lt;age&gt;1&lt;/age&gt;
            &lt;/author&gt;
        &lt;/authors&gt;
    &lt;/book&gt;
    &lt;book id="002"&gt;
        &lt;isbn&gt;24-300-042-3&lt;/isbn&gt;
        &lt;regDate&gt;1995-05-12&lt;/regDate&gt;
        &lt;title&gt;Distributed Systems&lt;/title&gt;
        &lt;publisher country="Nepal"&gt;Ekata&lt;/publisher&gt;
        &lt;price&gt;500&lt;/price&gt;
        &lt;authors&gt;
            &lt;author&gt;
                &lt;name&gt;Mahesh Poudel&lt;/name&gt;
                &lt;age&gt;2&lt;/age&gt;
            &lt;/author&gt;
                &lt;author&gt;
                &lt;name&gt;Bikram Adhikari&lt;/name&gt;
                &lt;age&gt;3&lt;/age&gt;
            &lt;/author&gt;
            &lt;author&gt;
                &lt;name&gt;Ramesh Poudel&lt;/name&gt;
                &lt;age&gt;4&lt;/age&gt;
            &lt;/author&gt;
        &lt;/authors&gt;
        &lt;/book&gt;
&lt;/catalog&gt;
</code></pre>

<p>start</p>

<pre><code>public BookSaxParser() {
    bookL = new ArrayList&lt;Book&gt;();
    authorL = new ArrayList&lt;Author&gt;();
}


public List&lt;Book&gt; printDatas() {

    return bookL;
}

@Override 
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    // if current element is book , create new book
    // clear tmpValue on start of element

    if (elementName.equalsIgnoreCase("book")) {
        bookTmp = new Book();
        bookTmp.setId(attributes.getValue("id"));
        bookTmp.setLang(attributes.getValue("lang"));

    }

    if (elementName.equalsIgnoreCase("author")) {
        authorTmp = new Author();

    }
    // if current element is publisher
    if (elementName.equalsIgnoreCase("publisher")) {
        bookTmp.setPublisher(attributes.getValue("country"));
    }

}
@Override
public void endElement(String s, String s1, String element) throws SAXException {
    // if end of book element add to list
    if (element.equals("book")) {
        bookL.add(bookTmp);
    }


    if (element.equals("authors")) {
        bookTmp.setAuthors(authorL);

    }

    if (element.equals("author")) {
        authorL.add(authorTmp);

    }

    if(element.equalsIgnoreCase("name")){
        authorTmp.setName(tmpValue);

    }

    if(element.equalsIgnoreCase("age")){
        authorTmp.setAge(Integer.parseInt(tmpValue));
    }




    if (element.equalsIgnoreCase("isbn")) {
        bookTmp.setIsbn(tmpValue);
    }
    if (element.equalsIgnoreCase("title")) {
        bookTmp.setTitle(tmpValue);
    }

    if(element.equalsIgnoreCase("price")){
        bookTmp.setPrice(Integer.parseInt(tmpValue));
    }

    if(element.equalsIgnoreCase("regDate")){
        try {
            bookTmp.setRegDate(sdf.parse(tmpValue));
        } catch (ParseException e) {
            //System.out.println("date parsing error");
        }
    }
}
@Override
public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue = new String(ac, i, j);
}

public void endDocument() throws SAXException {
    // you can do something here for example send
    // the Channel object somewhere or whatever.
}
</code></pre>

<p>`</p>
