kXML (XmlPullParser) not hitting END_TAG

Posted by Tejaswi Yerukalapudi on Stack Overflow See other posts from Stack Overflow or by Tejaswi Yerukalapudi
Published on 2010-12-23T17:13:21Z Indexed on 2010/12/24 4:54 UTC
Read the original article Hit count: 354

Hello all,

I'm trying to figure out a way to rewrite some of my XML parsing code. I'm currently working with kXML2 and here's my code -

    byte[] xmlByteArray;
    try {
        xmlByteArray = inputByteArray;
        ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
        InputStreamReader xmlReader = new InputStreamReader(xmlStream);
        KXmlParser parser = new KXmlParser();
        parser.setInput(xmlReader);
        parser.nextTag();

        while(true)
        {
            int eventType = parser.next();
            String tag = parser.getName();
            if(eventType == XmlPullParser.START_TAG)
            {
                System.out.println("****************** STARTING TAG "+tag+"******************");
                if(tag == null || tag.equalsIgnoreCase(""))
                {
                    continue;
                }
                else if(tag.equalsIgnoreCase("Category"))
                {
                    // Gets the name of the category.
                    String attribValue = parser.getAttributeValue(0);
                }
            }
            if(eventType == XmlPullParser.END_TAG)
            {
                System.out.println("****************** ENDING TAG "+tag+"******************");
            }
            else if(eventType == XmlPullParser.END_DOCUMENT)
            {
                break;
            }
    }
    catch(Exception ex)
    {

    }

My input XML is as follows -

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns="">
<Category name="xyz">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="abc">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="def">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>   

My problem briefly is, I'm expecting it to hit XmlPullParser.END_TAG when it encounters a closing xml tag. It does hit the XmlPullParser.START_TAG but it just seems to skip / ignore all the END_TAGs.

Is this how is it's supposed to work? Or am I missing something?

Any help is much appreciated,
Teja.

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about xmlparsing