JSTL XML <x:parse> 解析xml 的Tag标签

本文介绍如何利用JSTL中的&lt;x:parse&gt;标签来解析XML数据,并通过实例展示了如何读取并解析外部XML文件的内容。该方法适用于需要处理XML数据的Web应用程序。

The <x:parse> tag is used to parse XML data specified either via an attribute or in the tag body.

 

准备所必须的jar 包 以下三个jar包缺一不可:

 

jstl.jar

standard.jar

xalan-2.7.1.jar

 

将这些jar包放在tomcat 服务器器下的 webapps\ project 下的 \WEB-INF\lib 里即可

 


books.xml 和 test.jsp放在同一目录

Attribute:

The <x:parse> tag has following attributes:

AttributeDescription RequiredDefault
varA variable that contains the parsed XML dataNoNone
xmlText of the document to parse (String or Reader)NoBody
systemIdThe system identifier URI for parsing the documentNoNone
filterThe filter to be applied to the source documentNoNone
docXML document to be parsedNoPage
scopeScope of the variable specified in the var attribute.NoPage
varDomA variable that contains the parsed XML data.NoPage
scopeDomScope of the variable specified in the varDom attribute.NoPage

Example:

Following example shows how parse can be used to read external XML file and it can be parsed:

We have seen how we can parse XML from the body of the given document. Now let us put following content in books.xml file:

<?xml version="1.0" encoding="GBK"?>
<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>
 <book number="index1"> 
  <values-list>
	 <values>
		  <value>hello</value>
		  <value>jason</value>
		  <value>2000</value>
		  <value>www.baidu.com</value>
	 </values>
	 <values>
		  <value>hello</value>
		  <value>jason</value>
		  <value>2000</value>
		  <value>www.goole.com</value>
	 </values>
  </values-list>
</book>
</books>

 

 Now try the following test.jsp, keeping in the same directory:

 

<%@ 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="books.xml"/>

<x:parse xml="${bookInfo}" var="rss"/>
<b>The title of the first book is</b>: 
<x:out select="$rss/books/book[1]/name" />
<br>
<b>The price of the second book</b>: 
<x:out select="$rss/books/book[2]/price" />

<br>
	<x:forEach varStatus="status" select="$rss/books/book[@number='index1']/values-list/values">
       <li>
	   <a href="<x:out select="value[4]" escapeXml="false"/>" target="_blank">
		<x:out select="value[4]" />
	   </a>
	   </li>
      </x:forEach>
</body>
</html>

 

Now try to access above JSP using http://localhost:8080/test.jsp, this would produce following result:

Books Info:
The title of the first book is: Padam History 
The price of the second book: 2000 

www.baidu.com 
www.goole.com 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值