import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DOM4Write {
public static void main(String[] args) {
Document document = DocumentHelper.createDocument();
Element bookStore = document.addElement("bookstore");
Element book = bookStore.addElement("book");
book.addAttribute("id", "001");
Element name = book.addElement("name");
name.addText("明朝那些事");
Element price = book.addElement("price");
price.setText("100元");
Element type = book.addElement("type");
type.setText("歷史");
Element author = book.addElement("author");
author.setText("安格");
OutputFormat format = OutputFormat.createPrettyPrint();
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
"books.xml")), format);
writer.write(document);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}