gpt4 book ai didi

用于简单 XML 节点字符串的 Android XML 解析器或库

转载 作者:行者123 更新时间:2023-11-29 18:20:57 28 4
gpt4 key购买 nike

这是一个 XML 文件的例子:

  <?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>

<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress.</description>
</book>

<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England.</description>
</book>
</catalog>

Si 我想按许多标准在此文件中搜索书籍,例如按作者、按流派、价格等...

我将使用 XPath 查询来执行此操作,那么有一些简单的方法可以使用吗???例如,我想检查作者是否存在,为此我必须有一个方法,我将在其中传递一个 XPath 查询来为我提供结果...

提前致谢

最好的问候,

阿里

最佳答案

您可以将 DOM API 与 javax.xml.xpath.XPathFactory 结合使用和 javax.xml.xpath.XPathhttp://developer.android.com/reference/javax/xml/xpath/package-summary.html 所述.

例如:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();;
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("input.xml");

// NodeList books = document.getElementsByTagName("book");

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/catalog/book[1]/author"; // first book
Node author = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);

if (author == null)
System.out.println("Element author not exists");
else
System.out.println(author.getTextContent());

关于用于简单 XML 节点字符串的 Android XML 解析器或库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445608/

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