作者:悠悠做神仙
来源: 恒生LIGHT云社区
简介
首先,我们来看一下百度百科的一个介绍。
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、添加依赖
dom4j
dom4j
1.6.1
2、创建节点并写入文件
首先需要了解几个关键的
api类:
-
Document:表示xml文档信息,是一个树形结构。 -
Eelment:表示xml文件的元素节点,主要是提供一些元素的操作方法。 -
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();
}
}
}
3、效果
![]()
是不是感觉很简单?这次只是分享一下关于xml文件的创建,后续也会分享关于dom4j的其他使用技巧,主要是追加以及解析等。
希望大家能够有所收获,每次一个好用
api分享!