开发者

Using XPath on String in Android (JAVA)

I am looking for some examples of using xpath in Android? Or if anyone can share their experiences. I have been struggeling to make tail or head of this problem :-(

I have a string that contains a standard xml file. I believe I need to convert that into an xml document. I have found this code which I think will do the trick:

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

Next steps Assuming the code above is OK, I need to apply xpath to get values from cat: "/animal/mammal/feline/cat"

I look at the dev doc here: http://developer.android.com/reference/javax/xml/xpath/XPath.html and also look online, but I am not sure where to start!

I have tried to use the following code:

 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 ex开发者_Python百科pression against the XML Document to get the result.

But I get "Cannot be resolved". Eclipse doesn't seem to be able to fix this import. I tried manually entering:

javax.xml.xpath.XPath

But this did not work.

Does anyone know any good source code that I can utilise, for Android platform? 1.5


What version of the SDK are you using? XPath was introduced in SDK 8(2.2). If you aren't building against that version then the class doesn't exist.


Rpond is correct:

  • javax.xml.xpath is introduced since API level 8 (Android 2.2).
  • Android 1.5. is only API Level 3.

References

  • package javax.xml.xpath -- "Since: API Level 8"
    • Description -- with examples
  • Android API levels -- "Platform Version Android 1.5 - API Level 3"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜