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.

employee.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"?>

<employees>
   <employee>
      <name>Bhupi</name>
      <age>25</age>
      <department>CS</department>
   </employee>
   
   <employee>
      <name>Bhuvi</name>
      <age>30</age>
      <department>It</department>
   </employee>
   
   <employee>
      <name>Raj</name>
      <age>20</age>
      <department>DataScience</department>
   </employee>
   
   <employee>
      <name>Ayush</name>
      <age>21</age>
      <department>AI</department>
   </employee>


</employees>

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="http://java.sun.com/jsp/jstl/xml" %>  with the core JSTL Taglib that is <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

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"/>
<br/>

</x:forEach>

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

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

</head>
<body>

<x:parse xml="${xmlFile}" var="xmlData"></x:parse>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Department</th>
</tr>

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

<tr>

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

</tr>


</x:forEach>

</table>

</body>
<footer>
</footer>
</html>