Android的XML解析器或图书馆简单的XML节点串 [英] Android XML Parser or Library for Simple XML Node Strings

查看:147
本文介绍了Android的XML解析器或图书馆简单的XML节点串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML文件的这一个示例:

This an example of XML file:

  <?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 I want to search in this file by many criteria books, for example by author, by genre, price, etc...

我会使用XPath查询来做到这一点,那么,有一些简单的方法来使用??? 例如,我要检查,如果一个作家存在,而要做到这一点,我必须有一个在我将传递一个XPath查询把我的功法?

I will use XPath queries to do this, So is there some simple method to use??? For example, I want to check if an author exist and to do this I must have a method in which I'll pass an XPath query to turn me the result...

在此先感谢,

最好的问候,

阿里

推荐答案

您可以将DOM API使用的 javax.xml.xpath.XPathFactory 和的 javax.xml.xpath.XPath中作为descibed在 http://developer.android.com/reference/javax/xml/xpath/package-summary.html

You can combine DOM API with javax.xml.xpath.XPathFactory and javax.xml.xpath.XPath as descibed at http://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());

这篇关于Android的XML解析器或图书馆简单的XML节点串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆