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中:

讓我們將以下內容放入 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 標準標籤庫

廣告
© . All rights reserved.