JSTL -XML Tags

JSTL - XML Tags

e-trainings-JSTL-XML-Tags

JSP Home


  
 

There are different types of XML tags are available in JSTL under XML tags. Using these tags we can perform task like read XML file, iterate the nodes and also select/choose  a specific node from XML file and so on. 

Many of the XML tags, in JSTL, are very much the same as the 'core' tags. For example,  <x:forEach>, <x:if>, <x:when> etc.


books.xml

<?xml version="1.0" ?>
<books>           
         <book>
             <title>Java</title>
             <author>roy</author>
    </book>
    <book>
             <title>JavaScript</title>
             <author>herbert</author>
    </book>
    <book>
             <title>C PlusPlus</title>
             <author>robert</author>
    </book>
    <book>
             <title>HTML</title>
             <author>allaire</author>
    </book>
    <book>
             <title>XMLunleashed</title>
             <author>morry</author>
    </book>
    <book>
             <title>C Language</title>
             <author>allaire</author>
    </book>
</books>

XMLdemo1.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> </x:forEach> </body> </html>
The following program (demo2)displays the books and authors of xml file in table format. XMLdemo2.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> Think of this as an sql query like "select * from books where title='javascript'"
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>

 

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP