How to use <x:parse> tag of JSTL for parsing XML documents


If you want to parse a XML document, you can use x:parse tag like the following example;

Before using this example don’t forget to include standard.jar and jstl.jar inside lib folder.

Here is the content for the XML file used in this example

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
<SERVER NAME="server1" ENABLED="Y">
<IP>10.1.2.1</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>12345</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server2" ENABLED="Y">
<IP>10.1.2.2</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>54321</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server3" ENABLED="N">
<IP>10.1.2.3</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>012345</PASSWORD>
<DESCRIPTION>Application Server3</DESCRIPTION>
</SERVER>
<SERVER NAME="server4" ENABLED="Y">
<IP>10.1.2.4</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>0123456</PASSWORD>
<DESCRIPTION>Application Server4</DESCRIPTION>
</SERVER>
<SERVER NAME="server5" ENABLED="N">
<IP>10.1.2.5</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>01234567</PASSWORD>
<DESCRIPTION>Application Server5</DESCRIPTION>
</SERVER>
</SERVERS>
</SERVERCONFIG>

And the JSP page for processing XML content

index.jsp file

<%--
Document   : index
Created on : 16.Eyl.2011, 11:35:25
Author     : tunatore
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body style="background-color: aliceblue">
<center>
<h1>&lt;x:parse&gt; example</h1>
<!--c:import var="server" url="http://localhost:8080/JSTLxparseTagExample/servers.xml"/>-->
<c:import var="server" url="servers.xml"/>
<x:parse xml="${server}" var="output"/>
<table>
<tr>
<td align="center"><b> Getting the first server's information using XPath evaluation of &lt;x:out&gt; tag </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<tr><td><b>IP:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/DESCRIPTION" /></b></td></tr>
</table>
</td>
</tr>
<tr>
<td align="center"><b> Getting all servers using &lt;x:forEach&gt; tag  </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<x:forEach select="$output/SERVERCONFIG/SERVERS/SERVER"  >
<tr><td><b>SERVER--></b></td><td><b><x:out select="@NAME" /></b></td></tr>
<tr><td><b>IP:</b></td><td><b><x:out select="IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="DESCRIPTION" /></b></td></tr>
</x:forEach>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>

And this is the output for the program;

Download sample project

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s