我使用XmlPullParser打开一个文件,使用XPath获取根目录。(稍后我将修改代码以获取idx节点)
但是,我得到了以下错误:
javax.xml.transform.TransformerException: Unknown error in XPath.我已经在互联网上搜索过了,但没有找到任何可以解决这个问题的东西。
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>非常感谢您的时间和帮助。
发布于 2013-02-14 02:43:14
您正在将错误的属性传递给计算方法。尝试使用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);
}发布于 2013-02-07 10:23:10
在我看来,您应该将InputSource作为第二个参数传递给xpath.evaluate(),而不是解析器。请记住,XPath通常可能需要遍历整个文档树,因此除非在某些特殊的受限情况下,否则它不能与流式解析一起工作:您需要读取整个文档,构造一个内存树模型,并对其应用XPath。
https://stackoverflow.com/questions/11891193
复制相似问题