/**
* 生成xml文件
* @version 1.0
* @description
* @author meify 2013-9-3 上午10:43:24
* @throws IOException
* @throws FileNotFoundException
*/
public static void buildXml(List list,String filePath) throws FileNotFoundException, IOException{
Element root=new Element("list");
Document doc=new Document(root);
for(Object obj:list){
Map user=(Map)obj;
Element ele=new Element("user");
//給user節點 添加id屬性
ele.setAttribute("id",user.get("id").toString());
ele.addContent(new Element("name").setText(user.get("name").toString()));
ele.addContent(new Element("age").setText(user.get("age").toString()));
root.addContent(ele);
}
//對輸出格式進行美化--處理縮進
Format format = Format.getPrettyFormat();
//設置編碼防止生成的xml文件出現亂碼
format.setEncoding("utf-8");
XMLOutputter XMLOut = new XMLOutputter(format);
//xml文件生成到指定路徑
XMLOut.output(doc, new FileOutputStream(filePath));
}