加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:24:55 所属栏目:百科 来源:网络整理
导读: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() {
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());
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读