JSTL XML <x:parse> Tag

Advertisements


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

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:

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

Now try the following main.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="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>

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

Books Info:

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


Advertisements
Advertisements