在Android(JAVA)中对字符串使用XPath

在Android(JAVA)中对字符串使用XPath,java,xml,android,xpath,Java,Xml,Android,Xpath,我正在寻找一些在Android中使用xpath的例子?或者任何人都可以分享他们的经历。我一直在努力弄清这个问题的症结所在:-( 我有一个包含标准xml文件的字符串。我认为我需要将其转换为xml文档。我发现了以下代码,我认为这将起到作用: public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException { Document

我正在寻找一些在Android中使用xpath的例子?或者任何人都可以分享他们的经历。我一直在努力弄清这个问题的症结所在:-(

我有一个包含标准xml文件的字符串。我认为我需要将其转换为xml文档。我发现了以下代码,我认为这将起到作用:

public static Document stringToDom(String xmlSource) 
throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xmlSource)));
}
下一步 假设上面的代码没有问题,我需要应用xpath从cat获取值:“/animal/emdamma/feline/cat”

我在这里查看dev文档:也可以在线查看,但我不确定从哪里开始

我已尝试使用以下代码:

 XPathFactory xPathFactory = XPathFactory.newInstance();
// To get an instance of the XPathFactory object itself.

XPath xPath = xPathFactory.newXPath();
// Create an instance of XPath from the factory class.

String expression = "SomeXPathExpression";
XPathExpression xPathExpression = xPath.compile(expression);
// Compile the expression to get a XPathExpression object.

Object result = xPathExpression.evaluate(xmlDocument);
// Evaluate the expression against the XML Document to get the result.
但我得到“无法解决”。Eclipse似乎无法修复此导入。我尝试手动输入:

javax.xml.xpath.XPath
但这并不奏效


有人知道我可以为Android平台使用哪些好的源代码吗?1.5

您使用的SDK版本是什么?XPath是在SDK 8(2.2)中引入的。如果您不是根据该版本构建的,那么该类就不存在。

您使用的SDK版本是什么?XPath是在SDK 8(2.2)中引入的。如果您不是根据该版本生成,则该类不存在。

Rpond是正确的:

  • javax.xml.xpath
    是从API级别8(Android 2.2)开始引入的
  • Android 1.5是唯一的API级别3
工具书类
  • -“自:API级别8”
    • --举例说明
  • ——“平台版本Android 1.5-API级别3”
Rpond是正确的:

  • javax.xml.xpath
    是从API级别8(Android 2.2)开始引入的
  • Android 1.5是唯一的API级别3
工具书类
  • -“自:API级别8”
    • --举例说明
  • ——“平台版本Android 1.5-API级别3”

这已经晚了将近一年,但我希望你找到了SAXParser,不必用正则表达式来处理XML…这已经晚了将近一年,但我希望你找到了SAXParser,不必用正则表达式来处理XML…FWIW你的代码对我来说很好。(使用SDK 8)对于将来找到这篇文章的人来说值得注意。(我缺少了xpathexpression对象和编译步骤)FWIW您的代码对我来说很好。(使用SDK 8)对于将来找到这篇文章的人来说值得注意。(我缺少了xpathexpression对象和编译步骤)