<%@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>
本文地址:http://com.8s8s.com/it/it44104.htm