I want to use Xpath in Android to parse XML

2019-02-10 20:58发布

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

1条回答
▲ chillily
2楼-- · 2019-02-10 21:22

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);
查看更多
登录 后发表回答