2013年4月2日 星期二

Java XPath 使用範例

XML 範例books.xml
<?xml version="1.0" encoding="UTF-8"?>
<LIBRARY>
    <BOOKS>
        <BOOK isbn="ABCD7327923">
            <NAME>Java Programing</NAME>
            <SUBJECT>Java J2EE</SUBJECT>
            <AUTHORS>
                <AUTHOR>Tuna TORE</AUTHOR>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>James Gosling</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn="DFGH09093232">
             <NAME>XPATH for Dummies</NAME>
           <SUBJECT> XPATH development</SUBJECT>
            <AUTHORS>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>John Hawking</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn="DSKL2393A">
            <NAME>J2EE Patterns</NAME>
            <SUBJECT>Design Patterns</SUBJECT>
            <AUTHORS>
                <AUTHOR>Aka Tuna</AUTHOR>
            </AUTHORS>
        </BOOK>
    </BOOKS>
    <DVDS>
        <DVD id="123456">
            <NAME>Music DVD</NAME>
            <CONTENT>Music</CONTENT>
            <AUTHORS>
                <AUTHOR>James Gosling</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id="3213324">
            <NAME>Natural Science</NAME>
            <CONTENT>Science</CONTENT>
            <AUTHORS>
                <AUTHOR>John Green</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id="4353534">
            <NAME>Rally</NAME>
            <CONTENT>Race</CONTENT>
            <AUTHORS>
                <AUTHOR>Tuna</AUTHOR>
            </AUTHORS>
        </DVD>
    </DVDS>
</LIBRARY>



程式範例: XpathTest.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class XPathTest {

    public static void main(String[] args) throws Exception {

        //loading the XML document from a file
        DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
        builderfactory.setNamespaceAware(true);

        DocumentBuilder builder = builderfactory.newDocumentBuilder();
        Document xmlDocument = builder.parse("src\\books.xml");

        XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
        XPath xPath = factory.newXPath();

        //getting the name of the book having an isbn number == ABCD7327923
        String isbnNum = "ABCD7327923";
        XPathExpression xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" + isbnNum + "']//NAME");
        String nameOfTheBook = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();

        System.out.println("name of the Book ---> " + nameOfTheBook);

        //getting all authors for the book having an isbn number == ABCD7327923
        xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" + isbnNum + "']//AUTHORS//AUTHOR");

        NodeList nodeListBook = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        System.out.println("Total number of authors for the book " + nameOfTheBook + " is --> "
                + nodeListBook.getLength());

        for (int index = 0; index < nodeListBook.getLength(); index++) {
            String author = nodeListBook.item(index).getTextContent();
            System.out.println("Author " + (index + 1) + " ---> " + author);
        }

        //getting all tags for the dvd having id == 3213324
        String dvdID = "3213324";
        xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='" + dvdID + "']");
        NodeList nodeDVD = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        NodeList nodeListForDVD = nodeDVD.item(0).getChildNodes();

        for (int index = 0; index < nodeDVD.item(0).getChildNodes().getLength(); index++) {
            String tagName = nodeListForDVD.item(index).getNodeName();
            String tagValue = nodeListForDVD.item(index).getTextContent();
            if (!tagName.equals("#text")) //handling empty comment (#text)
                System.out.println(tagName + " value ---> " + tagValue);
        }
    }
}

執行結果:
name of the Book: Java Programing
Total number of authors for the book Java Programing is --> 3
Author 1: Tuna TORE
Author 2: Linus Torvalds
Author 3: James Gosling
NAME value: Natural Science
CONTENT value: Science
AUTHORS value:
                John Green
                Bill Gates

參考文件:http://tunatore.wordpress.com/2011/05/20/how-to-use-xpath-i-java-simple-example/

沒有留言:

張貼留言