以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  求sql数据表,导出成xml文件的源代码...万分感谢阿  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=25304)


--  作者:blimy
--  发布时间:12/12/2005 3:39:00 PM

--  求sql数据表,导出成xml文件的源代码...万分感谢阿
最近正在做一个基于xml的课设。
无奈学艺不精,只好再此求助了。

--  作者:nmgyjw
--  发布时间:12/21/2005 8:12:00 AM

--  
希望用什么语言来实现呀!如果没有什么要求的话,我想用DELPHI来帮你实现呀!怎么样,需要请和我联系呀!
--  作者:02492064
--  发布时间:10/12/2006 2:48:00 PM

--  
急需求sql数据和xml文件转换的源代码,希望个位大哥帮帮小弟.
           sinnm111@163.com
--  作者:Prentice2000
--  发布时间:10/13/2006 3:35:00 AM

--  
将关系型数据转化为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();
    }
    
}


--  作者:fanlinux
--  发布时间:10/13/2006 7:13:00 AM

--  
可以用java和C#做,用c#还是比较容易的。
--  作者:wangshucai
--  发布时间:4/24/2007 11:46:00 AM

--  
thank you
--  作者:cloudy
--  发布时间:11/22/2007 10:42:00 PM

--  
想问一下~~用了这段代码,为什么除了sql server原有的model数据库,其它自己建立的数据库导出来都是乱码?谢谢啦~~~~
--  作者:missing62
--  发布时间:4/22/2008 2:48:00 PM

--  
我要顶~~~~
--  作者:caiguoduoyi
--  发布时间:5/11/2008 10:46:00 PM

--  
急需sql与xml的转换代码,谢谢个位大哥大姐了!guoduoyi1987@126.com
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
300.781ms