Previous Page
Next Page

Hack 42. Create an RSS Feed Reader

Grab and display XML-based RSS news feeds.

Really Simple Syndication (RSS) is an XML-based format for publishing news, blog entries, and other fast-changing information. Thousands of web sites now provide RSS news feeds as an alternative to visiting the actual sites in a browser. An RSS feed reader lets you subscribe to various feeds. The reader periodically (usually not more than once per half hour) grabs the latest RSS file from each subscribed site, then lets you view those feeds. Some RSS feed readers are built into browsers (Firefox), others are integrated into mail clients (Opera), and others are entirely web-based.

Because RSS feeds are simply XML files, they're easy for an Ajax application to digest. This hack will show you how to read an RSS feed from your server, parse the XML data, and format it for the browser.

Handling RSS feeds is not limited to standalone feed readers. You may want to incorporate RSS data into other applications, such as web portals. RSS feeds are now used for a variety of data beyond just news. For example, the U.S. National Weather Service has weather forecasts and warnings available as RSS feeds (go to for a listing of available weather feeds).

The following abridged RSS file illustrates the basic structure of an RSS feed:

<?xml version='1.0' encoding='utf-8'?>

<rss version='2.0'

<title>O'Reilly Media, Inc. New Books</title>
<description>O'Reilly's New Books</description>
<copyright>Copyright 2005, O'Reilly Media, Inc.</copyright>
<itunes:author>O'Reilly Media, Inc.</itunes:author>
<itunes:category text='Technology' />

    <title>C in a Nutshell</title>
    <description><![CDATA[Covering the C programming language and C 
    runtime library, this book. . .]]>
    <author> (Tony Crawford, Peter Prinz)</author>

    <title>Run Your Own Web Server Using Linux &amp; Apache</title>
    <description><![CDATA[Learn to install Linux and Apache 2.0 on a 
    home or office computer for testing and development, and . . .]]>
    <author> (Tony Steidler-Dennison)</author>


Most RSS feeds contain a single channel element. In RSS files for news and blogs, the channel usually contains multiple items (one for each article).

The RSS files our Ajax application reads must reside on the same server, or within the same domain, as our application itself. For security reasons, most browsers don't let an application from one domain grab data from another domain. This makes browsing safer but limits functionality a bit.

A Simple RSS Reader

For our RSS reader, let's assume you've set up some mechanism to grab fresh RSS files periodically and store them on your server. This can be as simple as setting up a crontab entry on your Linux server:

0/30 * * * * wget q O /var/www/html/feeds/oreilly_new_titles.rss.xml \\

Figure 4-24 shows the simple user interface of our RSS reader: a pull-down list to select the RSS feed, and a checkbox to let users select more details for each article displayed.

Figure 4-24. A simple RSS feed reader

Select a news feed, and the matching RSS file is grabbed from the server. The RSS reader extracts information from the file and builds the HTML for the web page, as shown in Figure 4-25.

Figure 4-25. Displaying RSS feed content

Our RSS feed reader is contained in the files rss.html and rss_parse.js (and the ubiquitous JavaScript file xhr.js, which provides a browser-neutral XMLHttpRequest object). The first file, shown here, defines the web page itself:

<TITLE>O'Reilly RSS Reader</TITLE>

<script language="javascript" src="xhr.js"></script>
<script language="javascript" src="rss_parse.js"></script>


<b>O'Reilly RSS Reader</b><p>
<form id="frmRSSParse">

<select id="lbFeeds" onChange="get_rss_feed(  );">
    <option value="">SELECT A FEED</option>
    <option value="oreilly_news_articles.rss.xml">
        O'Reilly News and Articles
    <option value="oreilly_new_titles.rss.xml">
        O'Reilly New Titles
    <option value="oreillynet_articles_blogs.rss.xml">
        O'Reilly Network Articles and Weblogs

<input id="cbDetails" 
       onClick='format_rss_data ("content", last_xml_response);'
show details

<div id="content">



The web page references rss_parse.js, which defines the three JavaScript functions needed to implement the RSS reader.

How It Works

A handler is attached to the listbox's onChange event. When the user selects an item from the list, the get_rss_feed( ) JavaScript function is called:

<select id="lbFeeds" onChange="get_rss_feed(  );">

This function grabs the URL of the selected RSS file from the listbox and passes it to the get_xml_file( ) function. The second function does the work of retrieving the XML file from the server. This code shows these functions:

function get_xml_file (url) {
    var httpreq = getHTTPObject(  );

    //Precondition: must have a URL
    if (url == "") return;"GET", url, true);

    httpreq.onreadystatechange = function (  ) {
        if (httpreq.readyState == 4) {
            var content = document.getElementById("content");
            content.innerHTML = "Parsing XML...<br>";

            last_xml_response = httpreq.responseXML;
            format_rss_data ("content", last_xml_response);

    var content = document.getElementById("content");
    content.innerHTML = "Retrieving XML...<br>";
    httpreq.send (null);

function get_rss_feed (  ) {

    //Get selected RSS feed
    var lbFeeds = document.getElementById("lbFeeds");
    if (lbFeeds.value != "") {
        get_xml_file (lbFeeds.value);


The Document object has its own methods for loading an XML file, using the createDocument and load methods. However, let's use XMLHttpRequest in our example because this is more "Ajaxy." Either technique works fine, and both can be made to work (with a little effort) in most of the popular browsers.

The retrieved XML file is stored as a Document object. We pass this object to our third and final function, format_rss_data( ). This is where the Document object is examined and we pull out the items we need. Each news snippet is enclosed in an item element. For our RSS reader, we want to extract three pieces of information from each item: the title, the link to the full article, and a brief description of the article. Here's how it works:

function format_rss_data (divname, response) {
    var html = "";
    var doc = response.documentElement;
    var items = doc.getElementsByTagName('item');

    for (var i=0; i < items.length; i++) {

        var title = items[i].getElementsByTagName('title')[0];
        var link = items[i].getElementsByTagName('link')[0];

        html += "<b><a href='" 
             + "'>" 
             + "</a></b><br>";

        var cbDetails = document.getElementById("cbDetails");
        if (cbDetails.checked) {
            var desc = items[i].getElementsByTagName('description')[0];
            html += "<font size='-1'>" 
                    + "</font><p>";

    var target_div = document.getElementById(divname);
    target_div.innerHTML = html;

The format_rss_data( ) function uses a for loop to iterate over each item element in the RSS Document object. Using the getElementsByTagName( ) method, extract the title, link, and description information, and build the HTML displayed on the web page.

Now save the most recent Document object in the last_xml_response variable. If the user checks (or unchecks) the "show details" checkbox, you can reformat the current RSS data with another call to format_rss_data( ), and without another request to the server. Figure 4-26 shows the page with "show details" unchecked. In this view, the descriptions are hidden, and the user is presented with a simple list of article links.

Figure 4-26. The RSS reader with descriptions hidden

Hacking the Hack

This hack doesn't display all the information for each articleauthor and date information is omitted, and no general channel information is displayed. If you want to use this hack as a generic way to include feed information in web pages, you need to expand format_rss_data( ) to (at least) display the channel title.

Having the RSS feeds hardcoded into the listbox isn't very flexible, either. You can maintain a list of RSS feeds on your server (as an XML file, perhaps), but even this may be unwieldy if you monitor hundreds of feeds. You might consider using a "categories" listbox that populates the "feeds" listbox instead.

Mark Pruett

Previous Page
Next Page