Topic: [原创] 从数据库动态生成多重嵌套xml文件

  Print this page

1.[原创] 从数据库动态生成多重嵌套xml文件 Copy to clipboard
Posted by: MN
Posted on: 2003-06-14 22:17

在java中解析xml 文件, 有许多现成的工具(DOM,JDOM,SAX,etc).但是,如何使用java 自动生成XML 文件,尤其是从数据库动态生成xml文件, 却没有现成的class,尤其是,生成多重嵌套的xml文件:
for example:
table A-------1:n---table B

xml file :AtoB.DTD:
<!ELEMENT A.Field1 ( #PCDATA ) >

<!ELEMENT A.Filed2( #PCDATA ) >
<!ELEMENT root( A.Field1,A.Field2,B.Fields+)>
<!ELEMENT B.Fields(B.Field+)>
.
.
.

现有的工具 如XQuery, JAXP, 所生成的包含许多余的数据元数的flat 结构的XML file,

以下的class 将 直接从数据库的查询中 自动生成xml file:

import java.sql.*;
import java.util.*;
import java.io.*;

/**
* Title: tell you something
* Description:
* Copyright: Copyright 2002
* Company: XML Create from dataBase
* @author MN
* @version 1.0
*/

public class CreateXMLFromDB {
Connection conn;
String dataBase;
String userId;
String passWord;

public CreateXMLFromDB(Connection conn, String dataBase, String userId, String passWord,String SQLQuery ) {
this.conn = conn;
this.dataBase = dataBase;
this.userId = userId;
this.passWord = passWord;

try{
Statement stmt = conn.createStatement();
ResultSet rs=null;

rs =stmt.executeQuery(SQLQuery);
while(rs.next()) {
FileOutputStream outXML = null;
String xmlFile = "test.xml";
outXML = new FileOutputStream(xmlFile);
GeneratexmlHeader(outXML,rs,conn);
}
}catch(Exception eopg) {
eopg.printStackTrace();

}

}

public static void GeneratexmlHeader(OutputStream outxml,ResultSet rst,Connection conn ) {

try {
Writer out = new OutputStreamWriter(outxml, "UTF8");
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
out.write("<root>\r\n");
WriteToXML(rst,out);
out.write("</root>\r\n");
out.close();
}
catch (IOException e) {
System.err.println;
}
}

private static void WriteToXML(ResultSet reslt,Writer out){
try{
ResultSetMetaData rsmd=reslt.getMetaData();
String elementname="";
String elementType=null;
String elementData=null;

for(int column=1; column<=rsmd.getColumnCount();column++)
{
elementname=rsmd.getColumnName(column);
elementType=rsmd.getColumnTypeName(column);
elementData=GetDatafromTable(reslt,elementType,elementname);
try{

out.write("<"+elementname+">"+elementData+"</"+elementname+">\r\n");
out.flush();
}
catch(Exception eout){
eout.printStackTrace();
}
}

}
catch(SQLException eee){
eee.printStackTrace();
}

}

private static String GetDatafromTable(ResultSet rs,String elementtype,String elementname){
try{
if(elementtype.equals("LONG")||elementtype.equals("SHORT"))
return(""+rs.getInt(elementname));
else if (elementtype.equals("CHAR")||elementtype.equals("VARCHAR"))
return(rs.getString(elementname));
else if (elementtype.equals("DOUBLE"))
return(""+rs.getDouble(elementname));
else if (elementtype.equals("TIMESTAMP"))
return(""+rs.getTimestamp(elementname));
else if (elementtype.equals("DATE"))
return(""+rs.getDate(elementname));
else if (elementtype.equals("TINY"))
return(""+rs.getBoolean(elementname));
else
return "";
}
catch(SQLException esql){
esql.printStackTrace();
return "";
}
}

}

2.Re:[原创] 从数据库动态生成多重嵌套xml文件 [Re: MN] Copy to clipboard
Posted by: rego
Posted on: 2003-06-15 00:18

JBuilder所带的xmldbms应该能够完成你所说的任务。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923