How to read xml data in JSP using JSTL

What are the XML-related JSTL tags and how to read data from XML files in JSP using JSTL? In this JSTL tutorial, let’s create examples to read complete XML data, read data from an XML tag, read data into table format in JSP.

Read XML data in JSP using JSTL

In order to read the data from XML, we need two XML files to store the information and a JSP file that contains the JSTL tags to read the data from XML.


XML file is a file that contains data in user-defined tags, similarly, employee.xml is a file that contains information about the employees.

<?xml version="1.0" encoding="UTF-8"?>



JSTL Taglib for XML

To enable the JSTL XML tag on the web page, we have to use JSTL Taglib for XML which is <%@ taglib prefix="x" uri="" %>  with the core JSTL Taglib that is <%@ taglib prefix="c" uri="" %>

Read complete XML Data in JSTL

By using <c:import><c:import/> from core Taglib of JSTL we can simply import or include a file. and that’s it, Just run the web page over the browser it will display all the data from an XML file.

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

Read XML tag Data in JSP using JSTL

In order to read data from a specific tag of the XML file, we have to parse the XML file. by using <x:parse></x:parse> JSTL tag we can parse the data and to display it over the web page we will use <x:out/> JSTL tag that is also come from the XML tag lib of JSTL.

To parse XML define the file name in xml=”${xmlFile}” Attribute and a variable var=”xmlData”  in <x:parse></x:parse> Tag.

<x:parse xml="${xmlFile}" var="xmlData"></x:parse>

To Display result use the defined variable name for select=”$xmlData/employees/employee[1]/name attributes. this will read the name from the first index.

<x:out select="$xmlData/employees/employee[1]/name"/>

Read XML Data in foreach loop using JSP, JSTL

In JSTL XML tag lib, we have prebuild tag <x:forEach select=”$xmlData/employees/employee”> that s used to get the all the nodes. To strive the specific node data we can use us <x:out select="name"/> Tag.

<x:parse xml="${xmlFile}" var="xmlData"></x:parse>

<x:forEach select="$xmlData/employees/employee">

<x:out select="name"/>


Read XML data and display in table format in JSTL and JSTL

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="x" uri="" %>
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Read data from XML </title>
<c:import url="employee.xml" var="xmlFile"></c:import>


<x:parse xml="${xmlFile}" var="xmlData"></x:parse>
<table border="1">

<x:forEach select="$xmlData/employees/employee">


<td><x:out select="name"/></td>
<td><x:out select="age"/></td>
<td><x:out select="department"/></td>