13.8 Build Your Own RSS Consumer

You don't have to use a prebuilt RSS aggregation tool or RSS consumer. Because of the restrictions and constraints placed on RSS, it's relatively simple to build your own application that processes RSS and displays the results. You can use technology that is designed for XML or RDF (demonstrated throughout the book), or you can use technology specifically designed for RSS.

To demonstrate, I created an application to process the book recommendation RSS feed shown in Example 13-5. It uses an RSS API written in Java to process the RSS (any RSS) from a JSP page.

I'm rather partial to J2EE applications and have a Tomcat installation at my web site. My first inclination when creating a new dynamic web application is to see if there is a Java API I can use within JSP pages. In the next example, an RSS specialized API is used?a brand new API called the Informa RSS Library, available at Source Forge (http://sourceforge.net/projects/informa/.. This API provides Java classes that allow you to access specific pieces of RSS data, rather than having to write the XML parsing aspect of the code.

I downloaded the Java .jar files containing the code and added then to the JAVA_APP/WEB-INF/lib subdirectory associated with the location of the JSP pages. I then restarted Tomcat in order to pick up the new libraries.

The Informa RSS Java libraries are added into the JSP page with an imports statement, as shown in Example 13-6.

Example 13-6. JSP imports section
<%@ page import="java.net.URL,
                 de.nava.informa.parsers.RSS_1_0_Parser" %>

Next, within the HTML body of the JSP page, the RSS file is opened and the contents processed and printed out to the page, as shown in Example 13-7.

Example 13-7. Using RSS Java API to process RSS file
        // get RSS file URL
        String sRDFUri = request.getParameter("uri");
        String sTitle = null;

        URL u = null;
        ChannelParserIF parser = null;
        ChannelIF channel = null;
        Collection cItems = null;
        Iterator iItems = null;
        String sError;

        try {
          u = new URL(sRDFUri);

          // create RSS 1.0 parser
          parser = new RSS_1_0_Parser(
                             new ChannelBuilder(  ));

          // get channel 
          channel = parser.parse(u);
          sTitle = channel.getTitle(  );

          // print title as header
          out.println("<h1>" + sTitle + "</h1>");
          cItems = channel.getItems(  );

          // get items collection
          iItems = cItems.iterator( );

          // iterate through collection and print out RSS elements`
          while (iItems.hasNext(  )) {
             ItemIF item = (ItemIF)iItems.next(  );
             out.print("<a href='");
             out.print(item.getLink(  ));
             out.print(item.getTitle(  ));  
             out.println("<br />");

catch (Exception e) {
   sError = e.getMessage(  );

In the example, an RSS 1.0 parser is created to parse the RSS into memory. Once in memory the channel element is accessed and the title is pulled from the element and printed out. Next, the Java collection containing the RSS file's items is accessed and each item element is processed, pulling the data out for printing. The book recommendation item title and link are accessed and a list of hypertext-linked titles is printed out on the page, as shown in Figure 13-6.

Figure 13-6. JSP- and Java-generated book list from RSS

A disadvantage to using a specialized RSS API is that different RDF vocabularies can't be used with the API?it's focused purely on RSS elements. However, an advantage to the RSS API is that any RSS file can be processed using the same JSP page.

Since the RSS file URL is passed to the JSP page from an HTML form element, instead of passing in the book recommendation RSS, I passed in my weblog's RSS 1.0 file. Figure 13-7 shows the page that results from reading in this file (with no change to the code).

Figure 13-7. List of weblog entries pulled from weblog's RSS file

Because RSS 1.0 is first and foremost RDF, you can also use any of the technologies and languages in the rest of the book to parse, or generate, the RSS demonstrated in this chapter, which is ultimately why RDF is such a handy thing for a software developer such as myself.