我想在Android中使用Xpath来解析XML

时间:2010-05-12 07:56:35

标签: xml android parsing xpath

我喜欢使用Xpath来解析XML in java,但是当我在android上做同样的事情时,找不到XPath。

任何想法如何实施。并且如果它不可能那么任何其他的android快速解析器?

由于

1 个答案:

答案 0 :(得分:11)

自Android API级别8(我认为是Android 2.2)以来,Android XPath可用(即作为即用型实现),您可以找到更多信息here

为了帮助您入门 - 在活动范围内尝试:

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

可以通过将ur xml文档放入res / xml文件夹来获取“解析器”(您可能必须自己创建该xml文件夹)。然后您可以通过以下方式访问它:

//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);