Home Jsp Use of Core XML tags in JSP

Related Tutorials


 
 

Share on Google+Share on Google+

Use of Core XML tags in JSP

Advertisement
Core XML tags provides access to XML data and used to parse these data. There are three tags in this Core XML tag set.

Use of Core XML tags in JSP

        

Example program to show use of Core XML tag in JSP

JSTL (JSP standard tag library) XML tag can be categorized into three sets :

Function Tag name Prefix
Core out, parse, set x
Flow control choose, when, otherwise, forEach, if x
Transformation transform, param x

Core XML Tags:

Core XML tags provides access to XML data and used to parse these data. There are three tags in this Core XML tag set.

1. <x:out> :

This tag is used to evaluate an XPath expression and outputs the result of current variable.

Syntax for <x:out> is shown below:

<x:out select=?XPathExpression? [escapeXml=?{true|false}?]/>

Attributes

select : XPath expression to be evaluated. It's mandatory.
escapeXML: Default value is true. Determine whether characters "<", ">", "&", "'", """ in the resulting string should be converted to their corresponding character entity code.

2. <x:parse> : 

This tag is used to parse an XML document and it saves the resulting object into an EL variable which is specified by the attribute "var"

Syntax is as follows:

<x:parse doc="${xmlFileVariable}" var="variableName" scope="application|request|session" />

3. <x:set> : This tag is used to evaluate an XPath expression and set the result into attribute "var"

Syntax for <x:set> is as follows :

<x:set select=?XPathExpression? var="variableName" />

To use XML tags in your program you have to firstly download these jar files in your Tomcat's "lib" folder. These jar files are:

  1. jstl.jar 
  2. resolver.jar
  3. serializer.jar
  4. standard.jar
  5. xercesImpl.jar
  6. xercesSamples.jar
  7. xml-apis.jar
  8. xalan.jar

Visit this link to download these jar files: http://www.mail-archive.com/taglibs-user@jakarta.apache.org/msg08807.html

This example illustrates use of XML core tags into a JSP file to show parsed data of an XML file "user.xml".

1. user.xml

<?xml version="1.0"?>
  <user>
   <information>
   <fname>amit</fname>
   <mname>kumar</mname>
   <lname>raghuwanshi</lname>
   <age>21</age>
   </information>
   <information>
   <fname>sandeep</fname>
   <mname>kumar</mname>
   <lname>suman</lname>
   <age>23</age>
  </information>
   </user>

This user.xml data is being parsed and is shown in JSP file by using <x:parse> and <x:out> and one flow control tag <x:forEach>. To have use of these XML tags you have to include following code in your JSP file.

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>

One core JSTL is also used in this JSP file to import user.xml file into JSP file variable by var attribute of <c:import>. This core JSTL tag library can be used by using code:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

Full code for this JSP file is given as below:

2. XMLinJSP.jsp

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head><title>Use of Core XML tags</title></head>
<body>
<h2>Example of using Core XML tags</h2>
 <c:import url="http://localhost:8080/Example/user.xml" var="user" />
  <x:parse xml="${user}" var="doc" /> 
  <h3>Information of user.xml file</h3>
  <x:forEach select="$doc/user/information" var="n">
  First Name:<x:out select="$n/fname"/><br>
  Middle Name:<x:out select="$n/mname"/><br>
  Last Name:<x:out select="$n/lname"/><br>
   Age: <x:out select="$n/age"/><br>
  </x:forEach>
</body>
</html>

To execute this example follow these steps :

  • Create and save an XML file user.xml
  • Download and put all jar files (as given above) into lib folder
  • Create and save XMLinJSP.jsp
  • Start Tomcat web server and type following url
    http://localhost:8080/Example/XMLinJSP.jsp

Output:

user.xml file data would show like this:

and output of JSP file would be

Download Source code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 5, 2008

Related Tutorials

Discuss: Use of Core XML tags in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com