Unknown error in xpath (using xmlpullparser)

I use XmlPullParserto open the file and XPathto get root. (Later I will change my code to get idxnode)

However, I get the following error:

javax.xml.transform.TransformerException: Unknown error in XPath.

I searched the Internet, but did not find anything that could solve this problem.

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);
}

My xml file

<?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>

Your time and help is greatly appreciated.

+5
source share
2 answers

You are passing the wrong attribute to evaluate the method. Try using 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);
}
+2
source

, InputSource xpath.evaluate(), . , XPath , , , : , XPath .

0

All Articles