XPath测试事例

2 篇文章 0 订阅
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.*;

/**
 * User: herry
 * Date: 15-5-20 20:44
 */
public class TestXPath {
    public static void main(String[] args) throws Exception {
        String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<rss version=\"2.0\">\n" +
                "    <channel>\n" +
                "        <title>Java Tutorials and Examples 2</title>\n" +
                "        <language>en-us</language>\n" +
                "        <item>\n" +
                "            <title><![CDATA[Java Tutorials 2]]></title>\n" +
                "            <link>http://www.javacodegeeks.com/</link>\n" +
                "        </item>\n" +
                "        <item>\n" +
                "            <title><![CDATA[Java Examples 2]]></title>\n" +
                "            <link>http://examples.javacodegeeks.com/</link>\n" +
                "        </item>\n" +
                "    </channel>\n" +
                "</rss>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(testXML.getBytes("UTF-8")));

        XPathFactory factory = XPathFactory.newInstance();

        XPath xpath = factory.newXPath();

        String expression;
        Node node;
        NodeList nodeList;

        // 1. root element
        expression = "/*";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("1. " + node.getNodeName());

        // 2. root element (by name)
        expression = "/rss";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("2. " + node.getNodeName());

        // 3. element under rss
        expression = "/rss/channel";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("3. " + node.getNodeName());

        // 4. all elements under rss/channel
        expression = "/rss/channel/*";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("4. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();


         // 4.1 all elements under rss/channel/title
        expression = "/rss/channel/item";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("4.1 ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node1 = nodeList.item(i);
            System.out.print(node1.getNodeName() + " ");
            /**
             *  //para selects all the para descendants ofthe document root and thus selects all para elements in thesame document as the context node
             * .//para selects the para elementdescendants of the context node
             */
            System.out.println("===="+((Element)xpath.evaluate("./title", node1, XPathConstants.NODE)).getTextContent());
        }
        System.out.println();


        // 5. all title elements in the document
        expression = "//title";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("5. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();

        // 6. all elements in the document except title
        expression = "//*[name() != 'title']";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("6. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();

        // 7. all elements with at least one child element
        expression = "//*[*]";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("7. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();

        // 8. all level-5 elements (the root being at level 1)
        expression = "/*/*/*/*";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("8. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值