No one can have any second opinion about the elegance of xml tags in JSTL. If the readers have been following the earlier installments of this J2EE series of tutorials, they would have come across JAXP,DOM,SAX ,JDOM and such terms, and it mayhave been none too easy to learn. But the xml tags in JSTL , make XML processing and even Transformation , a cinch! And ,we now proceed to study them.
also read:
- Java EE Tutorials
- Java EE 7 Tutorials
- Java EE Interview Questions
Making our studyeven 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 followingexamples use the books.xmlfile.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 ofeach book. The syntax:
<x:forEachvar=”n” select=”$doc/books/book”>
is used to select the elements from the xml file.
<x:outselect=”$n/title”/>
is used to print the elements of the xml file. We begin byimporting the reference to the XML file to be parsed.
<c:importurl=”books.xml”var=”url” />
We have given a symbolic name for this file as ‘url’.Next we askthe program to parse this XML file.The resulting tree is given a symbolic name as ‘doc’.
<x:parsexml=”${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" %> <%@taglibprefix="c" uri="http://java.sun.com/jstl/core" %> <%@taglibprefix="c" >uri="http://java.sun.com/jstl/xml"%> <html> <body> <c:importurl="books.xml"var="url" /> <x:parsexml="${url}"var="doc"/> ----------------------------------------------- <x:forEachvar="n" select="$doc/books/book"> <x:outselect="$n/title"/> <x:outselect="$n/author"/> ======== </x:forEach> </body> </html>
Magically, we have parsed a given XML document and extracted information, without any mention about DOM,SAX and suchwords., atall!Wonderful!As a famous author would say, ‘anything that makes my job easier, I like!’. When we executethe ‘program’, we get the following result.
(Result for executing demo1.jsp)
========================== cobol roy ========== java herbert ========== c++ robert ========== coldfusion allaire ========== xmlunleashed morrison ========== jrun allaire ==========
The following program (demo2)displays the books and authors of xml filein table format.
demo2.jsp
<%@ page contentType="text/html" %> <%@taglibprefix="c" uri="http://java.sun.com/jstl/core" %> <%@taglibprefix="c" >uri="http://java.sun.com/jstl/xml"%> <html> <body> <c:importurl="books.xml"var="url" /> <x:parsexml="${url}"var="doc" /> <tableborder=1> <th> <tr> <td>title</td> <td>author</td> </tr> </th> <x:forEachvar="n" select="$doc/books/book"> <td> <tr> <x:outselect="$n/title"/></tr> <tr> <x:outselect="$n/author"/></tr> </td> </x:forEach> </table> </body> </html>
title |
author |
cobol |
roy |
java |
herbert |
c++ |
robert |
coldfusion |
allaire |
xml |
morrison |
jrun |
allaire |
demo3 deals with the selection of particular book’s authorfrom 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 ansql query like
"select * from table1 where title='jrun'"
demo3.htm
<html> <body> SELECT THE TITLE.<BR> YOU WILL GETTITLE & AUTHOR. <form method=postaction="demo3.jsp"> <select name="combo1"> <option value="xml unleashed">xml <option value="c++">c++ <option value="coldfusion">cold fusion <option value="java">java <optionvalue="cobol">cobol </select> <input type=submit> </form> </body> </html>
—————————
demo3.jsp
<%@ page contentType="text/html" %> <%@taglibprefix="c" uri="http://java.sun.com/jstl/core"%> <%@taglibprefix="c" uri="http://java.sun.com/jstl/xml"%> <html> <body> <c:importurl="books.xml"var="url" /> <x:parsexml="${url}"var="doc"/> <c:setvar="s" value="${param.combo1}"/> ------ <x:forEachvar="n" select="$doc/books/book" > <x:ifselect="$n/title=$s" > <x:outselect="$n/title"/> <br> <x:outselect="$n/author"/> <br> </x:if> </x:forEach> </body> </html>
demo4 is a simple variation on the same theme. In this case, the user selects the author name from the combo and any books by that author are displayed, due to the code. It will be noted that ‘allaire’ has two books to his credit and so if we choose ‘allaire’ in the combo,his two books are displayed.If ‘haris’ is chosen, we should display the message that it is yet to be published as there is no such entry in the xml file. But there is no ‘if-else’ construct and so we improvise. We have created a variable ‘a’ and assigned the value ‘ok’ to it. If there is no author to match the user’s selection, the conditional block is ignored and’a’ will not be ‘ok’.
From this, we conclude that ‘the book is not ready’.
demo4.htm
<html> <body> <b>Select name of author & view his books</b><br> <form method=post action="demo4.jsp"> <select name="combo1"> <option value="morrison">morrison <option value="robert">robert <option value="allaire">allaire <option value="herbert">herbert <p class="MsoNormal" style="margin-right: 4.5pt;