將JDBC返回的ResultSet轉(zhuǎn)換成XML

          import org.jdom.*;
          import org.jdom.output.*;
          import java.sql.*;
          import java.io.*;

          public class GenerateXMLView {
              
              
          /** Creates a new instance of GenerateXMLView */
              
          public GenerateXMLView() {
              }
              
              
          public static void main(String[] args) throws Exception {
                  WriteXMLtoFile();
              }
              
              
          public static void WriteXMLtoFile()
              {
                  String driver 
          = "org.apache.derby.jdbc.EmbeddedDriver";
                  String dbName
          ="/derby/demo/databases/toursdb";
                  String connectionURL 
          = "jdbc:derby:" + dbName; 
                  Connection conn 
          = null;
                  Statement st 
          = null;
                  ResultSet rs 
          = null;
                  ResultSetMetaData rmd 
          = null;
                  
                  
          try{
                      Class.forName(driver); 
                  } 
          catch(java.lang.ClassNotFoundException e) {
                      e.printStackTrace();
                  }

                  
          try {
                      conn 
          = DriverManager.getConnection(connectionURL); 
                      st
          =conn.createStatement();
                      rs
          =st.executeQuery("SELECT distinct COUNTRY,CITY_NAME,AIRPORT FROM cities order by COUNTRY,CITY_NAME");
                      rmd 
          = rs.getMetaData();
                      Document document 
          = new Document(new Element("ResultSet")); //創(chuàng)建文檔ROOT元素
                      document.getRootElement().setAttribute("type""sleep");
                      
          int colcount = rmd.getColumnCount();
                      
          while (rs.next()) {
                          Element RowElement 
          = new Element("DataRow");
                          RowElement.setAttribute(
          "size""XXXLarge");
                          
          for (int i = 1; i <= colcount; i++) {
                              Element TempElement
          =new Element(rmd.getColumnName(i).toString().toLowerCase());
                              
          //TempElement.setText(rs.getString(i));
                              TempElement.setAttribute("name",rmd.getColumnName(i).toString().toLowerCase());
                              TempElement.setAttribute(
          "value",rs.getString(i));
                              TempElement.addContent(rs.getString(i));
                              RowElement.addContent(TempElement);
                          }
                          document.getRootElement().addContent(RowElement);
                          Element RowElement2 
          = new Element("ShowRow");
                          RowElement2.setText(
          "Just For Show");
                          document.getRootElement().addContent(RowElement2);
                          Element Contact 
          = new Element("Contact").setText("tel");
                          document.getRootElement().addContent(Contact);
                      }
                      rs.close();
                      st.close();
                      conn.close();
                      XMLOutputter outp 
          = new XMLOutputter(Format.getPrettyFormat()); //格式華輸出,產(chǎn)生縮進(jìn)和換行
                      Format format = outp.getFormat();
                      format.setEncoding(
          "UTF-8"); //設(shè)置語(yǔ)言
                      format.setExpandEmptyElements(true); //設(shè)置輸出空元素為<sample></sample>格式
                      outp.setFormat(format);
                      outp.output(document, 
          new FileOutputStream("C:/ResultSet.xml")); //輸出XML文檔
                      System.out.print("XML 文檔生成完畢!");
                  }  
          catch (Exception e)  {   
                      e.printStackTrace();
                  }
              }
          }

          posted on 2007-08-24 16:06 靈魂守護(hù)者 閱讀(864) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          文章分類(2)

          文章檔案(4)

          收藏夾(9)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 黔南| 桂平市| 黑山县| 江川县| 平乐县| 巴里| 双江| 伊川县| 横山县| 漾濞| 武清区| 宁城县| 桦甸市| 珲春市| 连城县| 漠河县| 肃宁县| 家居| 吉木乃县| 呼伦贝尔市| 淮安市| 阜宁县| 图片| 临高县| 克东县| 玛沁县| 遂平县| 灵武市| 莲花县| 景泰县| 凌源市| 金沙县| 安国市| 云梦县| 嘉黎县| 景东| 观塘区| 新绛县| 克山县| 杭锦后旗| 巢湖市|