溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java如何通過XPath解析xml節(jié)點

發(fā)布時間:2020-07-14 10:31:03 來源:億速云 閱讀:160 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)java如何通過XPath解析xml節(jié)點,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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() != '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

看完上述內(nèi)容,你們對java如何通過XPath解析xml節(jié)點有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI