JSP - XML数据

当您通过HTTP发送XML数据时,使用JSP处理传入和传出的XML文档是有意义的;例如,RSS文档.由于XML文档只是一堆文本,因此通过JSP创建文档比创建HTML文档容易得多.

从JSP发送XML

您可以使用JSP发送XML内容,就像发送HTML一样.唯一的区别是您必须将页面的内容类型设置为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的文档树表示.

处理XML in JSP

在使用JSP继续进行XML处理之前,您需要将以下两个与XML和XPath相关的库复制到< Tomcat安装目录> \ ltlib :

  • XercesImpl.jar : 从 https://www.apache.org/dist/xerces/j/下载

  • xalan.jar : 从 https://xml.apache.org/xalan-j/index.html

让我们将以下内容放在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="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 访问上述JSP,将显示以下结果 :

Books Info:The title of the first book is:Padam HistoryThe 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 = "http://localhost:8080/style.xsl" var = "xslt"/>
      <x:transform xml = "${xmltext}" xslt = "${xslt}"/>
   </body>
</html>

以下结果将显示 :

Books Info:Padam HistoryZARA100Great MistryNUHA2000

要了解有关使用JSTL进行XML处理的更多信息,可以查看 JSP标准标记库