Articles Index
By Rodrigo
Oliveira
August 2003
RSS ("Really Simple Syndication") is a web content syndication
format. RSS is becoming the standard format for syndicating news
content over the web. As part of my recent contract with Sun
Microsystems, I was tasked with the development of a JSP Tag Library to
be used by anybody with a basic
understanding of RSS, JavaServer Pages, and HTML. The taglib is mostly
geared towards non-technical editors of web sites that use RSS for
aggregating news content. My goal was to develop a JSP tag library that
would
simplify the use of RSS content (versions 0.91, 0.92 and 2.0) in web
pages.
The RSS Utilities Package is the result of that project. It contains a
set of custom JSP tags which make up the RSS Utilities Tag library, and
a flexible RSS Parser. This document describes how to use the parser
and the library provided in the RSS Utilities Package. Click
here to download the first release. The zip file contains a jar
file, rssutils.jar, containing the classes needed to use
the utilities, and a tld file, rssutils.tld, which
defines JSP custom tags for extracting information from RSS documents.
Installing the taglib
Although it's easy to use the tag library, installing it requires some
knowledge of how your web server works and how to configure it. The
first step is to download and unzip the package. Once the zip file has
been unzipped, place a copy of rssutils.jar and rsstaglib.tld
inside the /WEB-INF/lib directory of your web
application. Add the following taglib definition to the /WEB-INF/web.xml
file of your web application:
<taglib> <taglib-uri>/WEB-INF/rssutils.tld</taglib-uri> <taglib-location>/WEB-INF/rssutils.tld</taglib-location> </taglib>
Using the taglib
Once the taglib has been installed in your web
application, you can use it in your JSP pages by following these steps.
First, add the following line to the top of your JSP page:
<%@ taglib uri="/WEB-INF/rssutils.tld" prefix="rss" %>
Next, add an RSS feed to your JSP page using the feed tag, as in the
following example:
<rss:feed
url="http://servlet.java.sun.com/syndication/rss_java_highlights-10.xml"
feedId="javaSunCom"/>
The url attribute of the "feed" tag must contain the URL
to the RSS document. The feedId attribute of the "feed"
tag is arbitrary, and can be set to anything. However, it is
recommended that the attribute be something intuitive that identifies
the RSS feed. If your application resides behind a firewall, you can
use the proxy attributes of the "feed" tag, namely proxyAddress
and proxyPort, to set your proxy settings. Check with
your network administrator if you don't know your proxy server address
proxy port. Here is an example:
<rss:feed
url="http://servlet.java.sun.com/syndication/rss_java_highlights-10.xml" feedId="example1" proxyAddress="129.149.246.4" proxyPort="8080"/>
|
Once you have added one or more RSS feeds to your page, you should
be able to extract almost any information out of the feeds using the
remaining set of tags. Here is an example showing how to extract the
title of the channel of the RSS feed we added above:
<rss:channelTitle feedId="javaSunCom"/>
Taglib examples
Example 1 (RSS 0.91):
<rss:feed url="http://servlet.java.sun.com/syndication/rss_java_highlights-XYZCompany-10.xml" feedId="example1"/> <b>Image: </b><rss:channelImage feedId="example1" asLink="true"/><br> <b>Title: </b><rss:channelTitle feedId="example1"/><br> <b>Link: </b><rss:channelLink feedId="example1" asLink="true"/><br> <b>Description: </b><rss:channelDescription feedId="example1"/><br> <ul> <li><rss:itemTitle feedId="example1" index="0"/><br> <rss:itemDescription feedId="example1" index="0"/><br><br> <li><rss:itemTitle feedId="example1" index="1"/><br> <rss:itemDescription feedId="example1" index="1"/><br> </ul>
|
Example 2 (RSS 0.92):
<rss:feed url="http://static.userland.com/gems/backend/gratefulDead.xml" feedId="example2"/> <b>Image: </b><rss:channelImage feedId="example2"/><br> <b>Title: </b><rss:channelTitle feedId="example2"/><br> <b>Link: </b><rss:channelLink feedId="example2" asLink="true"/><br> <b>Description: </b><rss:channelDescription feedId="example2"/><br> <ul> <rss:forEachItem feedId="example2"> <li><rss:itemDescription feedId="example2"/><br><br> </rss:forEachItem> </ul>
|
Example 3 (RSS 2.0):
<rss:feed url="http://static.userland.com/gems/backend/rssTwoExample2.xml" feedId="example3"/> <b>Image: </b><rss:channelImage feedId="example3"/><br> <b>Title: </b><rss:channelTitle feedId="example3"/><br> <b>Link: </b><rss:channelLink feedId="example3" asLink="true"/><br> <b>Description: </b><rss:channelDescription feedId="example3"/><br> <b>Copyright: </b><rss:channelCopyright feedId="example3"/><br> <b>Docs: </b><rss:channelDocs feedId="example3"/><br> <b>Generator: </b><rss:channelGenerator feedId="example3"/><br> <b>Language: </b><rss:channelLanguage feedId="example3"/><br> <b>Last Build Date: </b><rss:channelLastBuildDate feedId="example3"/><br> <b>Managing Editor: </b><rss:channelManagingEditor feedId="example3"/><br> <b>Pub Date: </b><rss:channelPubDate feedId="example3"/><br> <b>Skip Days: </b><rss:channelSkipDays feedId="example3"/><br> <b>Skip Hours: </b><rss:channelSkipHours feedId="example3"/><br> <b>TTL: </b><rss:channelTTL feedId="example3"/><br> <ul> <rss:forEachItem feedId="example3" startIndex="2" endIndex="4"> <li><rss:itemDescription feedId="example3"/><br><br> </rss:forEachItem> </ul>
|
How to use the RssParser
The parser was, in a way, a by-product of the project. Although the
parser was developed with the tag library in mind, it is completely
self-contained, and it can be used in Java applications. To do so,
however, you obviously need to know how to write at least basic Java
code. (If you know how to write Hello World in the Java language, you
are probably all set.)
First download and unzip the package. Once you have added rssutils.jar
to your classpath, create an instance of the RssParser
interface using the RssParserFactory. Here is an example:
RssParser parser = RssParserFactory.createDefault(); Rss rss = parser.parse(new URL("http://mydomain.com/document.rss"));
The RSS object generated by the parser is a Java object representation
of the RSS document found at the provided URL. Use the methods provided
by the RSS object to get a handle to other RSS objects, such as
Channels and Items. The RssParser can also parse File
objects and InputStream objects.
Conclusion
RSS provides a simple way to add and maintain news -- as well as other
content -- on your web site, from all
over the web. Even though RSS is a simple XML format, parsing and
extracting data out of XML documents
hosted elsewhere on the web can be a bit tricky-- or at least tedious
-- if you have to do it over and over again. The RSS Utilities Package
leverages Custom Tag and XML Parsing technologies to make the "Real
Simple Syndication" format live up to its name.
See also
Share your feedback and join our online
discussion!
1 As used on this web site, the terms Java virtual
machine or Java VM mean a virtual machine for the Java platform.
-->
Have a question about programming? Use Java Online Support.
|