日期:2014-05-17 浏览次数:20779 次
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>写入XML</title>
</head>
<body>
<form name="form1" method="post" action="OutXML">
<input type="submit" name="button" id="button" value="将数据库数据读为XML文件">
</form>
</body>
</html>
package sjw.pratice;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
@WebServlet("/OutXML")
public class OutXML extends HttpServlet {
private static final long serialVersionUID = 1L;
public OutXML() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
MethodOfSQL.GetConnection();
String demand="select * from contactor";
ResultSet rs=null;
try
{
rs=MethodOfSQL.SelectSQL(demand);
}
catch (Exception e)
{
e.printStackTrace();
}
Document document=DocumentHelper.createDocument();
Element root=document.addElement("contactorlist");
if(rs!=null)
{
try
{
while(rs.next())
{
Element row=root.addElement("contactor");
ResultSetMetaData rsmd=rs.getMetaData();
int columnCount=rsmd.getColumnCount();
for(int i=1;i<=columnCount;i++)
{
Element element = row.addElement(rsmd.getColumnName(i));