dom4j写入XML例子
package com.zuxia.dom4j;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class WriteXML {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("class");
Element student = root.addElement("student");
student.addElement("name").addText("王五");
student.addElement("sex").addText("女");
student.addElement("age").addText("20");
//设置输出格式
OutputFormat frm = OutputFormat.createCompactFormat();
frm.setIndentSize(4);
frm.setNewlines(true);
try {
XMLWriter write = new XMLWriter(new FileOutputStream("e:/test.xml"),frm);
write.write(doc);
System.out.println("成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}