java(写XML文件):将list中的数据转换成XML数据返回到客户端
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class ObjectToXml {
private final String TITLE = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
private final String ROOT_START = "<objects>";
private final String ROOT_END = "</objects>";
public String getObjectToXml(List<Object> list) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(TITLE);
stringBuffer.append(ROOT_START);
for (int i = 0; i < list.size(); i++) {
Object object = list.get(i);
stringBuffer.append("<" + object.getClass().getSimpleName());
Field[] fields = object.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
try {
String name = fields[j].getName();
Method method = object.getClass().getMethod(
"get" + name.substring(0, 1).toUpperCase()
+ name.substring(1), new Class[] {});
Object result = method.invoke(object, new Object[] {});
stringBuffer.append(" " + name + "=\"" + result + "\"");
} catch (Exception e) {
e.getStackTrace();
}
}
stringBuffer.append(" />");
}
stringBuffer.append(ROOT_END);
return stringBuffer.toString();
}
public boolean witerXmlFile(String filepath,String str){
File file = new File(filepath);
try {
if (!file.exists()) {
//TODO:文件不存在 则创建
file.createNewFile();
}
//BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
//while((str = bufferedReader.readLine())!=null){
// str += str+"\n";
//}
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bufferedWriter.write(str);
//bufferedReader.close();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
ObjectToXml objectToXml = new ObjectToXml();
List<Object> list = new ArrayList<Object>();
SimpleObject object = new SimpleObject();
object.setObjectStr1("xingming");
object.setObjectStr2("mingzi");
object.setObjectStr3("gaoshou");
list.add(object);
object = new SimpleObject();
object.setObjectStr1("xingming");
object.setObjectStr2("mingzi");
object.setObjectStr3("gaoshou");
list.add(object);
object = new SimpleObject();
object.setObjectStr1("xingming");
object.setObjectStr2("mingzi");
object.setObjectStr3("gaoshou");
list.add(object);
String xmlResult = objectToXml.getObjectToXml(list);
System.out.println(xmlResult);
boolean flag = objectToXml.witerXmlFile("c:/xmlResult.xml",xmlResult);
if(flag){
System.out.println("写文件成功!");
}else{
System.out.println("写文件失败!");
}
}
}
class SimpleObject {
private String objectStr1;
private String objectStr2;
private String objectStr3;
public SimpleObject() {
}
public String getObjectStr1() {
return objectStr1;
}
public void setObjectStr1(String objectStr1) {
this.objectStr1 = objectStr1;
}
public String getObjectStr2() {
return objectStr2;
}
public void setObjectStr2(String objectStr2) {
this.objectStr2 = objectStr2;
}
public String getObjectStr3() {
return objectStr3;
}
public void setObjectStr3(String objectStr3) {
this.objectStr3 = objectStr3;
}
}