JSTL XML Tags

in this tutorial you will learn JSTL XML examples

JSTL XML Tags

JSTL XML Tags

Part 1 | Part 2Part 3 | Part 4

----------------------------------------------

In this third part of the tutorial on JSTL, the author explains the use of xml tags of the JSTL and shows their wonderful simplicity ,ease of use and raw power.

----------------------------------------------

No one can  have any second opinion about the elegance of xml tags in JSTL.If the readers have been following the earlier instalments of this J2EE series of tutorials, they would have come across JAXP,DOM,SAX ,JDOM and such terms, and it may  have been none too easy to learn. But the xml tags in JSTL , make XML processing and even Transformation , a cynch! And ,we now proceed to study them.

Making our study  even easier, many of the xml tags in JSTL , are very much similar to the 'core' tags. For example, just like

<c:out>, we have <x:out>.

Similarly,

<x:forEach>, <x:if>,<x:when>  etc.

So, if we have understood the syntax of the 'core'; tags, it will not be difficult to use the 'xml' tags.

All the following  examples use the books.xml file.It contains 'elements' like 'title' and 'author'..

books.xml

<?xml version="1.0" ?>
<books> 
   <book>
   <title>cobol</title>
   <author>roy</author>
  </book>
  <book>
   <title>java</title>
   <author>herbert</author>
  </book>
  <book>
   <title>c++</title>
   <author>robert</author>
  </book>
  <book>
   <title>coldfusion</title>
   <author>allaire</author>
  </book>
  <book>
   <title>xml unleashed</title>
   <author>morrison</author>
  </book>
  <book>
   <title>jrun</title>
   <author>allaire</author>
  </book>
</books> 

-----------------------------------------------  

demo1

The following program reads the xml file using 'forEach' tag and displays the title and author of   each book..

The syntax:

  <x:forEach   var="n" 

   select="$doc/books/book">

is used to select the elements from the xml file.

  <x:out   select="$n/title"  />

is used to print the elements of the xml file. We begin by   importing the reference to the XML file to be parsed.

<c:import   url="books.xml"  var="url" />

-----

 We have given a symbolic name for this file as 'url'.Next we ask  the program to parse this XML file.The resulting tree is given a symbolic name as 'doc'.

<x:parse   xml="${url}"   var="doc"   />

In the next step, we direct the program to select each title and each author in the XPATH expression $doc/books/book. If we refer to the xml file , we find that the root element of the document is 'books'. Inside this, we have 'book'.So, XPATH can be thought of as just a file hierarchy. Just like <c:out, we have <x:out!

demo1.jsp

<%@ page contentType="text/html" %>  
<%@   taglib  prefix="c" uri="http://java.sun.com/jstl/core" %> 
<%@   taglib  prefix="c" >uri="http://java.sun.com/jstl/xml"   %>  

<html>  
<body>  

 
<c:import  
url="books.xml"  var="url" />  

 
<x:parse   xml="${url}"   var="doc"   />

 -----------------------------------------------<br>

 <x:forEach   var="n" 
  
select="$doc/books/book">
 <x:out   select="$n/title"  />
  <br>  
 <x:out   select="$n/author"  />  
  <br>

 ========  
 
<br>  
 
</x:forEach>
</body>
</html>

  Magically, we have parsed a given XML document and extracted information, without any mention about DOM,SAX and such  words., atall!Wonderful!As a famous author would say, 'anything that makes my job easier, I like!'.

 When we execute  the 'program', we get the following result.

-------------------------------------------

(Result for executing demo1.jsp)

==========================

cobol
roy
==========
java
herbert
==========
c++
robert
==========
coldfusion
allaire
==========
xml unleashed
morrison
==========
jrun
allaire
==========

***********************************************

The following program (demo2)displays the books and authors of xml file  in table format.  

demo2.jsp

<%@ page contentType="text/html" %>  
<%@   taglib   prefix="c" uri="http://java.sun.com/jstl/core" %> 
<%@   taglib  prefix="c" >uri="http://java.sun.com/jstl/xml"   %>  

<html>  
<body>  

 
<c:import  
url="books.xml"  var="url" />  

 
<x:parse   xml="${url}"   var="doc" />  
 
<table  border=1> 
 
<th> 
 
<tr> 
  
<td>title</td> 
  
<td>author</td> 
 
</tr> 
  
</th>

 <x:forEach   var="n" 
  
select="$doc/books/book">

  
<td> 
 
<tr> <x:out   select="$n/title"  /></tr> 

 
<tr> <x:out   select="$n/author"  /></tr> 
 
</td> 
 
</x:forEach> 

</table>  
</body>  
</html>

title

author

cobol

roy

java

herbert

c++

robert

coldfusion

allaire

xml unleashed

morrison

jrun

allaire

------------------------------------------------

demo3 deals with the selection of particular book's author  from the xml file ,when we give the title, with the help of <x:if> action tag.The title is choosen from combo box in demo2.htm file and submitted.We get a display of the selected title and its author.

 Think of this as an sql query like

"select * from table1 where title='jrun'"

--------------------------------------------------

demo3.htm

<html>  
<body> 

  
SELECT THE TITLE.<BR> 
 
YOU WILL GET  TITLE & AUTHOR.  
<form   method=post  action="demo3.jsp">
 

<select name="combo1">
  <option value="xml unleashed">xml
  <option value="c++">c++  
  <option value="coldfusion">cold fusion
  <option value="java">java
  <option  value="cobol">cobol
</select>
<input type=submit>
</form>
</body>
</html>

--------------------------

demo3.jsp  

<%@ page contentType="text/html" %>  
<%@   taglib   prefix="c" uri="http://java.sun.com/jstl/core"  %>  
<%@   taglib  prefix="c" uri="http://java.sun.com/jstl/xml"  %>  

<html>  
<body>  

 
<c:import  
url="books.xml"  var="url" />  

 
<x:parse  xml="${url}"   var="doc"   /> 

 
<c:set  var="s" 
value="${param.combo1}"/>

------

<x:forEach  var="n" select="$doc/books/book" >
<x:if  select="$n/title=$s" >
<x:out   select="$n/title"  />
<br>
<x:out   select="$n/author"  />
<br>
 </x:if>
</x:forEach>
</body>
</html>

Click Here to read more.......