Using XPath on String in Android (JAVA)
Posted
by Rav
on Stack Overflow
See other posts from Stack Overflow
or by Rav
Published on 2010-06-04T15:38:09Z
Indexed on
2010/06/08
10:02 UTC
Read the original article
Hit count: 304
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
© Stack Overflow or respective owner