新聞中心
當(dāng)通過HTTP發(fā)送XML數(shù)據(jù)時(shí),就有必要使用JSP來處理傳入和流出的XML文檔了,比如RSS文檔。作為一個(gè)XML文檔,它僅僅只是一堆文本而已,使用JSP創(chuàng)建XML文檔并不比創(chuàng)建一個(gè)HTML文檔難。

使用JSP發(fā)送XML
使用JSP發(fā)送XML內(nèi)容就和發(fā)送HTML內(nèi)容一樣。唯一的不同就是您需要把頁面的context屬性設(shè)置為text/xml。要設(shè)置context屬性,使用<%@page % >命令,就像這樣:
<%@ page contentType="text/xml" %>
接下來這個(gè)例子向?yàn)g覽器發(fā)送XML內(nèi)容:
<%@ page contentType="text/xml" %>
Padam History
ZARA
100
使用不同的瀏覽器來訪問這個(gè)例子,看看這個(gè)例子所呈現(xiàn)的文檔樹。
在JSP中處理XML
在使用JSP處理XML之前,您需要將與XML 和XPath相關(guān)的兩個(gè)庫文件放在
- XercesImpl.jar:在這下載http://www.apache.org/dist/xerces/j/
- xalan.jar:在這下載http://xml.apache.org/xalan-j/index.html
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:
The title of the first book is:
The price of the second book: 訪問http://localhost:8080/main.jsp,運(yùn)行結(jié)果如下:
BOOKS INFO:
The title of the first book is:Padam History
The price of the second book: 2000
使用JSP格式化XML
這個(gè)是XSLT樣式表style.xsl文件:
這個(gè)是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
運(yùn)行結(jié)果如下:
更多關(guān)于使用JSTL處理XML的內(nèi)容請查閱JSP標(biāo)準(zhǔn)標(biāo)簽庫。
當(dāng)前名稱:創(chuàng)新互聯(lián)JSP教程:JSPXML數(shù)據(jù)處理
文章來源:http://m.fisionsoft.com.cn/article/dppgodg.html


咨詢
建站咨詢
