XmlPullParser getAttributeValue returns null
Asked Answered
E

1

8

I have the following XML structure stored in my assets/xml folder:

<?xml version="1.0" encoding="utf-8"?>
<homescreen>
    <homeitem name="Name1"
              subtext="Description1"
              icon="iconresource1" />
    <homeitem name="Name2"
              subtext="Description2"
              icon="iconresource2" />
    <homeitem name="Name3"
              subtext="Description3"
              icon="iconresource3" />
</homescreen>

I'm reading each individual homeitem using an XmlPullParser:

int event;
String TAG_ITEM = "homeitem";
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
    if (event == XmlPullParser.START_TAG) {
        String tag = parser.getName();
        if (TAG_ITEM.equals(tag)) {
            // Works - Logs "Name1"
            Log.d(LOG_NAME, parser.getAttributeValue(0));

            // Works - Logs "name"
            Log.d(LOG_NAME, parser.getAttributeName(0));

            // Works - Logs ""
            Log.d(LOG_NAME, parser.getAttributeNamespace(0));

            // Fails - Logs null
            Log.d(LOG_NAME, parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name"));
        }
    }
}

My problem is: using getAttributeValue(String, String) always returns null. Using getAttributeValue(Integer) works fine. What am I doing wrong?

Device: Nexus 10, Stock KitKat 4.4

Ecumenism answered 26/11, 2013 at 11:47 Comment(0)
S
19

try this :

parser.getAttributeValue(null, "name"); 
Stantonstanway answered 26/11, 2013 at 12:21 Comment(6)
This works. I guess I was just assuming this wanted the same namespace as returned by getAttributeNamespace.Ecumenism
What are supposed to be passing (or not passing, as it seems) to this parameter anyway?Inflect
Works just use this in START_TAG caseAbbyabbye
why do we always pass null in the first parameter(namespace) ?Ammieammine
I think its default value if attribute does not exist.Margrettmarguerie
@KuldipSharma it's not the default value, it's the namespace. Would usually be left nullBreviary

© 2022 - 2024 — McMap. All rights reserved.