java通过XPath解析xml节点的代码详解

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java通过XPath解析xml节点的代码详解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.File;
import java.io.FileinputStream;
 
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 org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.nodelist;
 
public class FindElementsByAbsoluteLocationWIThXPath {
 
    public static void main(String[] args) throws Exception {
 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
 
        Document doc = db.parse(new FileInputStream(new File("in.xml")));
 
        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();
 
        // 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() != &#39;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();
 
    }
 
}

Input:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Java Tutorials and Examples 2</title>
        <language>en-us</language>
        <item>
            <title><![CDATA[Java Tutorials 2]]></title>
            <link>http://www.javacodegeeks.COM/</link>
        </item>
        <item>
            <title><![CDATA[Java Examples 2]]></title>
            <link>http://examples.javacodegeeks.com/</link>
        </item>
    </channel>
</rss>

输出:

1. rss
2. rss
3. channel
4. title language item item
5. title title title
6. rss channel language item link item link
7. rss channel item item
8. title link title link

以上就是java通过XPath解析xml节点的代码详解的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的java通过XPath解析xml节点的代码详解全部内容,希望文章能够帮你解决java通过XPath解析xml节点的代码详解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。