如何用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>

请给出代码或相关实例,小弟初学,在线急等,谢谢
...全文
205 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
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));
}

};

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧