Android的XML解析器或图书馆简单的XML节点串节点、图书馆、简单、Android

由网友(一杯源味白凯水)分享简介:XML文件的这一个示例:This an example of XML file:Gambardella, MatthewXML Developer's Guide 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());
阅读全文

相关推荐

最新文章