gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:39 25 4
gpt4 key购买 nike

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

知道如何实现它。如果不可能,还有其他任何快速的 android 解析器吗?

谢谢

最佳答案

Android XPath 从 Android API 级别 8(我认为那是 Android 2.2)开始可用(即作为现成的实现),您可以找到更多信息 here .

为了让您开始 - 在 Activity 范围内尝试:

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

“解析器”可以通过将您的 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);

关于xml - 我想在 Android 中使用 Xpath 来解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2816899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com