日期:2014-05-20 浏览次数:20894 次
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class TestXML { private static final Logger log = Logger.getLogger(TestXML.class.getName()); public TestXML(){ super(); } /** * 构造制定格式的xml * @param list * @return */ public Document createXML(List list){ Document document = DocumentHelper.createDocument(); Element students = document.addElement("students");//根节点 students for(int i =0 ;i<list.size() ;i++){ StudentsInforBean bean = new StudentsInforBean(); bean = (StudentsInforBean)list.get(i); Element student = students.addElement("student"); //子节点 student student.addElement("学号").setText(deNull(bean.getId())); student.addElement("姓名").setText(deNull(bean.getName())); student.addElement("年龄").setText(String.valueOf(bean.getAge())); student.addElement("爱好").setText(deNull(bean.getHab())); student.addElement("电话").setText(deNull(bean.getPhone())); } return document; } /** * 生成xml文件 * @param document * @param filename xml文件的完整路径,包括后缀名 * @throws Exception */ public void writeXML(Document document ,String filename) throws Exception{ try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF—8");//这个地方是按你题目要求生成的xml编码格式,生成的xml文件用IE打开会有问题,如果换成gb2312的编码就不会有问题 XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)),format); writer.write(document); writer.close(); } catch (IOException e) { log.error(e.getMessage()); throw new Exception("writeXML Exception:"+e.getMessage()); } } /** * 过滤为null的值 * @param str * @return */ private String deNull(String str) { if (str == null) { return ""; } return str; } public static void main(String args[]) throws Exception{ List list = new ArrayList(); StudentsInforBean bean1 = new StudentsInforBean(); bean1.setId("2006234"); bean1.setName("张威"); bean1.setAge(23); bean1.setHab("电脑,音乐"); bean1.setPhone("3456433"); StudentsInforBean bean2 = new StudentsInforBean(); bean2.setId("2006235"); bean2.setName("刘模"); bean2.setAge(21); bean2.setHab("艺术"); bean2.setPhone("5677644"); StudentsInforBean bean3 = new StudentsInforBean(); bean3.setId("2006236"); bean3.setName("王民"); bean3.setAge(22); bean3.setHab("艺术"); bean3.setPhone("3567677"); list.add(bean1); list.add(bean2); list.add(bean3); //TODO 以上数据的获取,你可以用自己的方法把数据从你对应的数据库表获取 TestXML testXML = new TestXML(); Document document = testXML.createXML(list); testXML.writeXML(document, "d://test.xml"); } } //数据bean public class StudentsInforBean implements java.io.Serializable{ /** * */ private static final long serialVersionUID = 1L; private String id; private String name; private int age; private String hab; private String phone; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHab() { return hab; } public void setHab(String hab) { this.hab = hab; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }