示例
以下示例显示如何使用解析来读取外部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 = "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>
上面的代码将产生以下结果-
Books Info:
The title of the first book is:Padam History
The price of the second book: 2000