反射---动态的把对象和list转成xml


首先是对象:
UserBean o = new UserBean();
o.setUId(1);
o.setUName("ysj");
o.setUdesc("男的");
list.add(o);
FileUtil.getValue(UserBean.class,o);

public static <T> List<T> getValue(Class<T> t, Object o)
throws InstantiationException, IllegalAccessException,
IntrospectionException, IllegalArgumentException,
InvocationTargetException {
File file = new File("d:\\test\\a.xml");
Field[] fields = t.getDeclaredFields();
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<roots>");
for (Field f : fields) {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(),t);
sb.append("<"+f.getName()+">").append(pd.getReadMethod().invoke(o)).append("</"+f.getName()+">");
// 写数据
pd.getWriteMethod().invoke(t.newInstance(), pd.getReadMethod().invoke(o));

}
sb.append("</roots>");
System.out.println(sb);
return null;
}

只要此方法不管对象怎么变都能写入很方便不用因为传入的对象不同而且重载;

下面的就是list:
List<UserBean> list = new ArrayList<UserBean>();
for(int i=0;i<5;i++){
UserBean o = new UserBean();
o.setUId(i);
o.setUName("ysj"+i);
o.setUdesc("男的"+i);
list.add(o);
}

FileUtil.getValue(UserBean.class,list);


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.NodeList;

public class FileUtil {

public static <T> void setValue(Class<T> t, List<T> list)
throws InstantiationException, IllegalAccessException,
IntrospectionException, IllegalArgumentException,
InvocationTargetException {
File file = new File("d:\\"+ t.getName() +".xml");
Field[] fields = t.getDeclaredFields();
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<roots>");
for(int i=0;i<list.size();i++){
sb.append("<root>");
for (Field f : fields) {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(),t);
sb.append("<"+f.getName()+">").append(pd.getReadMethod().invoke(list.get(i))).append("</"+f.getName()+">");
// 写数据
// pd.getWriteMethod().invoke(t.newInstance(), pd.getReadMethod().invoke(list.get(i)));
}
sb.append("</root>");
}
sb.append("</roots>");
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(sb.toString().getBytes("UTF-8"));
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {

}
}

System.out.println(sb);
}

public static <T> List<T> getValue(Class<T> t){
String filePath = "d:\\"+ t.getName() +".xml";
List<T> list = new ArrayList<T>();
org.w3c.dom.Document document = null;
Field[] fields = t.getDeclaredFields();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(filePath));
if(document == null){
return null;
}
NodeList node = (document).getChildNodes().item(0).getChildNodes();
for(int i = 0;i<node.getLength();i++){
T _t = t.newInstance();
for (Field f : fields) {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(),t);
if(pd.getPropertyType().equals(Integer.TYPE)){

Method method = t.getMethod(pd.getWriteMethod().getName(),int.class);
method.invoke(_t, Integer.parseInt((document).getDocumentElement()
.getElementsByTagName(f.getName()).item(i).getTextContent()));

}else if(pd.getPropertyType().equals(Boolean.TYPE)){

Method method = t.getMethod(pd.getWriteMethod().getName(),boolean.class);
method.invoke(_t, Boolean.parseBoolean((document).getDocumentElement()
.getElementsByTagName(f.getName()).item(i).getTextContent()));

}else{

Method method = t.getMethod(pd.getWriteMethod().getName(),
String.class);
method.invoke(_t, (document).getDocumentElement()
.getElementsByTagName(f.getName()).item(i).getTextContent());
}
}
list.add(_t);
}
} catch (Exception e) {
System.out.println("e-->"+e);
}
return list;
}

}

你可能感兴趣的:(list变成xml,list和xml的转换,对象和xml的转换,对象变成xml)