- JSP 基础教程
- JSP - 首页
- JSP - 概述
- JSP - 环境搭建
- JSP - 架构
- JSP - 生命周期
- JSP - 语法
- JSP - 指令
- JSP - 动作
- JSP - 隐式对象
- JSP - 客户端请求
- JSP - 服务器响应
- JSP - HTTP 状态码
- JSP - 表单处理
- JSP - 编写过滤器
- JSP - Cookie 处理
- JSP - 会话跟踪
- JSP - 文件上传
- JSP - 日期处理
- JSP - 页面重定向
- JSP - 点击计数器
- JSP - 自动刷新
- JSP - 发送邮件
- 高级 JSP 教程
- JSP - 标准标签库
- JSP - 数据库访问
- JSP - XML 数据
- JSP - Java Bean
- JSP - 自定义标签
- JSP - 表达式语言
- JSP - 异常处理
- JSP - 调试
- JSP - 安全性
- JSP - 国际化
- JSP 有用资源
- JSP - 问答
- JSP - 快速指南
- JSP - 有用资源
- JSP - 讨论
JSP - XML 数据
当您通过 HTTP 发送 XML 数据时,使用 JSP 来处理传入和传出的 XML 文档(例如 RSS 文档)是很有意义的;例如,RSS 文档。由于 XML 文档仅仅是一堆文本,因此通过 JSP 创建 XML 文档比创建 HTML 文档容易得多。
从 JSP 发送 XML
您可以像发送 HTML 一样使用 JSP 发送 XML 内容。唯一的区别在于您必须将页面的内容类型设置为 text/xml。要设置内容类型,请使用<%@page%>标签,如下所示:
<%@ page contentType = "text/xml" %>
以下示例将展示如何将 XML 内容发送到浏览器:
<%@ page contentType = "text/xml" %>
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
</books>
使用不同的浏览器访问上述 XML,查看上述 XML 的文档树表示。
在 JSP 中处理 XML
在继续使用 JSP 处理 XML 之前,您需要将以下两个与 XML 和 XPath 相关的库复制到您的<Tomcat 安装目录>\lib中:
XercesImpl.jar - 从 https://apache.org/dist/xerces/j/ 下载
xalan.jar - 从 https://xml.apache.org/xalan-j/index.html 下载
让我们将以下内容放入 books.xml 文件中:
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
尝试以下main.jsp文件,并将其保存在同一目录下:
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>JSTL x:parse Tags</title>
</head>
<body>
<h3>Books Info:</h3>
<c:import var = "bookInfo" url="https://:8080/books.xml"/>
<x:parse xml = "${bookInfo}" var = "output"/>
<b>The title of the first book is</b>:
<x:out select = "$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>:
<x:out select = "$output/books/book[2]/price" />
</body>
</html>
使用https://:8080/main.jsp访问上述 JSP,将显示以下结果:
Books Info:
The title of the first book is:Padam History The price of the second book: 2000
使用 JSP 格式化 XML
考虑以下 XSLT 样式表style.xsl:
<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0">
<xsl:output method = "html" indent = "yes"/>
<xsl:template match = "/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match = "books">
<table border = "1" width = "100%">
<xsl:for-each select = "book">
<tr>
<td>
<i><xsl:value-of select = "name"/></i>
</td>
<td>
<xsl:value-of select = "author"/>
</td>
<td>
<xsl:value-of select = "price"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
现在考虑以下 JSP 文件:
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>JSTL x:transform Tags</title>
</head>
<body>
<h3>Books Info:</h3>
<c:set var = "xmltext">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</c:set>
<c:import url = "https://:8080/style.xsl" var = "xslt"/>
<x:transform xml = "${xmltext}" xslt = "${xslt}"/>
</body>
</html>
将显示以下结果:
Books Info:
Padam History ZARA 100 Great Mistry NUHA 2000
要了解更多关于使用 JSTL 处理 XML 的信息,您可以查看 JSP 标准标签库。
广告