作者:悠悠做神仙
簡介
首先,我們來看一下百度百科的一個介紹。
dom4j是一個Java的XML API,是jdom的更新品,用來讀寫XML檔案的。
dom4j是一個十分優秀的JavaXML API,具有性能優異、功能強大和極其易使用的特點,它的性能超過sun公司官方的dom技術,同時它也是一個開放源代碼的軟體,可以在SourceForge上找到它。
在IBM developerWorks上面還可以找到一篇文章,對主流的Java XML API進行的性能、功能和易用性的評測,是以可以知道dom4j無論在哪個方面都是非常出色的。如今可以看到越來越多的Java軟體都在使用dom4j來讀寫XML,特别值得一提的是連Sun的JAXM也在用dom4j。這已經是必須使用的jar包, Hibernate也用它來讀寫配置檔案。
可見,大家對于dom4j是一個java開發需要了解的一個優秀API。這裡有一個dom4j官網大家也可以進入官網了解更多操作。

下面,我們進入整體,如何取建立xml檔案并寫入一些節點。
1、添加依賴
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
2、建立節點并寫入檔案
首先需要了解幾個關鍵的
api
類:
-
-
表示xml文檔資訊,是一個樹形結構。Document:
-
表示xml檔案的元素節點,主要是提供一些元素的操作方法。Eelment:
-
表示元素結點中的屬性,可以自定義。Attribute:
上代碼:
package com.hundsun.broker;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @author yyzsx
* @version 1.0.0
* @ProjectName 示範項目
* @ClassName Test.java
* @Description TODO
* @createTime 2021年08月22日 22:31:00
*/
public class Test {
public static void main(String[] args) {
saveXml("D:\\test.xml");
}
/**
* @author youyouzuoshenxian
* @description 建立xml并儲存為檔案
* @createDate 2021/8/22 22:45
* @param path
*/
public static void saveXml(String path) {
Document document = DocumentHelper.createDocument();
//建立根元素
Element root = document.addElement("root");
//建立子元素
Element child = root.addElement("child");
//給元素增加屬性
child.addAttribute("Id", "1");
child.addAttribute("Name", "子節點1");
Element prodef = child.addElement("Prodef");
prodef.addAttribute("Id", "1.1");
prodef.addAttribute("Name", "股票");
prodef.addAttribute("Type", "SH|滬A");
Element prodefchild = prodef.addElement("ProdefChild");
prodefchild.addAttribute("Id", "1.1.1");
prodefchild.addAttribute("Name", "散戶");
Element report = prodef.addElement("Report");
report.addAttribute("Id", "34141");
report.addAttribute("Name", "測試報告");
Element reportchild = report.addElement("ReportChild");
reportchild.addAttribute("ID", "1");
reportchild.addAttribute("Name", "測試結果概覽");
reportchild.addAttribute("Text", "測試子產品:斷言調試");
reportchild.addAttribute("Path", "D:\\work_space\\AutoTest_Reports\\index.html");
Element screenshoot = prodef.addElement("ScreenShoot");
screenshoot.addAttribute("ID", "2");
screenshoot.addAttribute("Name", "截圖展示");
Element screenshootchild = screenshoot.addElement("ScreenShootChild");
screenshootchild.addAttribute("ID", "1");
screenshootchild.addAttribute("Name", "開戶資料收取");
screenshootchild.addAttribute("Path", "D:\\work_space\\SootScreens\\資料收取20_02.png");
screenshootchild.addAttribute("ID", "2");
screenshootchild.addAttribute("Name", "大儲存");
screenshootchild.addAttribute("Path", "D:\\work_space\\SootScreens\\登記 11_04.png");
Element frreport = prodef.addElement("FRreport");
frreport.addAttribute("ID", "3");
frreport.addAttribute("Name", "帆軟表單");
Element frreportchild = frreport.addElement("FRreportChild");
frreportchild.addAttribute("ID", "1");
frreportchild.addAttribute("Name", "買入登記");
frreportchild.addAttribute("Path", "D:\\work_space\\2021-08-22 19.pdf");
OutputFormat format = OutputFormat.createPrettyPrint();
//設定xml文檔的編碼為utf-8
format.setEncoding("utf-8");
Writer out;
try {
//建立一個輸出流對象
out = new FileWriter(path);
//建立一個dom4j建立xml的對象
XMLWriter writer = new XMLWriter(out, format);
//調用write方法将doc文檔寫到指定路徑
writer.write(document);
writer.close();
System.out.print("生成XML檔案成功");
} catch (IOException e) {
System.out.print("生成XML檔案失敗");
e.printStackTrace();
}
}
}