XML+数据库+XSL返回网页处理结果

15 篇文章 0 订阅
XML+数据库+XSL返回网页处理结果 - lishirong - The CTO of LiShirong
 
xml从数据库取数据:


package business;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import database.DataSource;
import bean.Team;
import org.dom4j.*;

public class TeamBusiness{

public Document allTeamsXml() {
Document document = DocumentHelper.createDocument();
Element rootElm = document.addElement("teaminfo");
DataSource ds = new DataSource();
Connection cn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
cn = ds.getConnection();
String sql = "select id,name,slogan,leader from team";
pst = cn.prepareStatement(sql);
rs = pst.executeQuery();
Element teamsElm = rootElm.addElement("teams");
while(rs.next()) {
Element teamElm = teamsElm.addElement("team");
Element idElm = teamElm.addElement("id");
idElm.setText(rs.getString("id"));
Element nameElm = teamElm.addElement("name");
nameElm.setText(rs.getString("name"));
Element sloganElm = teamElm.addElement("slogan");
sloganElm.setText(rs.getString("slogan"));
Element leaderElm = teamElm.addElement("leader");
leaderElm.setText(rs.getString("leader"));
}
}
catch(Exception e) {
e.printStackTrace();
Element errorElm = rootElm.addElement("error");
errorElm.setText(e.getMessage());
}
finally {
try {
rs.close();
pst.close();
cn.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
return document;
}
}


teaminfo.xsl样式

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<base href="http://localhost:8080/team/"/>
<title>显示小组</title>
</head>
<body>
<xsl:apply-templates select="teaminfo/teams"/>
<xsl:apply-templates select="teaminfo/error"/>
</body>
</html>
</xsl:template>

<xsl:template match="teams">
<div align="center">
<h1>显示小组</h1>
</div>
<div align="center">
<table border="1">
<tr>
<th>ID</th><th>组名</th><th>口号</th><th>组长</th>
<xsl:apply-templates select="team"/>
</tr>
</table>
</div>
</xsl:template>

<xsl:template match="team">
<tr>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="slogan"/></td>
<td><xsl:value-of select="leader"/></td>
<td><a><xsl:attribute name="href">servlet/updateTeam?id=<xsl:value-of select="id"/></xsl:attribute>修改</a></td>
<td><a><xsl:attribute name="href">servlet/deleteTeam?id=<xsl:value-of select="id"/></xsl:attribute>删除</a></td>
</tr>
</xsl:template>

<xsl:template match="error">
<div>
<h1><xsl:value-of/></h1>
</div>
</xsl:template>

</xsl:stylesheet>

通过Servlet返回结果集

package servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.TeamBusiness;
import org.dom4j.*;

public class ViewTeamsXml extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out=response.getWriter();
TeamBusiness teamBusiness=new TeamBusiness();
Document document =teamBusiness.allTeamsXml();
//out.println("<?xml-stylesheet type=\"text/xsl\" href=\"../xsl/teaminfo.xsl\"?>");
out.println(document.getRootElement().asXML());
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值