我一直试图从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);