通过类反射生成xml文件

时间:2023-01-30 12:00:05
解析xml地址: http://blog.csdn.net/without0815/article/details/45461395

入口测试类:

注:class 有很多方法需看java的api

package priv.yingmm.main;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

import priv.yingmm.entity.Area;

/**
* @Description: xml生成测试类
* @ClassName: ReflexTest
* @author yingmm
* @date 2015-5-2 下午08:53:21
*
*/
public class ReflexTest {

public static void main(String[] args) throws Exception {
/**S 测试数据*/
List<Area> areaList = new ArrayList<Area>();
for (int i = 0; i < 10; i++) {
areaList.add(new Area("33022" + i, "宁波市", 3000));
}
/**E 测试数据*/
/**S 生成xml和写入数据*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String parseStr = ReflexTest.parseStr(areaList);
Document document = db.parse(new ByteArrayInputStream(parseStr.getBytes()));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty("encoding", "utf-8");
DOMSource doms = new DOMSource(document.getDocumentElement());
StreamResult sr = new StreamResult(new FileOutputStream("area.xml"));
t.transform(doms, sr);
/**E 生成xml和写入数据*/


}

/**
* @param 集合拼接转换成字符串
* @author yingmm
* @throws Exception
*/
private static String parseStr(List list) throws Exception {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer();
sb.append("<areas>");
for (int i = 0; i < list.size(); i++) {

@SuppressWarnings("rawtypes")
Class cl = list.get(i).getClass();
Field[] f = cl.getDeclaredFields();
sb.append("<area");
sb.append(" id=\""+ cl.getMethod("getAreaCode", new Class[] {})
.invoke(list.get(i), new Class[] {}) + "\">");
for (int j = 0; j < f.length; j++) {
if ("areaCode".equals(f[j].getName())) {
continue;
}
f[j].setAccessible(true);
sb.append("<" + f[j].getName() + ">");
sb.append(f[j].get(list.get(i)));
sb.append("</" + f[j].getName() + ">");
f[j].setAccessible(false);
}
sb.append("</area>");
}
sb.append("</areas>");
return sb.toString();
}

}
Area.java 类

package priv.yingmm.entity;

/**
* @Description: 区域类
* @ClassName: Area
* @author yingmm
* @date 2015-5-2 下午08:05:53
*
*/
public class Area {

private String areaCode;
private String areaName;
private int personNum;

// private String city;

public Area() {
super();
}

public Area(String areaCode, String areaName, int personNum) {
super();
this.areaCode = areaCode;
this.areaName = areaName;
this.personNum = personNum;
}

public String getAreaCode() {
return areaCode;
}

public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}

public String getAreaName() {
return areaName;
}

public void setAreaName(String areaName) {
this.areaName = areaName;
}

public int getPersonNum() {
return personNum;
}

public void setPersonNum(int personNum) {
this.personNum = personNum;
}

}