Rav
Rav

Reputation: 11

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

Upvotes: 1

Views: 6871

Answers (2)

polygenelubricants
polygenelubricants

Reputation: 383876

Rpond is correct:

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

References

Upvotes: 0

Robby Pond
Robby Pond

Reputation: 73494

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.

Upvotes: 4

Related Questions