精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

JSP XML 數據處理

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" %>

<books>
   <book>
      <name>padam history</name>
      <author>zara</author>
      <price>100</price>
   </book>
</books>

使用不同的瀏覽器來訪問這個例子,看看這個例子所呈現的文檔樹。

在jsp中處理xml

在使用jsp處理xml之前,您需要將與xml 和xpath相關的兩個庫文件放在<tomcat installation directory>\lib目錄下:

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文件:

<%@ 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" %>
 
<html>
<head>
  <title>jstl x:parse tags</title>
</head>
<body>
<h3>books info:</h3>
<c:import var="bookinfo" url="http://localhost: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>

訪問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="/">
  <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>

這個是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: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="http://localhost:8080/style.xsl" var="xslt"/>
<x:transform xml="${xmltext}" xslt="${xslt}"/>
 
</body>
</html>

運行結果如下:

更多關于使用jstl處理xml的內容請查閱jsp標準標簽庫

相關文章