Question

I am trying to parse some XML content using XMLPullParser in android, but I am stuck at one point. Here is the XML code snippet:

<admin2 type="ABC" code="123" id="123">ABCD</admin2>

and here is my code:

 int eventType = parser.getEventType();
                    while (eventType != XmlPullParser.END_DOCUMENT){
                        String tagName=parser.getName();

                        switch (eventType){
                            case XmlPullParser.START_TAG:
                                break;


                            case XmlPullParser.END_TAG:
                                if(tagName.startsWith("admin")){
                                     //expected 3.. but I am getting as -1
                                     String ID=parser.getAttributeCount();
                                }
              }

I expect ID to have its values as 3 but I am getting it as -1. Rest of the code seems to be working fine. Any help would be greatly appreciated.

Thanks

Was it helpful?

Solution

public abstract int getAttributeCount ()

Added in API level 1

Returns the number of attributes of the current start tag, or -1 if the current event type is not START_TAG

Basically you might use this just within START_TAG. As you're using it within END_TAG, it's logical that you're receiving a -1 value.

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