Java dom4j創(chuàng)建解析xml文檔過程解析
DOM4J解析
特征:
1、JDOM的一種智能分支,它合并了許多超出基本XML文檔表示的功能。
2、它使用接口和抽象基本類方法。
3、具有性能優(yōu)異、靈活性好、功能強(qiáng)大和極端易用的特點(diǎn)。
4、是一個開放源碼的文件
jar包:dom4j-1.6.1.jar

創(chuàng)建 book.xml:
package com.example.xml.dom4j;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
* dom4j創(chuàng)建xml文檔示例
*
*/
public class Dom4JTest4 {
public static void main(String[] args) throws Exception {
// 第二種方式:創(chuàng)建文檔并設(shè)置文檔的根元素節(jié)點(diǎn)
Element root2 = DocumentHelper.createElement("bookstore");
Document document2 = DocumentHelper.createDocument(root2);
// 添加一級子節(jié)點(diǎn):add之后就返回這個元素
Element book1 = root2.addElement("book");
book1.addAttribute("id", "1");
book1.addAttribute("name", "第一本書");
// 添加二級子節(jié)點(diǎn)
book1.addElement("name").setText("遇見未知的自己");
book1.addElement("author").setText("張德芬");
book1.addElement("year").setText("2014");
book1.addElement("price").setText("109");
// 添加一級子節(jié)點(diǎn)
Element book2 = root2.addElement("book");
book2.addAttribute("id", "2");
book2.addAttribute("name", "第二本書");
// 添加二級子節(jié)點(diǎn)
book2.addElement("name").setText("雙城記");
book2.addElement("author").setText("狄更斯");
book2.addElement("year").setText("2007");
book2.addElement("price").setText("29");
// 設(shè)置縮進(jìn)為4個空格,并且另起一行為true
OutputFormat format = new OutputFormat(" ", true);
// 另一種輸出方式,記得要調(diào)用flush()方法,否則輸出的文件中顯示空白
XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("book.xml"),format);
xmlWriter3.write(document2);
xmlWriter3.flush();
// close()方法也可以
}
}
運(yùn)行結(jié)果:

解析 book.xml:
package com.example.xml.dom4j;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* dom4j解析xml文檔示例
*
*/
public class Dom4JTest3 {
public static void main(String[] args) {
// 解析books.xml文件
// 創(chuàng)建SAXReader的對象reader
SAXReader reader = new SAXReader();
try {
// 通過reader對象的read方法加載books.xml文件,獲取docuemnt對象。
Document document = reader.read(new File("book.xml"));
// 通過document對象獲取根節(jié)點(diǎn)bookstore
Element bookStore = document.getRootElement();
System.out.println("根節(jié)點(diǎn)名:"+bookStore.getName());
// 通過element對象的elementIterator方法獲取迭代器
Iterator it = bookStore.elementIterator();
// 遍歷迭代器,獲取根節(jié)點(diǎn)中的信息(書籍)
while (it.hasNext()) {
System.out.println("=====開始遍歷子節(jié)點(diǎn)=====");
Element book = (Element) it.next();
System.out.println("子節(jié)點(diǎn)名:"+book.getName());
// 獲取book的屬性名以及 屬性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println("屬性名:" + attr.getName() + "--屬性值:"
+ attr.getValue());
}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
System.out.println("節(jié)點(diǎn)名:" + bookChild.getName() + "--節(jié)點(diǎn)值:" + bookChild.getStringValue());
}
System.out.println("=====結(jié)束遍歷該節(jié)點(diǎn)=====");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用TCP實(shí)現(xiàn)數(shù)據(jù)傳輸實(shí)例詳解
這篇文章主要介紹了Java使用TCP實(shí)現(xiàn)數(shù)據(jù)傳輸實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java 生成隨機(jī)單據(jù)號的實(shí)現(xiàn)示例
本文主要介紹了Java 生成隨機(jī)單據(jù)號的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
解決Jackson反序列化map,set等復(fù)雜類型問題
這篇文章主要介紹了解決Jackson反序列化map,set等復(fù)雜類型問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(14)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
SpringBoot如何使用@RequestBody進(jìn)行數(shù)據(jù)校驗(yàn)
在Web開發(fā)中,前臺向后臺發(fā)送數(shù)據(jù)是非常常見的場景,而在SpringBoot框架中,我們通常使用@RequestBody注解來接收前臺發(fā)送的?JSON數(shù)據(jù),并將其轉(zhuǎn)化為Java對象,本文將介紹如何在?SpringBoot?中使用?@RequestBody?進(jìn)行數(shù)據(jù)校驗(yàn)2023-06-06
Spring框架中一個有用的小組件之Spring Retry組件詳解
Spring Retry 是從 Spring batch 中獨(dú)立出來的一個功能,主要實(shí)現(xiàn)了重試和熔斷,對于那些重試后不會改變結(jié)果,毫無意義的操作,不建議使用重試,今天通過本文給大家介紹Spring Retry組件詳解,感興趣的朋友一起看看吧2021-07-07

