神秘的 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 月芽兒 閱讀(1021) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 唐海县| 武鸣县| 瑞丽市| 霍州市| 大竹县| 濮阳县| 宝丰县| 城口县| 岑巩县| 宜川县| 铜川市| 日土县| 那曲县| 措美县| 永善县| 金昌市| 安龙县| 新郑市| 南丰县| 博客| 丹棱县| 定陶县| 三河市| 万州区| 莒南县| SHOW| 华容县| 娄烦县| 当阳市| 巴楚县| 琼海市| 安达市| 武城县| 铜梁县| 凯里市| 天气| 永登县| 南皮县| 阳原县| 石家庄市| 新民市|