<!-- Filename: data.xml -->
<persons>
<student>
<name>Mary</name>
<age>12</age>
<class>5</class>
</student>
<student>
<name>John</name>
<age>13</age>
<class>6</class>
</student>
</persons>
Code language: HTML, XML (xml)
Assume that this file is placed next to JSP file where we need to parse this xml document. Following will be the JSP file which will parse data.xml file and display information of each student as a row in table. Content of ShowStudents.jsp <!-- Filename: ShowStudents.jsp -->
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<HTML>
<HEAD>
<TITLE>Display Student Information</TITLE>
<STYLE>
table {border: 1px solid blue; width: 300px;}
</style>
</HEAD>
<BODY>
<c:import var="xmlDoc" url="data.xml"/>
<x:parse var="parsedDocument" xml="${xmlDoc}"/>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Class</th>
</tr>
<x:forEach select="$parsedDocument/persons/student">
<tr>
<td> <x:out select="name" /> </td>
<td> <x:out select="age" /> </td>
<td> <x:out select="class" /> </td>
</tr>
</x:forEach>
</table>
</BODY>
</HTML>
Code language: HTML, XML (xml)
Understanding the code To parse a given XML document, first we need to import it using <c:import> tag. This tag will import the content of the url into a variable. In previous example, content of the data.xml will be copied into variable xmlDoc. For parsing the imported XML content, <x:parse> tag is used. Hence the parsedDocument variable will contain parsed XML document. This variable can be used then to access other child tags as well as properties. <x:forEach> tag can be used to iterate across a given tag. Here in above example we have iterated through <student> tag by using <x:forEach select=”$parsedDocument/persons/student”>. Child elements of <student> tag can be accessed by <x:out select=”name” />. Hence this tag will print the value of the <name> tag. <?xml version="1.0"?>
Code language: HTML, XML (xml)
The root of the RSS 2.0 format is <rss> and <channel> tag. All of the feed content goes inside these tags. <rss version="2.0">
<channel>
Code language: HTML, XML (xml)
Next comes the information about the feed such as title of feed, the description, like of the site etc. <title>Google News Feed</title>
<link>http://news.google.com</link>
<description>Google News Feed</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2008 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2008 09:41:01 GMT</lastBuildDate>
Code language: HTML, XML (xml)
A channel may contain any number of <item>s. An item may represent a “story” — much like a story in a newspaper or magazine; if so its description is a synopsis of the story, and the link points to the full story. An item may also be complete in itself, if so, the description contains the text (entity-encoded HTML is allowed), and the link and title may be omitted. All elements of an item are optional, however at least one of title or description must be present. Each item has a title, link, description, publication date and guid. <item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>
How do Americans get ready to work with Russians aboard the International Space Station?
</description>
<pubDate>Tue, 03 Jun 2008 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2008/06/03.html </guid>
</item>
Code language: HTML, XML (xml)
<!-- Filename: FeerReader.jsp -->
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<HTML>
<HEAD>
<TITLE>Feed Reader in JSTL</TITLE>
<STYLE>
table
{border: 2px ridge ; width: 500px}
#feed .title
{font-family: Arial; font-weight: bold; font-size: 18px}
#feed .label
{font-family: Tahoma; font-weight: bold; font-size: 11px}
#feed td
{font-family: Tahoma; font-size: 11px}
</style>
</HEAD>
<BODY>
<form>
<input type="text" name="feedURL"
value="http://news.google.com/?output=rss" />
<input type="submit" value="Display"/>
</form>
<c:if test="${param.feedURL != null}">
Feed URL: ${param.feedURL}
<c:import var="xmlContent" url="${param.feedURL}"/>
<x:parse var="doc" xml="${xmlContent}"/>
<table class="content-table"" id="feed">
<tr class="profile_odd">
<td align="center" colspan="2">
<span class="title">
<x:out select="$doc/rss/channel/title" />
</span>
</td>
</tr>
<x:forEach var="story"
select="$doc/rss/channel/item" varStatus="status">
<tr>
<td colspan="2"> <hr/> </td>
</tr>
<tr class="profile_even">
<td class="label">Topic</td>
<td> <x:out select="title" /> </td>
</tr>
<tr class="profile_even">
<td class="label">Published Date</td>
<td> <x:out select="pubDate" /> </td>
</tr>
<tr class="profile_even">
<td class="label">Category</td>
<td> <x:out select="category" /> </td>
</tr>
<tr class="" valign="top">
<td class="label">Description</td>
<td><x:out select="description" escapeXml="false"/></td>
</tr>
</x:forEach>
</table>
</c:if>
</BODY>
</HTML>
Code language: HTML, XML (xml)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks for this. http://viralpatel.net/ is now in my feed reader, I'll keep and eye out for your next story. I like the layout of your site, nice and clean and easy to read. Thakns.
Thanks Akmed for the comment :)
Watch this space for more fun on tech side.
Hi Viral,
Thanks a lot for that info. Since i am totally new to web development, i have some queries. What are the prerequisites for running the JSTL RSS feed reader that you have created above? can i simply run the above jsp page from my desktop and see if google news is getting parsed? If not what are the steps to be followed? At present when i tried running it using Mozilla, the taglib porion was getting displayed as it is and no parsing was occuring. Can ya please help out? Thanks a lot
Hi Koolz,
As I learned from your comment, you are probably new to web development, I think you have to setup first some basics environment for running JSP/JSTL in your machine. For that you may want to install servlet container like Tomcat 5.5 and then add this project in it and run it. Try to search for some basic information about JSP on internet. This much info may not be sufficient for a newbee to start JSP development, but believe me, it is pretty much easy. Just hook to it and try to read some books about JSP/Servlets etc.
Hope this will help..
I am getting the following error in the code you provided. Any clues for this error.
org.apache.jasper.JasperException: Exception in JSP: /rssparse.jsp:37
34:
35:
36:
37:
38:
39:
40:
It seems that your code contains html tags and thus is truncated. Can you wrap your code in a
<pre>
tag?Hi Viral,
I am not getting any data from the RSS parser. Here is my code. I have confirmed that the rss is loading in the xml variable. But after that I don't get any result just an empty page. Please help me.
<a href="">
i am not able to post any code.
@amit: wrap your code inside <pre> tag.
Hi Viral,
I have to parse a Medline Citation XML using JAXB.Can u plz help me?
I have already given my full week on R and D...
Thankx in advance
thank's been looking for this
Hi Viral,
Thanks a lot for this one.
If I want to parse the tag like
How can I do?