我正在使用 XmlPullParser
打开文件并使用 XPath
获取根目录。 (后面我会修改我的代码获取idx
节点)
但是,我收到以下错误:
javax.xml.transform.TransformerException: Unknown error in XPath.
我在 Internet 上搜索过,但没有找到任何可以解决此问题的方法。
try{
XmlPullParser xpp = getResources().getXml(R.xml.x1);
XPath xpath = XPathFactory.newInstance().newXPath();
String askFor2 = "/root";
NodeList creaturesNodes = (NodeList) xpath.evaluate(askFor2, xpp, XPathConstants.NODESET);
Log.d("", "");
} catch (Exception ex) {
String err = (ex.getMessage()==null)?"run thread failed":ex.getMessage();
Log.e("bm run catch", err);
}
我的 XML 文件是
<?xml version="1.0"?>
<root>
<child index="1">
<idx index="1" />
<idx index="2" />
<idx index="3" />
<idx index="4" />
<idx index="5" />
<idx index="6" />
<idx index="7" />
</child>
</root>
非常感谢您的宝贵时间和帮助。
最佳答案
您将错误的属性传递给评估方法。尝试使用 InputSource,
try{
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource is = new InputSource(getResources().openRawResource(R.raw.xm));
String askFor2 = "/root";
NodeList creaturesNodes = (NodeList) xpath.evaluate(askFor2, is, XPathConstants.NODESET);
Log.d("", "");
} catch (Exception ex) {
Log.e("bm run catch", err);
}
关于android - xpath 中的未知错误(使用 xmlpullparser),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11891193/