日期:2014-05-20 浏览次数:20730 次
public void BuildXML() throws Exception { Element root, student, number, name, age; root = new Element("student-info"); // 生成根元素:student-info student = new Element("student"); // 生成元素:student(number,name,age) number = new Element("number"); name = new Element("name"); age = new Element("age"); Document doc = new Document(root); // 将根元素植入文档doc中 number.setText("001"); name.setText("zhang"); age.setText("24"); student.addContent(name); // 先add的name,那么在XML文件中<name></name>出现在前面 student.addContent(number); student.addContent(age); root.addContent(student); Format format = Format.getCompactFormat(); format.setEncoding("UTF-8"); // 设置xml文件的字符为gb2312 format.setIndent(" "); // 设置xml文件的缩进为4个空格 XMLOutputter XMLOut = new XMLOutputter(format);// 元素后换行一层元素缩四格 XMLOut.output(doc, new FileOutputStream("c:\\studentinfo.xml")); }