Khi bạn gửi dữ liệu XML thông qua HTTP, nó sử dụng JSP để xử lý các tài liệu XML đang tới, ví dụ: các tài liệu RSS. Khi một tài liệu XML là một bó văn bản đơn thuần, việc tạo một XML thông qua JSP là không khó khăn hơn việc tạo một tài liệu HTML.

Gửi dữ liệu XML từ một JSP

Bạn có thể gửi XML content bởi sử dụng các JSP theo cùng cách như khi bạn gửi HTML. Điểm khác nhau duy nhất là bạn phải thiết lập contentType của trang thành text/xml. Để thiết lập kiểu nội dung, sử dụng thẻ %@page%, như sau:

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

Dưới đây là ví dụ đơn giản để gửi XML content tới trình duyệt:

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

<books>
   <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
   </book>
</books>

Thử truy cập XML trên bởi sử dụng các trình duyệt khác nhau để xem sự trình bày cây tài liệu như XML trên.

Tiến trình xử lý XML trong JSP

Trước khi bạn tiếp tục xử lý XML bởi sử dụng JSP, bạn cần copy hai thư viện XML và XPath có liên quan sau vào trong <Tomcat Installation Directory>\\lib của bạn:

Chúng ta đặt nội dung sau trong books.xml file:

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

Bây giờ thử main.jsp, giữ chúng trong cùng thư mục:

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

Thử truy cập JSP trên bởi sử dụng http://localhost:8080/main.jsp, nó sẽ cho kết quả sau:

<h3>Books Info:</h3>
<b>The title of the first book is</b>:Padam History
<br></br><b>The price of the second book</b>: 2000

Định dạng XML với JSP

Bạn theo dõi XSLT stylesheet với tên là style.xsl sau:

<?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>

Bây giờ, theo dõi JSP file sau:

<%@ 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="" xslt=""/>

</body>
</html>

Nó sẽ cho kết quả:

Books Info:

Padam HistoryZARA100
Great MistryNUHA2000

Viết câu trả lời

Drop Images

0 Bình luận