加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java通过XPath解析xml节点

发布时间:2020-12-14 23:53:21 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.File;import java.io.FileInputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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,XPathConstants.NODE);
        System.out.println("2. " + node.getNodeName());

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

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

    }

}

<?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 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读