In this tutorial you will learn about the JSTL xml core parse tag.
JSTL XML Tag parse Example
In this tutorial you will learn about the JSTL xml core parse tag.
<x:parse> tag of JSTL xml librarry parses the XML document.
Attributes of <x:parse> tag
- var : This is an optional attribute that specifies the name of a variable to store the parsed xml document value.
- varDom : An optional attribute specifies the name of a variable to store the parsed value of scope type variable org.w3c.dom.Document.
- scope : This is an optional attribute specifies the scope of the 'var' attribute.
- scopeDom : This is an optional attribute specifies the scope of the 'varDom' attribute.
- doc : This is an optional attribute specifies the source document for parsing. (xml attribute is deprecated so doc should be used instead).
- systemId : This is an optional attribute specifies the system identifier (URI) of the XML document to be parsed.
- filter : This is an optional attribute and is used for applying the Filter on source document.
Example :
In the example given below will demonstrate you about the JSTL xml <x:parse> tag. In this example at first I have created an xml file named studentDetail.xml because we have to fetch data from the xml file so to parse the xml file we will have to use <x:parse> tag in the example. Then created a JSP page where used the <c:import> tag of JSTL core to retrieve the XML document and stores it to the variable 'stu' by using 'var' attribute of this tag, then used the <x:parse> tag to parse the document using the attribute 'doc' and stores the result into the variable 'doc' using the attribute 'var' and to iterate over all the elements I have used the <x:forEach> tag and stored the value in the variable 'record' using 'var' attribute of this tag then in further statements I have used the <x:out> tag where the 'select' attribute will evaluate the XPath expression and output the result.
studentDetail.xml
<?xml version="1.0" ?> <students> <student> <name>Amit</name> <roll>33</roll> <grade>A</grade> </student> <student> <name>Mukesh</name> <roll>47</roll> <grade>B</grade> </student> <student> <name>Manoj</name> <roll>24</roll> <grade>B</grade> </student> <student> <name>Rajnish</name> <roll>84</roll> <grade>C</grade> </student> </students>
JstlXmlParse.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSTL x:parse Example</title> </head> <body> <c:import url="studentDetail.xml" var="stu" /> <x:parse doc="${stu}" var="doc"/> <p>Details of Students</p> <table> <x:forEach var="record" select="$doc/students/student"> <tr><td align="center">Name : </td> <td><x:out select="$record/name" /></td></tr> <tr><td align="center">Roll :</td> <td><x:out select="$record/roll" /></td></tr> <tr><td align="center">Grade :</td> <td><x:out select="$record/grade" /></td></tr> </x:forEach> </table> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
- xalan-2.3.1.jar
- xercesImpl-2.7.1.jar
After adding of these jar files you may execute your program in the following ways :
- Select JstlXmlParse.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your JstlXmlParse.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
When you will execute the above JSP page you will get the output on your eclipse browser as follows :
[ 0 ] Comments