簡體   English   中英

xpath中的未知錯誤(使用xmlpullparser)

[英]Unknown error in xpath (using xmlpullparser)

我使用XmlPullParser打開文件和XPath來獲取root。 (稍后我將修改我的代碼以獲取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>

非常感謝您的時間和幫助。

您將錯誤的屬性傳遞給evaluate方法。 嘗試使用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);
}

它看起來好像你應該將一個InputSource作為第二個參數傳遞給xpath.evaluate(),而不是一個解析器。 請記住,XPath通常可能需要遍歷整個文檔樹,因此除了在一些特殊的受限情況下,它不能與流式解析一起使用:您需要讀取整個文檔,構建內存中的樹模型並應用XPath到那。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM