How to read XML document using JSTL

Target Audience: Java Web Developers, Web Component Developers, JSP Developers

What you should know already? Java Server Pages (JSP), JSTL

Java Server Pages (JSP) is the standard presentation layer in the J2EE platform. As you know, JSP provides scripting elements and action tags like <jsp:include /> and <jsp:useBean /> those can be used to generate content dynamically. Starting with JSP1.1 developers have been able to create their own actions in the form of custom tag libraries.

The JSP Standard Tag Library (JSTL) is part of JSP1.2 custom tag libraries. JSTL allows the JSP developers to use simple logic and iterative code in the form of tags rather than typical Java code in JSP.

In this article, you will know about how to parse XML document using JSTL XML tags.

The XML

The usage of XML on web is highly increasing since it is well suited for exchanging data among loosely coupled systems. By it’s design XML provides a flexible way of representing data using meaningful tags. For this reason, enterprise companies prefers XML to store semi-important data rather than databases.

Example XML

The following XML (spots_en_US.xml) represents the tourism places of a city.

[xml]
<tourism-spots>
<spot id="tour_home">
<name>The Pondicherry</name>
<img-path>/images/pondicherry-beach.jpeg</img-path>
<about-spot>
<about>Welcome to the peaceful land that is Puducherry.</about>
<about>Pondicherry, now known as Puducherry</about>
</about-spot>
</spot>
<spot id="matrimandir">
<name>Auroville, Matrimandir</name>
<img-path>/images/matrimandir.jpg</img-path>
<about-spot>
<about>Auroville is a universal township</about>
<about>It is made up of 50 communities</about>
</about-spot>
</spot>
</tourism-spots>
[/xml]

Parsing XML using JSTL XML Tag <x:parse>

In JSP file, the XML documents are retrieved using JSTL Core tag <c:import> and stored into a variable which is then use by <x:parse> tag as shown below:

[xml]
<c:import url="/xml/hotspots/spots_en_US.xml" var="spot_doc" />
<x:parse var="docu" xml="${spot_doc}"></x:parse>
[/xml]

The parsed XML document is stored in the variable named docu in the page scope. Now using XPath expressions you can select a particular element from the parsed XML document. See the following code, here the select attribute of <x:set> tag accepts XPath expression which returns zero or more matched elements from the referred XML document. In this example it will return exactly one spot element whose id is matrimandir The matched elements are stored in the variable aSpot.

[xml]
<x:set var="aSpot" select="$docu/tourism-spots/spot[@id=’matrimandir’]"/>
[/xml]

From our example document, now, we can say that the variable aSpot represents the following XML

[xml]
<spot id="matrimandir">
<name>Auroville, Matrimandir</name>
<img-path>/images/matrimandir.jpg</img-path>
<about-spot>
<about>Auroville is a universal township</about>
<about>It is made up of 50 communities</about>
</about-spot>
</spot>
[/xml]

Storing XML document into a variable does not present the data to the user. For that, we have to use typical HTML tags combined with JSTL XML tags, as shown below:

[xml]
<div>
<x:if select="$aSpot/img-path">
<img src="<x:out select="$aSpot/img-path"/>"
alt="<x:out select="$aSpot/name"/>, Pondicherry"
/>
</x:if>
<x:forEach select="$aSpot/about-spot/about" var="about_para">
<p id="about-para">
<x:out select="$about_para" escapeXml="false"/>
</p>
</x:forEach>
</div>
[/xml]
The following JSP shows you the whole code with small change. The change is here it reads the tourism spot id from request scope variable place2see (see line # 6)
[xml]
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<c:import url="xml/hotspots/spots_en_US.xml" var="spot_doc" />
<x:parse var="docu" xml="${spot_doc}"></x:parse>
<x:set var="aSpot" select="$docu/tourism-spots/spot[@id=$requestScope:place2see]"/>
<html>
<head>
<title>Pondicherry Tourism <x:out select="$aSpot/name"/></title>
</head>
<body>
<h1 style="border-bottom: solid 1px #ddd;">
<x:out select="$aSpot/name"/>
</h1>
<div>
<x:if select="$aSpot/img-path">
<img src="<x:out select="$aSpot/img-path"/>"
alt="<x:out select="$aSpot/name"/>, Pondicherry"
/>
</x:if>
<x:forEach select="$aSpot/about-spot/about" var="about_para">
<p id="about-para">
<x:out select="$about_para" escapeXml="false"/>
</p>
</x:forEach>
</div>
</body>
</html>
[/xml]

Feel free to comment this blog. About Author

Permanent link to this article: https://blog.openshell.in/2011/07/how-to-read-xml-document-using-jstl/

Leave a Reply

Your email address will not be published.