public static void main(String[] args) throws IOException {
List
list.add(new Emp(1,"jacky",22,"男",3000));
list.add(new Emp(2,"tom",24,"男",4000));
list.add(new Emp(3,"kate",28,"女",5000));
list.add(new Emp(4,"boss",32,"男",30000));
/**
* 生成xml步骤:
* 1、创建文档对象Document
* 2、为Document添加根节点
* 3、为根节点组建树桩结构
* 4、创建xmlWriter
* 5、为xmlWriter指定写出目标
* 6、写出xml
*/
Document doc =DocumentHelper.createDocument();
Element root =doc.addElement("list");
for(Emp emp:list){
Element ele = root.addElement("emp");
ele.addElement("name").addText(emp.getName());
ele.addElement("age").addText(emp.getAge()+"");
ele.addElement("gender").addText(emp.getGender());
ele.addElement("salary").addText(emp.getSalary()+"");
ele.addAttribute("id", emp.getId()+"");
}
XMLWriter writer = new XMLWriter();
try {
FileOutputStream fos = new FileOutputStream("myemp.xml");
writer.setOutputStream(fos);
writer.write(doc);
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}