您的位置:首页 > 编程语言 > Java开发

dom4j生成xml文件

2016-02-01 11:40 453 查看
dom4j生成xml文件

 

import java.io.File;

import java.io.FileWriter;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.XMLWriter;

 

public class createXml {

 /**

  * @param args

  */

 public static void main(String[] args) {

  // TODO Auto-generated method stub

 /* holen.xml 

     <?xml version="1.0" encoding="UTF-8"?>

    <books>

     <!--This is a test for dom4j, holen, 2004.9.11-->

     <book show="yes">

    <title>Dom4j Tutorials</title>

    </book>

    <book show="yes">

    <title>Lucene Studing</title>

     </book>

    <book show="no">

    <title>Lucene in Action</title>

    </book>

     <owner>O'Reilly</owner>

    </books> */

   createXMLFile();

 }

 public static void createXMLFile(){

  Document document = DocumentHelper.createDocument();

  Element booksElement = document.addElement("books");

  booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");

  Element bookElement = booksElement.addElement("book");

  bookElement.addAttribute("show","yes");

  Element titleElement = bookElement.addElement("title");

  titleElement.setText("Dom4j Tutorials");

  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");

  Element ownerElement = booksElement.addElement("owner");

  ownerElement.setText("O'Reilly");

  try{

   XMLWriter writer = new XMLWriter(new FileWriter(new File("e://jwp.xml")));

   writer.write(document);

   writer.close();

   }catch(Exception ex){

    ex.printStackTrace();

   }

  /*

   建立一个XML文档

      *//**

     * 建立一个XML文档,文档名由输入属性决定

     * @param filename 需建立的文件名

      * @return 返回操作结果, 0表失败, 1表成功

     *//*

     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");

     *//** 类似的完成后两个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;

     } 

     说明:

     Document document = DocumentHelper.createDocument();

     通过这句定义一个XML文档对象。

      Element booksElement = document.addElement("books");

     通过这句定义一个XML 元素,这里添加的是根节点。

     Element有几个重要的方法:

     l addComment:添加注释

      l addAttribute:添加属性

     l addElement:添加子元素

     最后通过XMLWriter生成物 理文件,默认生成的XML文件排版格式比较乱,可以通过OutputFormat类的createCompactFormat()方法或 createPrettyPrint()方法格式化输出,默认采用createCompactFormat()方法,显示比较紧凑,这点将在后面详细谈 到。

     生成后的holen.xml文件内容如下:

     <?xml version="1.0" encoding="UTF-8"?>

     <books><!--This is a test for dom4j, holen, 2004.9.11--><book show="yes"><title>Dom4j Tutorials</title></book><book show="yes"><title>Lucene Studing</title></book><book show="no"><title>Lucene in Action</title></book><owner>O'Reilly</owner></books> 

*/

   return ;

 }

 

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dom4j java xml