DOM4J 代码演示
程序员文章站
2022-04-13 22:02:49
...
package com.openbox.DOM4JDemo;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
//使用JUnit5做单元测试,按序依次执行下面的方法
public class DOM4JTest {
@Test
public void testCreate() throws Exception {
//1.用doucumentHelper类创建一个document对象
Document doc = DocumentHelper.createDocument();
//2.先创建他们的根节点
Element rootEle = doc.addElement("contact");
//3.再创建linkman元素
Element linkmanEle = rootEle.addElement("linkman");
//4.创建linkman元素下,其它所有元素
Element nameEle = linkmanEle.addElement("name");
nameEle.setText("陆小凤");
linkmanEle.addElement("address").setText("中国*");
linkmanEle.addAttribute("id", "8877");
//5.同步操作
//格式化
OutputFormat fOutputFormat = OutputFormat.createPrettyPrint();
FileOutputStream out = new FileOutputStream("res/contact.xml");
//创建XMLWriter 对象
XMLWriter writer = new XMLWriter(out,fOutputFormat);
//将DOM内存对象,写到磁盘文件中
writer.write(doc);
}
@Test
public void testQuery() throws Exception {
File file = new File("res/contact.xml");
//1.NEW一个 SAXRader 对象
SAXReader reader = new SAXReader();
//2.将XML文件读入,生成document对象
Document document = reader.read(file);
//3.取得根元素
Element rootEle = document.getRootElement();
//4.得到linkman元素
List<Element> linkmanList = rootEle.elements("linkman");
//5.遍历所有linkman子元素
for (Element element : linkmanList) {
String id = element.attributeValue("id");
String name = element.elementText("name");
String address = element.elementText("address");
System.out.println("id="+id+",name="+name+",address="+address);
}
}
@Test
public void testAdd() throws Exception {
File file = new File("res/contact.xml");
//1.NEW一个 SAXRader 对象
SAXReader reader = new SAXReader();
//2.将XML文件读入,生成document对象
Document document = reader.read(file);
//3.取得根元素
Element rootEle = document.getRootElement();
//4.增加linkman
Element linkmanEle=rootEle.addElement("linkman").addAttribute("id", "9876");
//5.增加linkman元素下的子元素
linkmanEle.addElement("name").setText("王宝强");
linkmanEle.addElement("address").setText("中国内地");
//6.同步保存
OutputFormat format=OutputFormat.createPrettyPrint();
FileOutputStream out=new FileOutputStream(file);
XMLWriter writer= new XMLWriter(out, format);
writer.write(document);
writer.close();
}
@Test
public void testDelete() throws Exception {
File file = new File("res/contact.xml");
//1.NEW一个 SAXRader 对象
SAXReader reader = new SAXReader();
//2.将XML文件读入,生成document对象
Document document = reader.read(file);
//3.取得根元素
Element rootEle = document.getRootElement();
//4.得到所有linkman元素
List<Element> linkmanList = rootEle.elements("linkman");
//5.删除名字为王宝强的联系人
for (Element element : linkmanList) {
if(element.elementText("name").equals("王宝强")) {
rootEle.remove(element);
//System.out.println("delete "+element.elementText("name"));
}
}
//6.同步保存
OutputFormat format=OutputFormat.createPrettyPrint();
FileOutputStream out=new FileOutputStream(file);
XMLWriter writer= new XMLWriter(out, format);
writer.write(document);
writer.close();
}
}
上一篇: Java 解析XML dom4j
下一篇: 将实体类转化为XML