public int createXMLFile(String filename) {
int returnValue = 0;
Document document = DocumentHelper.createDocument(); //生成Document,用于管理XML文档
Element booksElement = document.addElement("books"); //添加根节点
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11"); //添加注释
Element bookElement = booksElement.addElement("book"); //在跟节点books下添加一个book子节点
bookElement.addAttribute("show", "yes"); //为bookElement添加属性,并制定属性的值
Element titleElement = bookElement.addElement("title"); //为bookElement节点添加一个title子节点
titleElement.setText("Dom4j Tutorials"); //为title子节点设置文本节点内容
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document); //普通格式,比较不美观
OutputFormat format =null;
// format=OutputFormat.createPrettyPrint(); //美化格式,读取后美化显示
// writer = new XMLWriter( System.out, format );
// writer.write( document );
// format = OutputFormat.createCompactFormat(); //缩减格式,读取后缩减显示
// writer = new XMLWriter( System.out, format );
// writer.write( document );
writer.close();
returnValue = 1;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}