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

前端之家收集整理的这篇文章主要介绍了XML+数据库+XSL返回网页处理结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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());
}
}

猜你在找的XML相关文章