用于简单XML节点字符串的Android XML解析器或库
这是XML文件的一个示例:用于简单XML节点字符串的Android XML解析器或库,android,xml,parsing,Android,Xml,Parsing,这是XML文件的一个示例: <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <p
<?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>
马修·甘巴德拉
XML开发人员指南
电脑类
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
拉尔斯,金
夜雨
幻想
5.95
2000-12-16
一位前建筑师与企业僵尸搏斗,
邪恶的女巫。
科雷茨,伊娃
梅夫上升
幻想
5.95
2000-11-17
在纳米技术崩溃之后
英国的社会。
Si我想在这个文件中搜索许多标准书籍,例如按作者、按流派、按价格等
我将使用XPath查询来实现这一点,那么是否有一些简单的方法可以使用???
例如,我想检查一个作者是否存在,要做到这一点,我必须有一个方法,在这个方法中,我将传递一个XPath查询以将结果返回给我
提前感谢,
致以最良好的祝愿
Ali您可以将DOM API与和结合在一起,如中所述 例如:
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());