java(写XML文件):将list中的数据转换成XML数据返回到客户端

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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值