|   
 将关系型数据转化为XML形式  这个程序是根据文章《Querying XML Views of Relational Data》中所提到的Default XML View。 将指定数据库中所有基本表的关系数据输出到一个XML文档中,形成对原来关系数据的Default XML View。我使用Java+JDOM,使用的开发工具是NetBeans 5.0,大家在使用的时候仅需修改数据库名,以及登陆SQL Server 2000的用户名和密码  这是小弟发的第一个帖子请大家多多指教  package XMLViewGenerator;  import org.jdom.*;import org.jdom.output.*;
 import java.sql.*;
 import java.io.*;
 import java.util.*;
  public class DefaultXMLView {
 private java.sql.Connection con = null;
 private final String url = "jdbc:microsoft:sqlserver://";
 private final String serverName= "localhost";
 private final String portNumber = "1433";
 private final String databaseName= "Collage";
 private final String userName = "sa";
 private final String password = "";
 private final String selectMethod = "cursor";
 
 /** Creates a new instance of DefaultXMLView */
 public DefaultXMLView() {
 }
 private String getConnectionUrl() {
 return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";"; }
 private java.sql.Connection getConnection() {
 try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
 if(con!=null)
 System.out.println("成功连接数据库:"+databaseName); } catch(Exception e) {
 e.printStackTrace();
 System.out.println("跟踪在方法getConnection()出现的错误 : " + e.getMessage()); }
 return con; }
 
 private void closeConnection() {
 try {
 if(con!=null)
 con.close();
 con=null; } catch(Exception e){ e.printStackTrace(); } }
 public void GenerateDefaultView() {
 DatabaseMetaData dbMetaData = null;
 ResultSet TableSet = null;
 ResultSet rs = null;
 ResultSetMetaData rmd = null;
 String RootName=databaseName;
 Document XMLDocument = new Document(new Element(RootName)); //创建文档ROOT元素,ROOT元素名即为数据库名
 try{con= this.getConnection();
 if(con!=null) {
 dbMetaData=con.getMetaData();
 ArrayList TableList=new ArrayList();
 TableSet=dbMetaData.getTables(null,null,null,null);//查找出数据库中所有表的名称,包括系统表和用户表
 int ListSize;
 while(TableSet.next()) {
 TableList.add(TableSet.getString("TABLE_NAME"));//将表的名称存入到ArrayList中
 }
 ListSize=TableList.size();
 for(int i=0;i<ListSize;i++) {
 //从ArrayList中依次取出每个表,然后用SELECT查询表中的所有字段
 String SQLInstruction="SELECT * FROM"+" "+TableList.get(i).toString();
 PreparedStatement pstmt = con.prepareStatement(SQLInstruction,
 ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
 //将表名作为元素名
 Element TableElement = new Element(TableList.get(i).toString());
 rs = pstmt.executeQuery();
 rmd = rs.getMetaData();//获取元数据,如属性名等
 int colcount = rmd.getColumnCount();//获取属性列的个数
 while (rs.next()) {
 Element RowElement = new Element("ROW");
 //每一个元组构成一个元素“ROW”,“ROW”中的子元素就是元组中的属性
 //取出元组中的每一个属性名和属性的取值
 //属性名作为元素名,属性的取值作为元素的内容
 for (int j = 1; j <= colcount; j++) {
 Element TempElement=new Element(rmd.getColumnName(j).toString());
 TempElement.setText(rs.getString(j));
 RowElement.addContent(TempElement);
 }
 //将每一个“ROW”元素作为以表名作为元素名元素的子元素
 TableElement.addContent(RowElement);
 }
 //添加到根元素中
 XMLDocument.getRootElement().addContent(TableElement);
 pstmt.close();
 }
 //关闭数据库连接
 rs.close();
 rs = null;
 closeConnection();
 
 XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式化输出,产生缩进和换行
 
 Format format = outp.getFormat();
 format.setEncoding("GB2312"); //设置语言
 format.setExpandEmptyElements(true); //设置输出空元素格式
 outp.setFormat(format);
 
 outp.output(XMLDocument, new FileOutputStream(databaseName+".XML"));//输出XML文档
 System.out.print("XML 文档生成完毕!");
 } else
 System.out.println("错误: 没有可用的数据库连接!");} catch(Exception e){ e.printStackTrace(); }
 }
 public static void main(String[] args) throws Exception {
 DefaultXMLView myDefaultView = new DefaultXMLView();
 myDefaultView.GenerateDefaultView();
 }
 
 }
 |