java使用xpath解析xml示例分享
XPath即為XML路徑語(yǔ)言(XML Path Language),它是一種用來(lái)確定XML文檔中某部分位置的語(yǔ)言。XPath基于XML的樹(shù)狀結(jié)構(gòu),提供在數(shù)據(jù)結(jié)構(gòu)樹(shù)中找尋節(jié)點(diǎn)的能力。起初 XPath 的提出的初衷是將其作為一個(gè)通用的、介于XPointer與XSL間的語(yǔ)法模型。但是 XPath 很快的被開(kāi)發(fā)者采用來(lái)當(dāng)作小型查詢(xún)語(yǔ)言。
XPathTest.java
package com.hongyuan.test;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XPathTest {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
// 解析文件,生成document對(duì)象
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.parse(new File("bookstore.xml"));
// 生成XPath對(duì)象
XPath xpath = XPathFactory.newInstance().newXPath();
// 獲取節(jié)點(diǎn)值
String webTitle = (String) xpath.evaluate(
"/bookstore/book[@category='WEB']/title/text()", document,
XPathConstants.STRING);
System.out.println(webTitle);
System.out.println("===========================================================");
// 獲取節(jié)點(diǎn)屬性值
String webTitleLang = (String) xpath.evaluate(
"/bookstore/book[@category='WEB']/title/@lang", document,
XPathConstants.STRING);
System.out.println(webTitleLang);
System.out.println("===========================================================");
// 獲取節(jié)點(diǎn)對(duì)象
Node bookWeb = (Node) xpath.evaluate(
"/bookstore/book[@category='WEB']", document,
XPathConstants.NODE);
System.out.println(bookWeb.getNodeName());
System.out.println("===========================================================");
// 獲取節(jié)點(diǎn)集合
NodeList books = (NodeList) xpath.evaluate("/bookstore/book", document,
XPathConstants.NODESET);
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
System.out.println(xpath.evaluate("@category", book,
XPathConstants.STRING));
}
System.out.println("===========================================================");
}
}
bookstore.xml
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
運(yùn)行效果
相關(guān)文章
Java Hutool 包工具類(lèi)推薦 ExcelUtil詳解
這篇文章主要介紹了Java Hutool 包工具類(lèi)推薦 ExcelUtil詳解,需要引入hutool包,版本號(hào)可根據(jù)實(shí)際情況更換,除hutool包之外,還需要引入操作Excel必要包,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
MyBatis動(dòng)態(tài)SQL與緩存原理深入分析
這篇文章主要介紹了MyBatis動(dòng)態(tài)SQL與緩存原理,Mybatis框架的動(dòng)態(tài)SQL技術(shù)是一種根據(jù)特定條件動(dòng)態(tài)拼裝SQL語(yǔ)句的功能,它存在的意義是為了解決拼接SQL語(yǔ)句字符串時(shí)的痛點(diǎn)問(wèn)題2023-02-02
Spring Boot整合mybatis并自動(dòng)生成mapper和實(shí)體實(shí)例解析
本文是小編給大家總結(jié)的關(guān)于Spring Boot整合mybatis并自動(dòng)生成mapper和實(shí)體的內(nèi)容,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢(xún)及分頁(yè),排序)
這篇文章主要介紹了Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢(xún)及分頁(yè),排序),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

