看项目中需要实体类转xml,大家是拼接的。感觉可以利用反射实现。于是写了下
代码如下
package com.kevin.util;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
/**
* @description: 实体类转xml
* @author: kevin
* @create: 2020-08-17 08:50
*/
public class EntityToXml {
public static String entityToXml(Object obj) {
StringBuilder xml = new StringBuilder();
xml.append(""<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>");
try {
Class<?> aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String name = field.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
Object o = ObjectUtils.isEmpty(field.get(obj)) ? "" : field.get(obj);
Class<?> curFieldType = field.getType();
xml.append("<").append(name).append(">");
if (curFieldType.equals(List.class)) {
getListXMl(xml, field, (List) o);
} else {
xml.append(o);
}
xml.append("</").append(name).append(">");
}
} catch (IllegalAccessException e) {
//todo 这里记录异常
}
xml.append("</root>");
return xml.toString();
}
private static StringBuilder getListXMl(StringBuilder xml, Field field, List o) throws IllegalAccessException {
Type genericType = field.getGenericType();
if (null == genericType) {
return xml;
}
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
// 得到泛型里的class类型对象
Class<?> actualTypeArgument = (Class<?>) pt.getActualTypeArguments()[0];
List<Object> lists = o;
Field[] declaredFields = actualTypeArgument.getDeclaredFields();
for (Object list : lists) {
xml.append("<row>");
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
String rowName = declaredField.getName();
rowName = rowName.substring(0, 1).toUpperCase() + rowName.substring(1);
Object rowValue = ObjectUtils.isEmpty(declaredField.get(list)) ? "" : declaredField.get(list);
xml.append("<").append(rowName).append(">").append(rowValue).append("</").append(rowName).append(">");
}
xml.append("</row>");
}
}
return xml;
}
}
测试
@Test
public void entityToXml() {
QueryDrugListVO queryDrugListVO = new QueryDrugListVO();
queryDrugListVO.setBlDrugName("a");
queryDrugListVO.setId(1);
QueryDrugListReq req = new QueryDrugListReq();
req.setBigKindName("大佬");
QueryDrugListReq req1 = new QueryDrugListReq();
req1.setBigKindName("大佬1");
List<QueryDrugListReq> reqs = new ArrayList<>();
reqs.add(req);
reqs.add(req1);
queryDrugListVO.setOrders(reqs);
String s = EntityToXml.entityToXml(queryDrugListVO);
System.out.println(s);
}
输出结果:
"<?xml version="1.0" encoding="UTF-8"?><root><Id>1</Id><BlDrugName>a</BlDrugName><SpecValue></SpecValue><MinPackageUnit></MinPackageUnit><Orders><row><FirstKindCode></FirstKindCode><BigKindName>大佬</BigKindName><BigKindState></BigKindState><PageNumber></PageNumber><PageSize></PageSize></row><row><FirstKindCode></FirstKindCode><BigKindName>大佬1</BigKindName><BigKindState></BigKindState><PageNumber></PageNumber><PageSize></PageSize></row></Orders></root>