我正在寻找一些在 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/mammal/feline/cat" 获取值
我在这里查看开发文档:http://developer.android.com/reference/javax/xml/xpath/XPath.html也上网查了一下,但我不知道从哪里开始!
我尝试使用以下代码:
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