如何用jsp把数据库内容生成xml文件?

wu_lin_326 2006-02-23 09:02:25
表 select_ employees

EMP_ID 名称 JOB_TITLE HIRE_DATE 存储
10 Allan Baker Manager 2000-01-01 1
11 Carol Dan Manager 2000-01-01 2
20 Eric Frank Sales 2000-01-02 1
21 Garry Hans Asst 2000-02-01 1
22 Ivan James Sales 2000-02-01 2
12 Karen Law Manager 2000-06-05 3
23 Mike Nis Sales 2000-06-06 3
把我得mysql数据库select_ employees表通过jsp生成

如下select_employees.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<SQLResult>
...
<EMPLOYEES>

<EMP_ID>21</EMP_ID>
<NAME>Garry Hans</NAME>
<JOB_TITLE>Asst Sales
Rep</JOB_TITLE>
<HIRE_DATE>2000-02-01</HIRE_DATE>
<STORE>1</STORE>
</EMPLOYEES>
<EMPLOYEES>
<EMP_ID>22</EMP_ID>
<NAME>Ivan James</NAME>
<JOB_TITLE>Sales Rep</JOB_TITLE>
<HIRE_DATE>2000-02-01</HIRE_DATE>
<STORE>2</STORE>
</EMPLOYEES>
...
</SQLResult>

请给出代码或相关实例,小弟初学,在线急等,谢谢
...全文
167 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
skycncomp 2006-02-25
招聘
地点北京

qq: 283765999
回复
panzi667 2006-02-24
学习
回复
zx2002027 2006-02-24
学习
回复
lydvqq 2006-02-24
org.jdom.conrib.input包中的ResultSetBuilder方法也可以

Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select EMP_ID,NAME,JOB_TITLE,HIRE_DATE from emp");
ResultSetBuilder builder = new ResultSetBuilder(rs);
Document doc = builder.build();

回复
wu_lin_326 2006-02-23
不会,再顶一下
回复
infowain 2006-02-23
看看这个
http://www.ccw.com.cn/htm/app/aprog/02_1_18_2.asp
回复
你了解几个函数,做出来应该不难。
setNodeText 设置XML节点内容
BulidSendXML,不言而喻
分配xmlOut对象……
算法就是:以根,向下扩展。

回复
panzi667 2006-02-23
up
回复
lydvqq 2006-02-23

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

import java.io.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;

public class DBtoXMLbyJDom{
public static void main(String[] main) throws Exception{
Vector emps = new Employee().getEmps();
XmlOut x = new XmlOut();
Employee e = null;
for(int i=0;i<emps.size();i++){
e = (Employee)emps.get(i);
e.show();
x.addEmpNode(e);
}
}
};

class Employee{
private int empId;
private String name;
private String jobTitle;
private String hireDate;

public void setEmpId(int empId){
this.empId = empId;
}
public int getEmpId(){
return empId ;
}

public void setName(String name){
this.name = name;
}
public String getName(){
return name ;
}
public void setJobTitle(String jobTitle){
this.jobTitle = jobTitle;
}
public String getJobTitle(){
return jobTitle ;
}

public void setHireDate(String hireDate){
this.hireDate = hireDate;
}
public String getHireDate(){
return hireDate ;
}

public Vector getEmps(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&charaterEncoding=utf-8","root","root");
Statement pst = con.createStatement();
ResultSet rs = pst.executeQuery("select * from emp");
Vector emps = new Vector();
while(rs.next()){
Employee e = new Employee();
e.setEmpId(rs.getInt(1));
e.setName(rs.getString(2));
e.setJobTitle(rs.getString(3));
e.setHireDate(rs.getString(4));
emps.addElement(e);
}
return emps;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public void show(){
System.out.print(" "+empId);
System.out.print(" "+name);
System.out.print(" "+jobTitle);
System.out.print(" "+hireDate);
System.out.println("");
}


};

class XmlOut{
Format format = Format.getCompactFormat();
XMLOutputter XMLOut = null;
private Element root = new Element("SQLResult");
Document doc = new Document(root);
private static final String LOG = "emps.xml";
Element employee = null;
XmlOut() throws Exception{
format.setEncoding("gbk");
format.setIndent(" ");
XMLOut = new XMLOutputter(format);
XMLOut.output(doc, new FileOutputStream(LOG));
}

public void addEmpNode(Employee emp) throws Exception
{
employee = new Element("employee");
doc.getRootElement().addContent(employee);

Element empId = new Element("empId");
Element name = new Element("name");
Element jobTitle = new Element("jobTitle");
Element hireDate = new Element("hireDate");
employee.addContent(empId);
empId.addContent(""+emp.getEmpId());
employee.addContent(name);
name.addContent(""+emp.getName());
employee.addContent(jobTitle);
jobTitle.addContent(""+emp.getJobTitle());
employee.addContent(hireDate);
hireDate.addContent(""+emp.getHireDate());

XMLOut.output(doc, new FileOutputStream(LOG));
}

};
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-02-23 09:02
社区公告
暂无公告