O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.4. Using the XML Core JSTL Tags

Problem

You want to use the JSTL's XML tags in a JSP.

Solution

Use the various XML tags after declaring the tag library with the proper taglib directive (uri attribute of http://java.sun.com/jstl/xml for JSTL 1.0 or http://java.sun.com/jsp/jstl/xml for JSTL 1.1).

Discussion

Many web developers have to write programs that parse or read XML to find information, or they have to write code that displays the encapsulated XML information in a readable format. The JSTL XML tags are a nice tool for these tasks. Example 23-3 displays some information from an Ant build.xml file. (See Chapter 4 on the Ant tool if you are new to Ant.) I'm using this XML file just for an example of how to use the XML-related JSTL tags. Notice that the taglib directives at the top of the page allow the use of the XML and core JSTL tags further along in the code.

Example 23-3. A JSP parses an ant build file

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head><title>Using the Core XML tags</title></head>
<body>
<h2>Here are the target and property values from the XML file</h2>

<c:import url="http://localhost:8080/home/build.xml" var="buildXml" />

<x:parse xml="${buildXml}" var="antDoc" />
 
<h3>First the target names...</h3>
 
<x:forEach select="$antDoc/project/target" >
 
    <x:out select="@name"/> 
    <x:if select="@depends"> : depends=<x:out select="@depends"/></x:if><br />
 
</x:forEach> <h3>Then property names ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required