神秘的 J2ee 殿堂

          ·古之學者必有師·做學者亦要做師者·FIGHTING·

          Dom4j構建、解析XML實例

          package com.hunau.liuyong;

          import java.io.File;
          import java.io.FileWriter;
          import java.io.IOException;

          import org.dom4j.Document;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;
          import org.dom4j.io.OutputFormat;
          import org.dom4j.io.XMLWriter;

          //dom4j官網 (里面有教程) http://dom4j.org/
          public
           class Dom4jCreateXML {
              
          public Document createDocument() {
                  Document document 
          = DocumentHelper.createDocument();
                  Element root 
          = document.addElement( "root" );

                  Element author1 
          = root.addElement( "author" )
                      .addAttribute( 
          "name""月芽兒" )
                      .addAttribute( 
          "location""UK" )
                      .addText( 
          "James Strachan" );
                  
                  Element author2 
          = root.addElement( "author" )
                      .addAttribute( 
          "name""Bob" )
                      .addAttribute( 
          "location""US" )
                      .addText( 
          "Bob McWhirter" );
                        
          return document;
              }
              
              
          public static void main(String[] args) throws Exception{
                  
          //FileWriter out = new FileWriter( "D:/test2.xml" );
                  Dom4jCreateXML djxml=new Dom4jCreateXML();
                  
                  djxml.write(djxml.createDocument());
              }
              
              
              
          public void write(Document document) throws IOException {
                  
                  
          // lets write to a file
                  XMLWriter writer = new XMLWriter(
                      
          new FileWriter( "d:/output.xml" )
                  );
                  writer.write( document );
                  writer.close();
                  
                  
          //Pretty print the document to System.out
                  OutputFormat format = OutputFormat.createPrettyPrint();
                  writer 
          = new XMLWriter( System.out, format );
                  writer.write( document );

                  
                  
          //格式化了XML輸出,看效果,這個有用一些
                  
          //OutputFormat format = OutputFormat.createPrettyPrint();
                  /** 指定XML字符集編碼 */
                  format.setEncoding(
          "GBK");
                  writer 
          = new XMLWriter(new FileWriter(new File("d:/output2.xml")),format);
                  writer.write(document);
                  writer.close();
                  
                  
                  
          //Compact format to System.out
                  format = OutputFormat.createCompactFormat();
                  writer 
          = new XMLWriter( System.out, format );
                  writer.write( document );
              }
              
          }

          posted on 2008-07-11 13:42 月芽兒 閱讀(1018) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 通辽市| 辉南县| 伽师县| 贺兰县| 永德县| 亳州市| 锦屏县| 河间市| 巴林右旗| 广灵县| 天祝| 武胜县| 栾川县| 宜阳县| 大埔区| 西充县| 盘山县| 辰溪县| 微博| 沈丘县| 朝阳区| 荣成市| 小金县| 天长市| 长岛县| 湄潭县| 特克斯县| 合肥市| 莲花县| 吉水县| 永胜县| 昭通市| 双柏县| 沧州市| 精河县| 陕西省| 遵义市| 雷波县| 梁山县| 石楼县| 洱源县|