Question

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.

Was it helpful?

Solution 2

Oops, I'm sorry, as always, it turned out to be a bad idea to write a catch-all block. I didn't notice that there were exceptions in a piece of code that I didn't post here, which is supposed to go into XmlPullParser.END_TAG. Removed it and works like charm :)

OTHER TIPS

Well not sure if this is your exact problem but you are missing a

</root>

Have you tried printing out what eventType type is after start tag and after content?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top