建立document和root節點
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
doc.setRootElement(root);
在root加入一名為name的子節點,其值為william
Element Type = root.addElement("name").addText("william");
產生檔案
public void createFile(Document doc) throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");//加個編碼可避免掉亂碼的問題
String filename = "F://test123.xml";
FileOutputStream fos = new FileOutputStream(filename);
XMLWriter writer = new XMLWriter(fos, format);
writer.write(doc);
writer.close();
}
沒有留言:
張貼留言