將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è)置語言
                      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)論

          主站蜘蛛池模板: 全州县| 阜新| 江源县| 凤庆县| 库车县| 光山县| 循化| 永福县| 礼泉县| 银川市| 绥化市| 苏尼特左旗| 工布江达县| 乌兰县| 高邑县| 南投市| 宜兰县| 阿合奇县| 安康市| 富蕴县| 县级市| 韶关市| 平原县| 莎车县| 三门峡市| 慈利县| 平湖市| 科尔| 平度市| 拜城县| 鸡东县| 息烽县| 湖南省| 蓝田县| 肇东市| 台州市| 彭州市| 山阳县| 西乡县| 呼图壁县| 漾濞|