java生成xml文件

/**
	 * 生成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));
	}
	

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章