2007年1月8日

          用dom4j建立,修改XML文檔,并解決格式化輸出和中文問題[轉(zhuǎn)]

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

          --------------------------------
          package com.holen.dom4j;

           

          import java.io.File;

          import java.io.FileWriter;

          import java.util.Iterator;

          import java.util.List;

           

          import org.dom4j.Attribute;

          import org.dom4j.Document;

          import org.dom4j.DocumentHelper;

          import org.dom4j.Element;

          import org.dom4j.io.OutputFormat;

          import org.dom4j.io.SAXReader;

          import org.dom4j.io.XMLWriter;

           

          /**

           * 
          @author Holen Chen

           
          */

          public class Dom4jDemo {

             

              
          public Dom4jDemo() {

              }

             

              
          /**

               * 建立一個XML文檔,文檔名由輸入?yún)?shù)決定

               * 
          @param filename 需建立的文件名

               * 
          @return 返回操作結(jié)果, 0表失敗, 1表成功

               
          */

              
          public int createXMLFile(String filename){

                 
          /** 返回操作結(jié)果, 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節(jié)點(diǎn) */

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

                 
          /** 加入show參數(shù)內(nèi)容 */

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

                 
          /** 加入title節(jié)點(diǎn) */

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

                 
          /** 為title設(shè)置內(nèi)容 */

                  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節(jié)點(diǎn) */

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

                  ownerElement.setText(
          "O'Reilly");

                

                 
          try{

                     
          /** 將document中的內(nèi)容寫入文件中 */

                     XMLWriter writer 
          = new XMLWriter(new FileWriter(new File(filename)));

                     writer.write(document);

                     writer.close();

                     
          /** 執(zhí)行成功,需返回1 */

                     returnValue 
          = 1;

                  }
          catch(Exception ex){

                     ex.printStackTrace();

                 }

                        

                  
          return returnValue;

              }

             

              
          /**

               * 修改XML文件中內(nèi)容,并另存為一個新文件

               * 重點(diǎn)掌握dom4j中如何添加節(jié)點(diǎn),修改節(jié)點(diǎn),刪除節(jié)點(diǎn)

               * 
          @param filename 修改對象文件

               * 
          @param newfilename 修改后另存為該文件

               * 
          @return 返回操作結(jié)果, 0表失敗, 1表成功

               
          */

              
          public int ModiXMLFile(String filename,String newfilename){

                 
          int returnValue = 0;

                 
          try{

                     SAXReader saxReader 
          = new SAXReader();   

                     Document document 
          = saxReader.read(new File(filename));

                     
          /** 修改內(nèi)容之一: 如果book節(jié)點(diǎn)中show參數(shù)的內(nèi)容為yes,則修改成no */

                     
          /** 先用xpath查找對象 */

                     List list 
          = document.selectNodes("/books/book/@show" );   

                     Iterator iter 
          = list.iterator();

                     
          while(iter.hasNext()){

                         Attribute attribute 
          = (Attribute)iter.next();

                         
          if(attribute.getValue().equals("yes")){

                             attribute.setValue(
          "no");

                         }  

                     }

                    

                     
          /**

                      * 修改內(nèi)容之二: 把owner項(xiàng)內(nèi)容改為Tshinghua

                      * 并在owner節(jié)點(diǎn)中加入date節(jié)點(diǎn),date節(jié)點(diǎn)的內(nèi)容為2004-09-11,還為date節(jié)點(diǎn)添加一個參數(shù)type

                      
          */

                     list 
          = document.selectNodes("/books/owner" );

                     iter 
          = list.iterator();

                     
          if(iter.hasNext()){

                         Element ownerElement 
          = (Element)iter.next();

                         ownerElement.setText(
          "Tshinghua");

                         Element dateElement 
          = ownerElement.addElement("date");

                         dateElement.setText(
          "2004-09-11");

                         dateElement.addAttribute(
          "type","Gregorian calendar");

                     }

                    

                     
          /** 修改內(nèi)容之三: 若title內(nèi)容為Dom4j Tutorials,則刪除該節(jié)點(diǎn) */

                     list 
          = document.selectNodes("/books/book");

                     iter 
          = list.iterator();

                     
          while(iter.hasNext()){

                         Element bookElement 
          = (Element)iter.next();

                         Iterator iterator 
          = bookElement.elementIterator("title");

                         
          while(iterator.hasNext()){

                             Element titleElement
          =(Element)iterator.next();

                             
          if(titleElement.getText().equals("Dom4j Tutorials")){

                                bookElement.remove(titleElement);

                            }

                         }

                     }          

                    

                     
          try{

                         
          /** 將document中的內(nèi)容寫入文件中 */

                         XMLWriter writer 
          = new XMLWriter(new FileWriter(new File(newfilename)));

                         writer.write(document);

                         writer.close();

                         
          /** 執(zhí)行成功,需返回1 */

                         returnValue 
          = 1;

                     }
          catch(Exception ex){

                         ex.printStackTrace();

                     }

                    

                  }
          catch(Exception ex){

                     ex.printStackTrace();

                 }

                  
          return returnValue;

              }

             

              
          /**

               * 格式化XML文檔,并解決中文問題

               * 
          @param filename

               * 
          @return

               
          */

              
          public int formatXMLFile(String filename){

                 
          int returnValue = 0;

                 
          try{

                     SAXReader saxReader 
          = new SAXReader();   

                     Document document 
          = saxReader.read(new File(filename));

                     XMLWriter output 
          = null;

                     
          /** 格式化輸出,類型IE瀏覽一樣 */

                     OutputFormat format 
          = OutputFormat.createPrettyPrint();

                     
          /** 指定XML字符集編碼 */

                     format.setEncoding(
          "GBK");

                     output 
          = new XMLWriter(new FileWriter(new File(filename)),format);

                     output.write(document);

                     output.close();       

                     
          /** 執(zhí)行成功,需返回1 */

                     returnValue 
          = 1;      

                  }
          catch(Exception ex){

                     ex.printStackTrace();

                 }

                  
          return returnValue;

              }

           

              
          public static void main(String[] args) {

                  Dom4jDemo temp 
          = new Dom4jDemo();

                  System.out.println(temp.createXMLFile(
          "d://holen.xml"));

                  System.out.println(temp.ModiXMLFile(
          "d://holen.xml","d://holen2.xml"));

                  System.out.println(temp.formatXMLFile(
          "d://holen2.xml"));

              }

          }

           

          轉(zhuǎn)自:http://www.donews.net/holen/archive/2004/09/11/96062.aspx
          posted @ 2007-05-11 17:40 Bernie Yoo 閱讀(300) | 評論 (0)編輯 收藏
           

          ?

          ????????Thread?t = new ?Thread() {
          ????????????
          public ? void ?run() {
          ????????????????
          try {
          ????????????????????Thread.sleep(
          100 );
          ????????????????????showWait();
          ?????????????????????
          ?????????????????????
          // your?coding..

          ???????????????}
          ? catch (Exception?ex) {
          ????????????????????javax.swing.JOptionPane.showMessageDialog(
          null , " 發(fā)生錯誤:\n " + ex.getMessage());
          ????????????????????ex.printStackTrace();
          ????????????????????
          return ;
          ????????????????}

          ????????????????stopWait();
          ????????????}

          ????????}
          ;
          ????????t.start();
          posted @ 2007-03-28 23:30 Bernie Yoo 閱讀(289) | 評論 (0)編輯 收藏
           
               摘要: 在使用EMaker時發(fā)現(xiàn)竟然沒有Tree組件可以使用,可又需要靠它來完成一定的功能。在參考了Emaker的文檔后,終于寫了出來,發(fā)在這里與大家共享。  閱讀全文
          posted @ 2007-03-09 18:01 Bernie Yoo 閱讀(1385) | 評論 (0)編輯 收藏
           
               摘要: 總是在網(wǎng)絡(luò)上copy別人的源代碼,今天我也貼出自己今天寫的源碼,相信這個程序會對大家在平時的工作中需要頻繁從數(shù)據(jù)庫中提取數(shù)據(jù)轉(zhuǎn)化成xml文件會有幫助。????最近公司項(xiàng)目中有一件事就是從數(shù)據(jù)庫表中讀出數(shù)據(jù),然后轉(zhuǎn)換成xml文件供客戶端下載,由于數(shù)據(jù)庫中表太多,不可能為單獨(dú)的每個表都寫一個轉(zhuǎn)換程序。于是,經(jīng)過分析,寫了一個通用的用ResultSet對象轉(zhuǎn)換成xml文件的程序。這樣,只需把查詢結(jié)果集(...  閱讀全文
          posted @ 2007-01-08 18:24 Bernie Yoo 閱讀(1058) | 評論 (2)編輯 收藏
           
          主站蜘蛛池模板: 玉田县| 巴塘县| 武宁县| 任丘市| 昭觉县| 景德镇市| 托克托县| 开封县| 沾益县| 金平| 塘沽区| 会昌县| 项城市| 阳春市| 沾益县| 贵港市| 綦江县| 八宿县| 襄汾县| 车致| 新竹市| 广南县| 西充县| 宜城市| 满洲里市| 甘孜| 新兴县| 海林市| 河津市| 广元市| 高碑店市| 陆川县| 汾阳市| 当阳市| 白水县| 乡宁县| 徐州市| 江口县| 通州区| 克东县| 东至县|