在android xpath中“TransformerException:一个预期的位置步骤”

前端之家收集整理的这篇文章主要介绍了在android xpath中“TransformerException:一个预期的位置步骤”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图从xml文件提取数据,但我不断收到此错误,我不知道我做错了什么.
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.

这是我的代码

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

这是我的xml文档:

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

解决方法

问题很明显:

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

使用的XPath表达式:

data/monday/p1/

以“/”结尾,因此在语法上是非法的.

使用:

pill = xpath.evaluate("//data/monday/p1",xml);
原文链接:https://www.f2er.com/android/315412.html

猜你在找的Android相关文章