以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  自己写的db2xml转换程序,与大家交流  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=13049)


--  作者:s02r307
--  发布时间:12/28/2004 1:25:00 AM

--  自己写的db2xml转换程序,与大家交流
写了一个程序,两个功能
1,用jdbc从db中读出数据,再用dom写入到xml文档
2,用dom遍历转换所得的xml文档,写入到数据库中

在access下,测试成功。

存在问题;
本来设计目标是对任意数据库转换为xml,但在转换sql server为xml文档时总出错。
不知什么原因,请教高手指点。

xml结构:
<database database_name="" dbms_name="">
  <table tabla_name="">
     <recorder ...

以下是db2xml的源程序;先谢了!

package dbxml;


import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.swt.widgets.List;
import java.io.FileOutputStream;
import java.sql.*;

public class Db2XML {

 Connection connect; //connect to the database
 Statement sql;
 String[] att,ele;
 int attnum,elenum;

 ResultSet rsc;
 ResultSetMetaData rsmd;
 String attdata,databasename;
 DatabaseMetaData dbmd;
 
 public Db2XML(String dbname,Connection connection) {
  databasename=dbname;
  connect=connection;

 }
 //*******TABLE Tag********
  
 public boolean TableExist(String tname){
  for (int m=0;m<attnum;m++){    
   int index=att[m].indexOf(".");
   if(att[m].substring(0,index).equals(tname))
   {
    return true;   
   }
  }
  for (int n=0;n<elenum;n++){
   int index=ele[n].indexOf(".");
   if(ele[n].substring(0,index).equals(tname))
            {
    return true;
   }
  }
  return false;
 }

 public void WriteTable(String f,List selectatt,List selectele) throws Exception
 {
  att=selectatt.getItems();
  ele=selectele.getItems();
        attnum=selectatt.getItemCount();
        elenum=selectele.getItemCount();
        
        dbmd = connect.getMetaData();
        String SYSTEM_ID = "db.dtd";
        
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  DOMImplementation domImpl = builder.getDOMImplementation();

  ////////////////////////////////////////////
  DocumentType docType = domImpl.createDocumentType("DATABASE",
                null,
                SYSTEM_ID);
  Document dtddocument = domImpl.createDocument("", "DATABASE", docType);

  
  //root
  Document document = domImpl.createDocument("", "DATABASE", null);
  Element root = document.getDocumentElement();
  
  //root
  Attr rootattr1=document.createAttribute("DATABASE_NAME");
  rootattr1.setValue(databasename);
  root.setAttributeNode(rootattr1);
  
  //root DBMS
  Attr rootattr2=document.createAttribute("DBMS_NAME");
  rootattr2.setValue(dbmd.getDatabaseProductName());
  root.setAttributeNode(rootattr2);
  
  try {
   ResultSet rst;
   //DatabaseMetaData dbmd;
   //dbmd = connect.getMetaData();
   String[] type = { "table" };
   String tablename;
   rst = dbmd.getTables(null, null, null, type);

   
   while (rst.next()) {
    tablename = rst.getString("TABLE_NAME");
    if(TableExist(tablename)){
                    //table tag
     Element child1 = document.createElement("TABLE");

     //table tag table
     Attr attr=document.createAttribute("TABLE_NAME");
     attr.setValue(tablename);
     child1.setAttributeNode(attr);
     WriteColumn(document,child1,tablename);
                
     root.appendChild(child1);
     int i=0;
     i++;
     System.out.print(i);
    }
   }
   rst.close();
   
  } catch (Exception e) {
   System.out.println(e.getMessage()+"rst");
  }
  //the foot of tablename    
  //attnum=0;
  TransformerFactory transFactory = TransformerFactory.newInstance();
  Transformer transformer = transFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, SYSTEM_ID);
        //transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, PUBLIC_ID);


  DOMSource source = new DOMSource(document);
  FileOutputStream os = new FileOutputStream(f);
  StreamResult result = new StreamResult(os);
  transformer.transform(source, result);
 }
 public void WriteColumn(Document document,Element child1,String tablename)
 {
  try {
   ResultSet rsc;
   //ResultSetMetaData rsmd;
   sql = connect.createStatement();
   rsc = sql.executeQuery("SELECT * FROM " + tablename);
            
   ResultSet rscinfo;
   rscinfo=dbmd.getColumns(null,null,tablename,"%");

   //****************************************************
   //******TABLE TAG DataType******************
   //****************************************************
   
   //DataType tag
   Element child1_0 = document.createElement("DataType");
   
   //DataType TAG
   while(rscinfo.next()){
    //DataType
    Element child1_0_1 =document.createElement(rscinfo.getString("COLUMN_NAME"));
    
    //DATA_TYPE
    Attr attr1=document.createAttribute("DATA_TYPE");
    attr1.setValue(rscinfo.getString("DATA_TYPE"));
    child1_0_1.setAttributeNode(attr1);
    
                //DATA_TYPE
    Attr attr2=document.createAttribute("TYPE_NAME");
    attr2.setValue(rscinfo.getString("TYPE_NAME"));
    child1_0_1.setAttributeNode(attr2);
    
                //DATA_TYPE
    Attr attr3=document.createAttribute("COLUMN_SIZE");
    attr3.setValue(rscinfo.getString("COLUMN_SIZE"));
    child1_0_1.setAttributeNode(attr3);
    
                //DATA_TYPE
    Attr attr4=document.createAttribute("NULLABLE");
    attr4.setValue(rscinfo.getString("NULLABLE"));
    child1_0_1.setAttributeNode(attr4);
    
                //DATA_TYPE
    //Attr attr5=document.createAttribute("REMARKS");
    //attr5.setValue(rscinfo.getString(2));
    //child1_0_1.setAttributeNode(attr5);
    
    //child1_0_1.appendChild(document.createTextNode(rscinfo.getString(6)+"("+rscinfo.getString("COLUMN_SIZE")+")"));
    child1_0.appendChild(child1_0_1);
   }
   //rscinfo.deleteRow();
  
   rscinfo.close();
   child1.appendChild(child1_0);
   
   //*****************************************************
   //******TABLE TAGNI RECORDER******************
   //*****************************************************
   while (rsc.next()) {
   
   //RECORDER tag
   Element child1_1 = document.createElement("RECORDER");
   
   //*******Recorder************
   for (int m=0;m<attnum;m++){
    int index=att[m].indexOf(".");
    if(att[m].substring(0,index).equals(tablename))
    {
     Attr attr=document.createAttribute(att[m].substring(att[m].indexOf(".")+1));
                    attdata=rsc.getString(att[m].substring(att[m].indexOf(".")+1));
     attr.setValue(attdata);
     child1_1.setAttributeNode(attr);    
    }
   }
   
            //*******Recorder************
   for (int n=0;n<elenum;n++){
    int index=ele[n].indexOf(".");
    if(ele[n].substring(0,index).equals(tablename))
                {
     Element child1_1_1 =document.createElement(ele[n].substring(ele[n].indexOf(".")+1));
         
     //Text data=document.createTextNode(rsc.getString(ele[n].substring(ele[n].indexOf(".")+1)));
        //child1_1_1.setData(rsc.getString(ele[n].substring(ele[n].indexOf(".")+1)));
     child1_1_1.appendChild(document.createTextNode(rsc.getString(ele[n].substring(ele[n].indexOf(".")+1))));
     
     child1_1.appendChild(child1_1_1);                    
    }

   }                        
   child1.appendChild(child1_1);   

      }//the end of while (rsc.next())
   //rsc.deleteRow();
   rsc.close();
   
  }//the end of try
   catch (Exception e) {
   System.out.println(e.getMessage()+"ggg");
  }
 } //the end of public void writecolumn
}


--  作者:lernji
--  发布时间:1/16/2005 3:08:00 PM

--  
我现在遇到的问题是如何将数据库中的数据在已有的DTD文件约束下导出到一个XML文档中,你的这个代码所对应的DTD能否借看一下?我的这个问题应该如何加以解决?请您给予帮助解答,多谢!!!:)
--  作者:canxing
--  发布时间:5/25/2005 9:57:00 AM

--  
请问这个的运行环境是……?
--  作者:wawehi
--  发布时间:5/27/2005 5:47:00 PM

--  
关注作者的进展,这正是我所需要的资料,希望作者能够在做完后放上来供我等菜鸟研究研究~~~~~感激不尽!!!!!
--  作者:wawehi
--  发布时间:5/31/2005 11:52:00 AM

--  
D:\PHPsrc\graduate>javac db2xml.java
db2xml.java:35: package org.eclipse.swt.widgets does not exist
import org.eclipse.swt.widgets.List;
                               ^
db2xml.java:76: cannot resolve symbol
symbol  : class List
location: class dbxml.Db2XML
public void WriteTable(String f,List selectatt,List selectele) throws Exception

                                 ^
db2xml.java:76: cannot resolve symbol
symbol  : class List
location: class dbxml.Db2XML
public void WriteTable(String f,List selectatt,List selectele) throws Exception

                                                ^
db2xml.java:103: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
  rootattr1.setvalue(databasename);
           ^
db2xml.java:108: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
  rootattr2.setvalue(dbmd.getDatabaseProductName());
           ^
db2xml.java:128: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
     attr.setvalue(tablename);
         ^
db2xml.java:183: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr1.setvalue(rscinfo.getString("DATA_TYPE"));
         ^
db2xml.java:188: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr2.setvalue(rscinfo.getString("TYPE_NAME"));
         ^
db2xml.java:193: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr3.setvalue(rscinfo.getString("COLUMN_SIZE"));
         ^
db2xml.java:198: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr4.setvalue(rscinfo.getString("NULLABLE"));
         ^
db2xml.java:229: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
     attr.setvalue(attdata);
         ^
11 errors

以上是作者的代码在我的机器上的测试结果,请问高手,我这个环境还差什么东西?应该怎么搞,我急死了,谢谢!!!!!!!!!!!!!!!!!!!!


--  作者:wawehi
--  发布时间:5/31/2005 10:16:00 PM

--  
db2xml.java:103: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
  rootattr1.setvalue(databasename);
           ^
db2xml.java:108: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
  rootattr2.setvalue(dbmd.getDatabaseProductName());
           ^
db2xml.java:128: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
     attr.setvalue(tablename);
         ^
db2xml.java:183: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr1.setvalue(rscinfo.getString("DATA_TYPE"));
         ^
db2xml.java:188: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr2.setvalue(rscinfo.getString("TYPE_NAME"));
         ^
db2xml.java:193: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr3.setvalue(rscinfo.getString("COLUMN_SIZE"));
         ^
db2xml.java:198: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
    attr4.setvalue(rscinfo.getString("NULLABLE"));
         ^
db2xml.java:229: cannot resolve symbol
symbol  : method setvalue (java.lang.String)
location: interface org.w3c.dom.Attr
     attr.setvalue(attdata);
         ^
8 errors

研究了一下,错误减少了几个,还有8个,搞不定了,我是菜鸟~~~~~作者还是快点来的好啊!!!


--  作者:NewBieInXML
--  发布时间:7/8/2005 10:37:00 AM

--  
这些程序都是在什么环境下运行的?
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
93.750ms