2012-08-09 89 views
5

我使用XmlPullParser来打开一个文件,并使用XPath来获取根目录。 (后来,我将修改我的代码,以获得idx节点)xpath中的未知错误(使用xmlpullparser)

不过,我收到以下错误:

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> 

你的时间和帮助表示高度赞赏。

回答

0

它看起来像我应该将InputSource作为第二个参数传递给xpath.evaluate(),而不是解析器。请记住,一般来说XPath可能需要遍历整个文档树,除了在某些特殊的受限情况下,它不能用于流式分析:您需要读入整个文档,构建内存树模型并应用XPath。

2

您正在通过错误的属性来评估方法。尝试使用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); 
}