想飛就別怕摔

          大爺的并TM罵人

          java中使用Dom4j創建xml文件【轉】

           1 package org.zzn.dom4j;
           2 
           3 import java.io.File;
           4 import java.io.FileWriter;
           5 import java.io.IOException;
           6 
           7 import org.dom4j.Document;
           8 import org.dom4j.DocumentHelper;
           9 import org.dom4j.Element;
          10 import org.dom4j.io.OutputFormat;
          11 import org.dom4j.io.XMLWriter;
          12 
          13 public class XmlDom4J {
          14     public void generateDocument() {
          15         
          16         // 使用 DocumentHelper 類創建一個文檔實例。 DocumentHelper 是生成 XML 文檔節點的 dom4j API
          17         // 工廠類。
          18         Document document = DocumentHelper.createDocument();
          19         
          20         // 使用 addElement() 方法創建根元素 catalog 。 addElement() 用于向 XML 文檔中增加元素。
          21         //<catalog></catalog>  root層的。
          22         Element catalogElement = document.addElement("catalog");
          23         
          24         // 在 catalog 元素中使用 addComment() 方法添加注釋“An XML catalog”。
          25         //<!--An XML catalog--> 添加注釋!
          26         catalogElement.addComment("An XML catalog");
          27         
          28         // 在 catalog 元素中使用 addProcessingInstruction() 方法增加一個處理指令。
          29         catalogElement.addProcessingInstruction("target""text");
          30         
          31         // 在 catalog 元素中使用 addElement() 方法增加 journal 元素。
          32         Element journalElement = catalogElement.addElement("journal");
          33         
          34         // 使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 屬性。
          35         journalElement.addAttribute("title""XML Zone");
          36         journalElement.addAttribute("publisher""IBM developerWorks");
          37         
          38         //向 article 元素中添加 journal 元素。
          39         Element articleElement = journalElement.addElement("article");
          40         
          41         //為 article 元素增加 level 和 date 屬性。
          42         articleElement.addAttribute("level""Intermediate");
          43         articleElement.addAttribute("date""December-2001");
          44         
          45         //向 article 元素中增加 title 元素。
          46         Element titleElement = articleElement.addElement("title");
          47         
          48         //使用 setText() 方法設置 article 元素的文本。
          49         titleElement.setText("Java configuration with XML Schema");
          50         
          51         //在 article 元素中增加 author 元素。
          52         Element authorElement = articleElement.addElement("author");
          53         
          54         //在 author 元素中增加 firstname 元素并設置該元素的文本。
          55         Element firstNameElement = authorElement.addElement("firstname");
          56         firstNameElement.setText("Marcello");
          57         
          58         //在 author 元素中增加 lastname 元素并設置該元素的文本。
          59         Element lastNameElement = authorElement.addElement("lastname");
          60         lastNameElement.setText("Vitaletti");
          61         
          62         //可以使用 addDocType() 方法添加文檔類型說明。
          63 //        document.addDocType("catalog", null, "file://e:/Dtds/catalog.dtd");
          64         
          65         try{
          66             OutputFormat format = OutputFormat.createPrettyPrint();
          67             format.setEncoding("GBK");
          68             XMLWriter output = new XMLWriter(
          69                     new FileWriter( new File("e:/catalog.xml")),format);
          70                 output.write( document );
          71                 output.close();
          72                 }
          73              catch(IOException e){System.out.println(e.getMessage());}
          74         }
          75     
          76         public static void main(String[] argv){
          77         XmlDom4J dom4j=new XmlDom4J();
          78         dom4j.generateDocument();
          79         
          80     }
          81 }
          82 

          運行此程序所需要的包,dom4j.jar

          posted on 2008-12-28 15:04 生命的綻放 閱讀(1273) 評論(0)  編輯  收藏 所屬分類: JAVA

          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 运城市| 连南| 阜平县| 志丹县| 洪洞县| 阿拉善左旗| 怀远县| 景泰县| 齐河县| 屏山县| 青河县| 榆社县| 新和县| 乳源| 张掖市| 墨脱县| 延庆县| 博客| 合作市| 甘孜县| 辽阳市| 永昌县| 石狮市| 枣强县| 鹤山市| 吉林省| 绥江县| 茌平县| 南木林县| 东辽县| 靖边县| 阿拉善盟| 久治县| 铜陵市| 腾冲县| 贞丰县| 广灵县| 肇东市| 武陟县| 青神县| 平南县|