Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use Xpath in Android to parse XML

I like to use Xpath to parse XML ins java, but when I am doing the same on android, XPath is not found.

any idea how it can be implemented. and also if its not possible then any other parser for android which is fast?

Thanks

Kai

like image 459
Adil Bhatty Avatar asked May 12 '10 07:05

Adil Bhatty


1 Answers

Android XPath is available (i.e. as a ready-to-use implementation) since Android API Level 8 (I think thats Android 2.2) you can find more information here.

To get you started - within the scope of an activity try:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "myNode";
NodeList nodes = (NodeList) xpath.evaluate(expression, parser, XPathConstants.NODESET);

The "parser" can be obtained by putting ur xml document into your res/xml folder (you might have to create that xml folder yourself). Then you can access it via:

//Do this withon the scope of an activity, you need the activitie's context!
Resources res = getResources();
//Get the xml file
//this is how you access the content of the xml folder 
//you previously created in the res folder
parser = res.getXml(R.xml.yourxmlfile);
like image 92
Ready4Android Avatar answered Oct 23 '22 17:10

Ready4Android