Randomize your RSS feeds with ColdFusion

Learn some neat things you can do with RSS, including how you can use randomization code to show a random selection of news stories from a random sampling of RSS feeds.

You don't have to go to to get the latest tech news, Yahoo! News for world events, and then my blog to see what I've been rambling about lately. Instead, you can get news from multiple sources consolidated in a single page with a bit of scripting and some RSS savvy. The RSS standard, a version of XML, allows news to be syndicated and read in a standard file format.

The basics of how to use an RSS feed have been covered before here at; so, for this article, I'll be covering some neat tricks you can do with RSS. In particular, you can use randomization code to do more than simply show the top five news articles from Yahoo or Instead, you can show a random selection of news stories from a random sampling of RSS feeds. This can give your site or blog an edge. Not only can it provide news and other content, but it can be different with each visit, providing a rich experience to your readers every time they drop by your site.

The RSS feed
First, we'll cover the RSS feed itself. ColdFusion has a great new feature for displaying variables: The CFDUMP tag allows you to output to the screen complex variables that, previously, were next to impossible to display on the screen without a great deal of coding. This image gives you an idea of the output from the CFDUMP tag with the RSS feed provided.

The RSS feed is a very structured XML-esque file with a main structure, RSS with multiple subnodes, like CHANNEL, and XMLattributes. The CHANNEL node contains the multiple ITEM nodes, each of which is a news item. Each ITEM has an element for link, title, and so on.

Accessing an RSS feed and making use of it
ColdFusion makes accessing an RSS feed very easy; a few lines of code and you've grabbed, parsed, and displayed your file. This is the simple line of code needed to access the RSS feed you would like to grab:

After setting the returned RSS feed to an application scope variable that is accessible throughout the entire ColdFusion site, we can parse the data on any page we choose. My site displays a random sample of RSS stories (random stories from a random number of sources) on the home page. Listing A shows the code used to display a random group of five stories from Yahoo news.

The end result of Listing A is a random display of four news headlines, randomly selected out of the list of 10 or so that Yahoo returns in its tech headlines RSS feed. When users refresh or return to the home page, they are presented with a new random selection of headlines.

A little more random RSS
We've looked at my Web site, randomly picking four stories from the single RSS feed from Yahoo. Now, we'll (obligatory Emeril) kick it up a notch by not only randomizing the stories but randomizing the sources as well. My site now takes three RSS feeds (Yahoo News and two "top five" feeds) and randomly displays five headlines.

The new code is a slight modification of the original. Where I used to download and store a single feed in a global variable, I now download and store three feeds in an array of structures. ColdFusion handles this type of complex data pretty well, allowing plenty of flexibility in dealing with arrays and structures. On the main home page, the original code that looped over the feed randomly displaying an entry now has the added capability of randomly selecting from the array of structures holding the RSS.

Listing B shows the code that is used to parse the RSS feed into an object that ColdFusion can use. All of this occurs in the Application.cfm, creating the final usable object as a global variable. I took two steps to help minimize the load on the server since grabbing RSS feeds; especially three of them, is no small task.

First, I placed all of the parsing in one page, keeping everything in the global APPLICATION scope. This will help page loading since there won't be any global to local variable translation on the page that displays the feeds. The other strategy I employed was a timestamp on the actual grabbing of news feeds. The first time a user visits the site, a global variable is created with the current time. Each subsequent visit is compared against the timestamp and visits within 20 minutes use the existing RSS feeds. After 20 minutes, the feeds are refreshed. Each parsed feed is stored in a local array that is then processed and manipulated by Listing C. Listing C looks very much like the original RSS feed display code from Listing A, with the exception that, in place of a single variable, VARIABLES.LocalRSS, we now have an array of data, APPLICATION.ParsedRSS[i].

With the code from the above examples, the end result is five randomly selected headlines from three different news sources. This example of "spicing up" syndicated content display hopefully offers an alternative to the standard syndicated headlines from a single source. Offer your users a little variety, a better reason to visit your site, and another reason to hit the refresh button.

Editor's Picks

Free Newsletters, In your Inbox