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.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
/**
* @author 作者:
* @date 创建时间2021/6/15 15:58
*/
public class XMLUtil {
public static void main(String[] args) {
Student student = new Student("4", "赵六", "1004", 22, "男", "北京市");
//调用方法把对象转xml格式
System.out.println(XMLUtil.getXmlString(student));
//调用getXmlString方法把xml格式重新转换为对象
System.out.println(XMLUtil.getObject(XMLUtil.getXmlString(student), Student.class));
}
/**
* xml字符串转对象
*
* @param xmlString
* @param clazz
* @return
*/
public static Object getObject(String xmlString, Class<?> clazz) {
Document document = null;
try {
document = DocumentHelper.parseText(xmlString);
} catch (DocumentException e) {
e.printStackTrace();
}
return getObject(document, clazz);
}
/**
* xml文档Document转对象
*
* @param document
* @param clazz
* @return
*/
public static Object getObject(Document document, Class<?> clazz) {
Object obj = null;
//获取根节点
Element root = document.getRootElement();
try {
//创建对象
obj = clazz.newInstance();
//获取所有的子节点
List<Element> properties = root.elements();
for (Element pro : properties) {
//获取属性名 首字母大写
String propertyName = pro.getName();
String propertyValue = pro.getText();
Method m;
if (propertyName.equals("Age")) {
m = obj.getClass().getMethod("set" + propertyName, Integer.class);
m.invoke(obj, Integer.parseInt(propertyValue));
} else {
m = obj.getClass().getMethod("set" + propertyName, String.class);
m.invoke(obj, propertyValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
/**
* 对象转xml格式的字符串
*
* @param b
* @return
*/
public static String getXmlString(Object b) {
return getDocument(b).asXML();
}
/**
* 对象转xml文件
*
* @param b
* @return
*/
public static Document getDocument(Object b) {
Document document = DocumentHelper.createDocument();
try {
//创建根节点元素 Student
Element root = document.addElement(b.getClass().getSimpleName());
//获取实体类b的所有属性,返回Field数组
Field[] field = b.getClass().getDeclaredFields();
//遍历所有的属性
for (int i = 0; i < field.length; i++) {
//获取属性的名字
String name = field[i].getName();
//去除串行化序列属性
if (!name.equals("serialVersionUID")) {
//将属性的首字符大写,方便构造get,set方法
name = name.substring(0, 1).toUpperCase() + name.substring(1);
Method m = b.getClass().getMethod("get" + name);
// System.out.println("属性get方法返回值类型:" + m.getReturnType());
//获取属性值
String propertyValue = null;
if (m.getReturnType().getName().equals("java.lang.Integer")) {
Integer pro = (Integer) m.invoke(b);
propertyValue = pro.toString();
} else {
propertyValue = (String) m.invoke(b);
}
Element property = root.addElement(name);
property.setText(propertyValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
}
运行结果:
<?xml version="1.0" encoding="UTF-8"?>
<Student><Id>4</Id><Name>赵六</Name><Number>1004</Number><Age>22</Age><Gender>男</Gender><City>北京市</City></Student>
Student{id='4', name='赵六', number='1004', age=22, gender='男', city='北京市'}