1.读取package com.etoak.test;
import java.io.File;
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;
public class ReadXml {
public static void main(String[] args) {
//使用dom4j读取已经存在的xml文件,如拆快递
try{
//1.拿取解析器Sample Api Xml
SAXReader sax = new SAXReader();
//2.读取指定 的xml文档。将其封装在document对象中
Document doc = sax.read(new File("etoak1.xml"));
//3.拿取根元素<students>
Element root = doc.getRootElement();
//4.拿取所有的一级子元素,集合
List<Element> firstChild = root.elements();
//5.遍历一级子元素集合
for(Element firstEle:firstChild){
//拿取所有一级子元素名
System.out.println("一级子元素名字"+firstEle.getName());
//拿取一级子元素中的属性
List<Attribute> afirstEle = firstEle.attributes();
//遍历所有属性
for(Attribute attr:afirstEle){
System.out.println("拿取属性名是"+attr.getName());
System.out.println("拿取的属性值是"+attr.getValue());
}
//拿取所有的二级子元素
List<Element> secondChild = firstEle.elements();
for(Element secondEle:secondChild){
System.out.println("第二级子元素是"+secondEle.getName());
System.out.println("第二级子元素嵌套的文本是"+secondEle.getText());
}
}
}catch(DocumentException ex){
ex.printStackTrace();
}
}
}
2.写入package com.etoak.test;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class WriteXml {
public static void main(String[] args) throws Exception{
//1.创建一个Document对象
Document doc = DocumentHelper.createDocument();
//2.创建根对象
Element root = doc.addElement("stuList");
//3.创建一个一级子元素
Element student = root.addElement("student");
//4.给一级子元素添加属性值
student.addAttribute("id", "et001");
student.addAttribute("name", "Tom");
//5.给一级子元素添加二级子元素
Element email = student.addElement("email");
email.setText("123@163.com");
Element phone = student.addElement("phone");
phone.setText("111");
//6.设置输出流来生成一个xml文件
OutputStream os = new FileOutputStream("etoak2.xml");
//Format格式输出格式刷
OutputFormat format = OutputFormat.createPrettyPrint();
//设置xml编码
format.setEncoding("utf-8");
//写:传递两个参数一个为输出流表示生成xml文件在哪里
//另一个参数表示设置xml的格式
XMLWriter xw = new XMLWriter(os,format);
//将组合好的xml封装到已经创建好的document对象中,写出真实存在的xml文件中
xw.write(doc);
//清空缓存关闭资源
xw.flush();
xw.close();
}
}
package com.etoak.test;
import java.io.File;
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;
public class ReadXml {
public static void main(String[] args) {
//使用dom4j读取已经存在的xml文件,如拆快递
try{
//1.拿取解析器Sample Api Xml
SAXReader sax = new SAXReader();
//2.读取指定 的xml文档。将其封装在document对象中
Document doc = sax.read(new File("etoak1.xml"));
//3.拿取根元素<students>
Element root = doc.getRootElement();
//4.拿取所有的一级子元素,集合
List<Element> firstChild = root.elements();
//5.遍历一级子元素集合
for(Element firstEle:firstChild){
//拿取所有一级子元素名
System.out.println("一级子元素名字"+firstEle.getName());
//拿取一级子元素中的属性
List<Attribute> afirstEle = firstEle.attributes();
//遍历所有属性
for(Attribute attr:afirstEle){
System.out.println("拿取属性名是"+attr.getName());
System.out.println("拿取的属性值是"+attr.getValue());
}
//拿取所有的二级子元素
List<Element> secondChild = firstEle.elements();
for(Element secondEle:secondChild){
System.out.println("第二级子元素是"+secondEle.getName());
System.out.println("第二级子元素嵌套的文本是"+secondEle.getText());
}
}
}catch(DocumentException ex){
ex.printStackTrace();
}
}
}
package com.etoak.test;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class WriteXml {
public static void main(String[] args) throws Exception{
//1.创建一个Document对象
Document doc = DocumentHelper.createDocument();
//2.创建根对象
Element root = doc.addElement("stuList");
//3.创建一个一级子元素
Element student = root.addElement("student");
//4.给一级子元素添加属性值
student.addAttribute("id", "et001");
student.addAttribute("name", "Tom");
//5.给一级子元素添加二级子元素
Element email = student.addElement("email");
email.setText("123@163.com");
Element phone = student.addElement("phone");
phone.setText("111");
//6.设置输出流来生成一个xml文件
OutputStream os = new FileOutputStream("etoak2.xml");
//Format格式输出格式刷
OutputFormat format = OutputFormat.createPrettyPrint();
//设置xml编码
format.setEncoding("utf-8");
//写:传递两个参数一个为输出流表示生成xml文件在哪里
//另一个参数表示设置xml的格式
XMLWriter xw = new XMLWriter(os,format);
//将组合好的xml封装到已经创建好的document对象中,写出真实存在的xml文件中
xw.write(doc);
//清空缓存关闭资源
xw.flush();
xw.close();
}
}