JSP XML数据处理

当通过HTTP发送XML数据时,就有必要使用JSP来处理传入和流出的XML文档了,比如RSS文档。作为一个XML文档,它仅仅只是一堆文本而已,使用JSP创建XML文档并不比创建一个HTML文档难。

使用JSP发送XML
使用JSP发送XML内容就和发送HTML内容一样。唯一的不同就是您需要把页面的context属性设置为text/xml。要设置context属性,使用<%@page % >命令,就像这样:

<%@ page contentType=“text/xml” %>
接下来这个例子向浏览器发送XML内容:

<%@ page contentType=“text/xml” %>

Padam History ZARA 100 使用不同的浏览器来访问这个例子,看看这个例子所呈现的文档树。

在JSP中处理XML
在使用JSP处理XML之前,您需要将与XML 和XPath相关的两个库文件放在\lib目录下:

XercesImpl.jar:在这下载http://www.apache.org/dist/xerces/j/
xalan.jar:在这下载http://archive.apache.org/dist/xml/xalan-j/
books.xml文件:

Padam History ZARA 100 Great Mistry NUHA 2000 main.jsp文件:

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=“x” uri=“http://java.sun.com/jsp/jstl/xml” %>

JSTL x:parse Tags

Books Info:

<x:parse xml=“ b o o k I n f o &quot; v a r = &quot; o u t p u t &quot; / &gt; &lt; b &gt; T h e t i t l e o f t h e f i r s t b o o k i s &lt; / b &gt; : &lt; x : o u t s e l e c t = &quot; {bookInfo}&quot; var=&quot;output&quot;/&gt; &lt;b&gt;The title of the first book is&lt;/b&gt;: &lt;x:out select=&quot; bookInfo"var="output"/><b>Thetitleofthefirstbookis</b>:<x:outselect="output/books/book[1]/name” />


The price of the second book:
<x:out select="$output/books/book[2]/price" />

访问http://localhost:8080/main.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="/">

<xsl:template match=“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” %>

JSTL x:transform Tags

Books Info:

Padam History ZARA 100 Great Mistry NUHA 2000

<c:import url=“http://localhost:8080/style.xsl” var=“xslt”/>
<x:transform xml=" x m l t e x t &quot; x s l t = &quot; {xmltext}&quot; xslt=&quot; xmltext"xslt="{xslt}"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值