JSTL - XML <x:parse> 標籤



<x:parse> 標籤用於解析透過屬性或標籤體指定的 XML 資料。

屬性

<x:parse> 標籤具有以下屬性:

屬性 描述 必填 預設值
var 包含已解析 XML 資料的變數
xml 要解析的文件文字 (字串或讀取器) 主體
systemId 用於解析文件的系統識別符號 URI
filter 要應用於源文件的過濾器
doc 要解析的 XML 文件 頁面
scope var 屬性中指定的變數的作用域 頁面
varDom 包含已解析 XML 資料的變數 頁面
scopeDom varDom 屬性中指定的變數的作用域 頁面

示例

以下示例演示如何使用 parse 讀取外部 XML 檔案:

我們已經看到了如何從給定文件的主體解析 XML。現在讓我們將以下內容放入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_standard_tag_library.htm
廣告