JSP處理XML資料


當通過HTTP傳送XML資料時,使用JSP處理傳入和傳出的XML文件是有意義的; 例如RSS文件。 由於XML文件只是一堆文字,通過JSP建立一個文字文件比建立HTML文件要容易得多。

從JSP傳送XML

可以使用JSP以與傳送HTML相同的方式傳送XML內容。 唯一的區別是必須將頁面的內容型別設定為text/xml。 要設定內容型別,請使用<%@ page%>標籤,如下所示:

<%@ page contentType = "text/xml" %>

以下範例是展示如何將XML內容傳送到瀏覽器 -

<%@ page contentType = "text/xml" %>

<books>
   <book>
      <name>Padam History</name>
      <author>Maxsu</author>
      <price>198</price>
   </book>
</books>

在JSP中處理XML

為了更好地演示,開啟Eclipse建立一個動態Web專案:XmlData,其目錄結構如下 -

在使用JSP進行XML處理之前,需要將以下兩個XML和XPath相關的庫複製到<Tomcat安裝目錄>\lib中,或者複製到{webapp}/WEB-INFO/lib目錄下 -

將以下內容放在books.xml檔案中 -

<books>
   <book>
      <name>Padam History</name>
      <author>Maxsu</author>
      <price>198</price>
   </book>

   <book>
      <name>Great Mistry</name>
      <author>NewWong</author>
      <price>1000</price>
   </book>
</books>

建立一個read_xml.jsp,儲存在Web應用目錄中 -

<%@ 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>JSP+XML</title>
</head>
<body>
    <div style="margin: auto; width: 90%">
        <h3>圖書資訊:</h3>
        <c:import var="bookInfo" url="http://localhost:8080/XmlData/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" />
    </div>
</body>
</html>

在編寫完成上面的程式碼後,部署專案並開啟瀏覽器存取URL:http://localhost:8080/XmlData/read_xml.jsp 存取上述JSP,將顯示以下結果 -

使用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檔案:formatxml.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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用JSP格式化XML</title>
</head>
<body>
<div style="margin:auto;width:90%">
<h3>圖書資訊:</h3>
      <c:set var = "xmltext">
         <books>
            <book>
               <name>Padam History</name>
               <author>Maxsu</author>
               <price>99</price>
            </book>

            <book>
               <name>Great Mistry</name>
               <author>NewWong</author>
               <price>1998</price>
            </book>
         </books>
      </c:set>

      <c:import url = "http://localhost:8080/XmlData/style.xsl" var = "xslt"/>
      <x:transform xml = "${xmltext}" xslt = "${xslt}"/>
</div>
</body>
</html>

在編寫完成上面的程式碼後,部署專案並開啟瀏覽器存取URL:http://localhost:8080/XmlData/formatxml.jsp 存取上述JSP,將顯示以下結果 -

要了解更多有關使用JSTL的XML處理,可以檢視JSP標準庫