开发者

kXML (XmlPullParser) not hitting END_TAG [closed]

开发者 https://www.devze.com 2023-02-01 04:04 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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>
</Cat开发者_Python百科egory>
<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.


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?


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消