Question

Is the method .selectNodes available in Android ? I need to access particular data in a xhtml code using its XPath.

I read the method exists in Android since API Level 8, but I don't know how to use it and what packages to import in my code.

PS: I am used to use getElementsByTagName, but it isn't as powerful as SelectNodes.

NodeList nodes = doc.getElementsByTagName("title");
Was it helpful?

Solution

The XPath package is indeed supported since API Level 8. If you're wondering how to use that, simply specify your build target to be at least this level (Android 2.2 and above).

However, the core functionality for parsing HTML/XML documents, that has been around since API Level 1, is provided by Document. Although this class contains the getElementsByTagName method, selectNodes appears to be missing. In stead you could use an external libary like dom4j, which does provide selectNodes(String xpathExpression) to evaluate an XPath expression. Just add the dom4j jar to your build path and you should be good to go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top