dom4j生成xml实例

来源:百度文库 编辑:神马文学网 时间:2024/04/28 23:24:10
import   java.io.File;
import   java.io.FileWriter;
import   org.dom4j.io.XMLWriter;
import   org.dom4j.*;
public   class   CreateDoc{
public   int   createXMLFile(String   filename){
/**   返回操作结果,   0表失败,   1表成功   */
int   returnValue   =   0;
/**   建立document对象   */
Document   document   =DocumentHelper.createDocument();
/**   建立XML文档的根books   */
Element   booksElement   =   document.addElement("books");
/**   加入一行注释   */
booksElement.addComment("This   is   a   test   for   dom4j,   holen,   2004.9.11");
/**   加入第一个book节点   */
Element   bookElement   =   booksElement.addElement("book");
/**   加入show属性内容   */
bookElement.addAttribute("show","yes");
/**   加入title节点   */
Element   titleElement   =   bookElement.addElement("title");
/**   为title设置内容   */
titleElement.setText("Dom4j   Tutorials");
/** 加入author节点*/
Element  authorElement  = booElement.addElement("author");
/** 为author 设置CDTAT */
authorElement.addCDATA("

(.*?)

");
/**   类似的完成后两个book   */
bookElement   =   booksElement.addElement("book");
bookElement.addAttribute("show","yes");
titleElement   =   bookElement.addElement("title");
titleElement.setText("Lucene   Studing");
bookElement   =   booksElement.addElement("book");
bookElement.addAttribute("show","no");
titleElement   =   bookElement.addElement("title");
titleElement.setText("Lucene   in   Action");
/**   加入owner节点   */
Element   ownerElement   =   booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try{
/**   将document中的内容写入文件中   */
XMLWriter   writer   =   new   XMLWriter(new   FileWriter(new   File(filename)));
writer.write(document);
writer.close();
/**   执行成功,需返回1   */
returnValue   =   1;
}catch(Exception   ex){
ex.printStackTrace();
}
return   returnValue;
}
public   static   void   main(String[]args){
CreateDoc   cd=new   CreateDoc();
int   i=cd.createXMLFile("D:\\mybook.xml");
if(i==1){
System.out.println("建立成功!");
}
else{
System.out.println("建立失败!");
}
}
}