XmlPullParser getAttributeValue返回null

Cod*_*gue 6 xml android xmlpullparser xml-parsing

我在assets/xml文件夹中存储了以下XML结构:

<?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>
Run Code Online (Sandbox Code Playgroud)

我正在homeitem使用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"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:using getAttributeValue(String, String) always总是返回null.使用getAttributeValue(Integer)工作正常.我究竟做错了什么?

设备:Nexus 10,Stock KitKat 4.4

Har*_*thi 17

试试这个 :

parser.getAttributeValue(null, "name"); 
Run Code Online (Sandbox Code Playgroud)

  • 这可行。我想我只是在假设它要使用与getAttributeNamespace返回的名称空间相同的名称空间。 (2认同)
  • 为什么我们总是在第一个参数(命名空间)中传递null? (2认同)