【学习】Xpath

1、Xpath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的

2、Xpath http://zh.wikipedia.org/wiki/XPath(wiki)

3、Xpath http://www.w3school.com.cn/xpath/xpath_syntax.asp(W3C语法)

4、结合Java使用XPath的学习:http://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html

4、Xpath Java实例

package com.pachira.d;

import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class Xpath {
    /***
     * <xml>
<inventory id="1">
    <book year="2000">
        <title>Snow Crash</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553380958</isbn>
        <price>14.95</price>
    </book>
    <book year="2005">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <isbn>0743416910</isbn>
        <price>5.99</price>
    </book>
</inventory>
<inventory id="2">
    <book year="1995">
        <title>Zodiac</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553573862</isbn>
        <price>7.50</price>
    </book>
</inventory>
</xml>
     */
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("books.xml");

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        //获得inventory的所有子节点
        XPathExpression expr = xpath.compile("/inventory");
        //获得所有的book节点
        expr = xpath.compile("//book");
        //获得inventory下的属性year为2000的book节点(绝对路径)
        expr = xpath.compile("/xml/inventory/book[@year='2000']");
        //获得属性year为2000的book节点的isbn节点(简洁表示)
        expr = xpath.compile("//book[@year='2000']/isbn");
        //获得属性year为2000的book节点的isbn节点(啰嗦表示)
        expr = xpath.compile("//book[@year='2000']/child::isbn");
        //获得含有isbn节点的父节点的属性year为2000的book节点
        expr = xpath.compile("//isbn/parent::book[@year='2000']");
        //获得price大于6的book节点
        expr = xpath.compile("//price[price>6.0]");
        //获得price大于7的book节点的父节点信息
        expr = xpath.compile("//book[price>7]/..");
        //获得price大于7的book节点的price孩子节点
        expr = xpath.compile("//book[price>7]/child::price");
        //获得price大于7的book节点的所有孩子节点
        expr = xpath.compile("//book[price>7]/child::*");
        //获得price大于7的book节点父节点的id=2的inventory节点
        expr = xpath.compile("//book[price>7]/parent::inventory[@id=2]");
        
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        System.out.println(nodes.getLength());
        for (int i = 0; i < nodes.getLength(); i++) {
            for (int j = 0; j < nodes.item(i).getChildNodes().getLength(); j++) {
                NodeList children = nodes.item(i).getChildNodes();
                String nodeName = children.item(j).getNodeName();
                String val = children.item(j).getTextContent().trim();
                System.out.println(nodeName + "\t" + val);
            }
            System.out.println("=========================");
        }
    }
}

 

posted on 2014-12-10 14:28  有个姑娘叫小芳  阅读(199)  评论(0编辑  收藏  举报