5

I am using XmlPullParser to open a file and XPath to get the root. (Later I will modify my code to get the idx node)

However, I am getting the following error:

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

I've searched on the internet but haven't found anything that could resolve this issue.

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 is

<?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 highly appreciated.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
Abid
  • 279
  • 2
  • 19

2 Answers2

2

You are passing wrong attribute to evaluate 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);
}
Tapemaster
  • 487
  • 5
  • 9
ranjk89
  • 1,380
  • 16
  • 21
0

It looks to me as if you should be passing an InputSource as the second argument to xpath.evaluate(), not a parser. Keep in mind that XPath in general may need to walk an entire document tree, so except in some special restricted cases, it cannot work with a streaming parse: you need to read in the whole document, construct an in-memory tree model and apply XPath to that.

Mike Sokolov
  • 6,914
  • 2
  • 23
  • 31