Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TransformerException: A location step was expected" in android xpath

Tags:

android

xml

xpath

Ive been trying to pull data from an xml file but i keep getting this error and im not sure what im doing wrong.

10-23 14:20:29.250: WARN/System.err(3541): --------------- linked to ------------------
10-23 14:20:29.250: WARN/System.err(3541): javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.

Here is my code:

String pill;

        URL url = new URL("file:///mnt/sdcard/cpdata/cpxml.xml");
        InputSource xml = new InputSource(url.openStream());
        XPath xpath = XPathFactory.newInstance().newXPath();


        pill = xpath.evaluate("//data/monday/p1/",xml);
        pills.add(pill);
        Log.d("PILLLLLLSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS", pill);
        tv.setText(pill + "hi");

And this is my xml document:

-<data> 
  -<monday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
  </monday> 
  -<tuesday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
  </tuesday> 
  -<wednesday> 
    <p1>1.0</p1> 
    <p2>test1</p2> 
</wednesday> 
-<thursday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
</thursday> 
-<friday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
</friday> 
-<saturday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
</saturday> 
-<sunday> 
    <p1>test1</p1> 
    <p2>test1</p2> 
</sunday> 

like image 232
Peter Avatar asked Oct 23 '11 18:10

Peter


1 Answers

The isssue is obvious:

pill = xpath.evaluate("//data/monday/p1/",xml);

The XPath expression used:

    data/monday/p1/

ends with "/" and thus is syntactically illegal.

Use:

  pill = xpath.evaluate("//data/monday/p1",xml);
like image 159
Dimitre Novatchev Avatar answered Nov 20 '22 03:11

Dimitre Novatchev