Android自定义XML解析器无法解析Android命名空间

Android自定义XML解析器无法解析Android命名空间,android,string,xml-namespaces,xmlpullparser,Android,String,Xml Namespaces,Xmlpullparser,我想获得使用XmlResourceParser解析定制android标记的正确方法。我正在将Eclipse3.6与android插件一起使用,我希望像name这样的属性可以用strings.xml中的完整字符串展开 下面是index.xml,它正在res/xml/文件夹中解析 <?xml version="1.0" encoding="utf-8"?> <Index xmlns:android="http://schemas.android.com/apk/res/androi

我想获得使用XmlResourceParser解析定制android标记的正确方法。我正在将Eclipse3.6与android插件一起使用,我希望像
name
这样的属性可以用
strings.xml
中的完整字符串展开

下面是
index.xml
,它正在
res/xml/
文件夹中解析

<?xml version="1.0" encoding="utf-8"?>
<Index xmlns:android="http://schemas.android.com/apk/res/android">
<Sheet
    shortName="o_2sq"
    android:name="@string/o_2sq"
    instructions=""
/>
</Index>
以及使用XmlResourceParser解析第一个
index.xml
的代码片段:

String name = xpp.getAttributeValue(null, "android:name");
String shortName = xpp.getAttributeValue(null, "shortName");
变量
name
包含
null
,但
shortName
包含
“o_2sq”
。我还尝试了以下方法,但没有成功:

String name = xpp.getAttributeValue("android", "name");
写句子的正确方法是什么,使变量名包含“组织两个正方形”?

尝试以下方法:

String name = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");

我找到的解决这个问题的最好办法

String s = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");
String name = null;
if(s != null && s.length() > 1 && s.charAt(0) == '@') {
  int id = Integer.parseInt(s.substring(1));
  name = getString(id);
} else {
  name = "";
}

我认为上面的代码可以帮助您。

很好的尝试。它给出了更好的结果:我的变量
name
现在包含字符串
“@2131099651”
。不确定原因。可能是字符串资源ID。请尝试使用getString(数字);方法获取字符串。您的意思是
else{name=s'}
?似乎这将允许该属性要么是资源,要么是硬编码字符串,不是吗?在我的例子中,我需要名称不为null。这就是为什么。
String s = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");
String name = null;
if(s != null && s.length() > 1 && s.charAt(0) == '@') {
  int id = Integer.parseInt(s.substring(1));
  name = getString(id);
} else {
  name = "";
}
final String NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
final int VALUE_NOT_SET = -1;
int resId = parser.getAttributeResourceValue(NAMESPACE_ANDROID, "name", VALUE_NOT_SET);
String value = null;
if (VALUE_NOT_SET != resId) {
    value = context.getString(resId);
}