2011-03-26 2 views
1

이 XML 파일의 예를 들면 다음과 같습니다 많은 기준을 책으로안드로이드 XML 파서 또는 라이브러리 간단한 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> 

시 I이 파일에서 검색하려면, 예를 들어 저자, 장르, 가격, 등 ...

나는 이것을하기 위해 XPath 쿼리를 사용할 것이므로, 사용할 간단한 방법이 무엇입니까 ??? 예를 들어, 나는 저자가 존재하는지 확인하고이 일을 내가 나에게 결과를 설정하는 XPath 쿼리를 전달할 수 있습니다하는 방법 ... 사전에

감사가 있어야합니다

http://developer.android.com/reference/javax/xml/xpath/package-summary.html에서 VII로부터 안부,

알리

답변

3

당신은 javax.xml.xpath.XPathFactoryjavax.xml.xpath.XPath와 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());