<%@page contenttype="text/html;charset=gbk"%>
<html>
<head>
<title>用jdom实现数据库到xml的转换</title>
</head>
<body>
<%@page import = "org.jdom.*"%>
<%@page import = "org.jdom.output.*"%>
<%@page import = "java.*"%>
<%@page import = "java.sql.*" %>
<%@page import = "db.*" %>
<%@ page import="java.io.*" %>
<%
string sql = null;
resultset rs = null;
document document=new document(new element("push统计"));//创建文档
int count = 1;
string selement = "用户";
try{
dbgameconnection cn = new dbgameconnection();
sql = "select * from tbl_wappush where length(stuid)>20 order by dpushdate";
rs = cn.executequery(sql);
resultsetmetadata rsmd = rs.getmetadata(); //获取字段名
int numberofcolumns = rsmd.getcolumncount(); //获取字段数
while (rs.next()){
selement = "用户" + count;
element element0=new element(selement); //创建元素 生成jdom树
document.getrootelement().addcontent(element0);
for (int i=1; i<=numberofcolumns;i++)
{
string date=rs.getstring(i); //代码转换
element element=new element(rsmd.getcolumnname(i)).settext(date);
element0.addcontent(element);
}
count++;
}
rs.close();
}catch (sqlexception e){
if(rs!=null)rs.close();
e.printstacktrace();
}
//创建一个statement
xmloutputter outp = new xmloutputter();
outp.output(document, new fileoutputstream("/home/tomcat/webapps/game/data.xml")); //输出xml文档
//out.print("xml 文档生成完毕!");
%>
<a href="http://xlincn/game/data.xml">点击打开产生的xml文档 </a>
</body>
</html>