student.java
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author 作者:
* @date 创建时间2021/6/15 13:51
*/
@Data
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
private String name;
private String number;
private Integer age;
private String gender;
private String city;
public Student(String id, String name, String number, Integer age, String gender, String city) {
this.id = id;
this.name = name;
this.number = number;
this.age = age;
this.gender = gender;
this.city = city;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", number='" + number + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", city='" + city + '\'' +
'}';
}
}
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.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
/**
* @author 作者:
* @date 创建时间2021/6/15 14:04
*/
public class Dom4jEntityWriter {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<>();
stuList.add(new Student("1", "张三", "1001", 21, "男", "湖南"));
stuList.add(new Student("2", "李四", "1002", 20, "男", "甘肃"));
stuList.add(new Student("3", "王五", "1003", 22, "男", "湖南"));
writeXml(stuList, Student.class);
}
/**
* 写入xml
*
* @param stuList 集合数据
* @param elementClass 对应的class
*/
public static void writeXml(ArrayList stuList, Class elementClass) {
//创建一个document
Document document = DocumentHelper.createDocument();
//增加根节点
Element root = document.addElement(elementClass.getSimpleName() + "s");
//循环student节点
for (Object obj : stuList) {
Element stuElement = root.addElement(elementClass.getSimpleName());
try {
//获取Student对象的所有属性
Field[] fields = elementClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
System.out.println("属性名:" + field.getName() + ",值" + field.get(obj));
//给student节点中添加student的子节点
stuElement.addElement(field.getName()).setText(field.get(obj).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
OutputFormat format = OutputFormat.createPrettyPrint();
//将xml写入文件
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("students.xml")), format);
xmlWriter.write(document);
xmlWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成一个Students.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<id>1</id>
<name>张三</name>
<number>1001</number>
<age>21</age>
<gender>男</gender>
<city>湖南</city>
</Student>
<Student>
<id>2</id>
<name>李四</name>
<number>1002</number>
<age>20</age>
<gender>男</gender>
<city>甘肃</city>
</Student>
<Student>
<id>3</id>
<name>王五</name>
<number>1003</number>
<age>22</age>
<gender>男</gender>
<city>湖南</city>
</Student>
</Students>
将xml转换为实体对象
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author 作者:
* @date 创建时间2021/6/15 14:49
* 将xml转换为实体类
*/
public class Dom4jEntityReader {
public static void main(String[] args) {
getDocument(new File("Students.xml"));
}
private static void getDocument(File file) {
try {
ArrayList<Student> list = new ArrayList<>();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
Element rootElement = document.getRootElement();
//枚举所有子节点
for (Iterator i = rootElement.elementIterator(); i.hasNext(); ) {
Element elementStudent = (Element) i.next();
List<Attribute> attributes = elementStudent.attributes();
for (Attribute attribute : attributes) {
System.out.println("属性名:" + attribute.getName() + "--属性值:" + attribute.getValue());
}
Iterator iterator = elementStudent.elementIterator();
Student student = new Student();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
System.out.println("节点名:" + child.getName() + "--节点值:" + child.getStringValue());
//将属性赋给student
if (child.getName().equals("id")) {
student.setId(child.getStringValue());
}
if (child.getName().equals("name")) {
student.setName(child.getStringValue());
}
if (child.getName().equals("number")){
student.setNumber(child.getStringValue());
}
if (child.getName().equals("age")){
student.setAge(Integer.valueOf(child.getStringValue()));
}
if (child.getName().equals("gender")){
student.setGender(child.getStringValue());
}
if (child.getName().equals("city")){
student.setCity(child.getStringValue());
}
}
list.add(student);
}
for (Student student : list) {
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
节点名:id--节点值:1
节点名:name--节点值:张三
节点名:number--节点值:1001
节点名:age--节点值:21
节点名:gender--节点值:男
节点名:city--节点值:湖南
节点名:id--节点值:2
节点名:name--节点值:李四
节点名:number--节点值:1002
节点名:age--节点值:20
节点名:gender--节点值:男
节点名:city--节点值:甘肃
节点名:id--节点值:3
节点名:name--节点值:王五
节点名:number--节点值:1003
节点名:age--节点值:22
节点名:gender--节点值:男
节点名:city--节点值:湖南
Student{id='1', name='张三', number='1001', age=21, gender='男', city='湖南'}
Student{id='2', name='李四', number='1002', age=20, gender='男', city='甘肃'}
Student{id='3', name='王五', number='1003', age=22, gender='男', city='湖南'}