Tuesday, December 10, 2024

Consuming RSS Feeds With ColdFusion

Share

ColdFusion doesn’t seem to get the respect it deserves amongst the dedicated Java or .NET or even PHP development circles, but having worked in all of those environments, I’d like to state for the record, that completing a large scale development project AHEAD of schedule, keeping your Business Managers and Projects Managers happy, and let’s not forget the most important of all…

THE CLIENT…Well, that’s priceless. Well worth any investment made towards ColdFusion.

Web services, RSS, XML, SOAP to name several, are all welcomed friends to the ColdFusion web development language. This article will focus on the likes of RSS and XML.

Definition: RSS is a family of web feed formats used to publish frequently updated digital content, such as blogs, news feeds or podcasts. Consumers of RSS content use special browsers called aggregators to watch for new content in dozens or even hundreds of web feeds. The initials “RSS” are variously used to refer to the following standards:

Really Simple Syndication (RSS 2.0) Rich Site Summary (RSS 0.91, RSS 1.0) RDF Site Summary (RSS 0.9 and 1.0) RSS formats are specified in XML (a generic specification for data formats). RSS delivers its information as an XML file called an “RSS feed”, “webfeed”, “RSS stream”, or “RSS channel”.

That’s the Wikipedia definition for an RSS feed. What’s not mentioned is that developers are able to take advantage of the content provided by these RSS feeds to display current and changing data on their websites, which adds value to the website and hopefully bring return visitors for more of the information that you provide.

Consuming an RSS feed in ColdFusion can be as simple or as complicated as you want it to be. There are several ways or more, to do this in ColdFusion. Parsing through array elements and defining the parent elements to children and testing for grandchildren elements, and recursive behaviors etc. But this article is not for the experienced ColdFusion developer. This article is for someone who might like to see some of the “ease of operation” of ColdFusion and how it relates to a popular web practice known as RSS.

We’ll look at a fun and popular feed from RollingStone Magazine. Located here http://www.rollingstone.com/rssxml/album_reviews.xml as of the time of this article. It’s a feed that I have implemented on several of my websites. Now it’s important to note that you should select feeds that are relevant to your website, I have several music based websites and thus having an “Album Review” feed was appropriate. Ok, so now what do we do with this feed you ask? Well, basically there are only 3 steps to consuming and parsing an XML/RSS feed.

1. Read the Feed in

2. Loop through the elements of the feed

3. Display the feed

Am I simplifying things a bit? Not really, the purpose here is to show how ColdFusion can do much of the ‘heavy lifting’ as it pertains to application development making many day to day programming tasks much less tedious.

Here’s an example:

In ColdFusion we read the feed in like this.

<cfhttp url="http://www.rollingstone.com/rssxml/album_reviews.xml" method="GET" resolveurl="No" ></cfhttp>
<cfset album_reviews_xml=XMLParse(cfhttp.FileContent)>

What this does is utilize the CFHTTP tag in ColdFusion which has many many uses, in this case it performs the request for the XML document on the server. The second line defines a variable called “album_reviews_xml” and gives it the value of the http response. Using the built in function “XMLParse()” in ColdFusion takes the http response and delivers it to your browser in XML format or as an XML Object for you OOP guys.

If you would like to have ColdFusion show you a ‘pretty’ presentation of the XML you can simply add a cfdump statement to your document and you will see a nicely rendered and easy to read xml structure.

The next step is parsing or looping through the elements of the XML Object.

You do that like this:

<cfoutput>
<cfloop index="x" from="1" to="#ArrayLen(album_reviews_xml.rss.channel.item)#">
Title: #album_reviews_xml.rss.channel.item[x].title.xmlText#
Link: {a href="#album_reviews_xml.rss.channel.item
[x].link.xmlText#"}click here
Description:#album_reviews_xml.rss.channel.item[x].description.xmlText#

</cfloop>
</cfoutput>

This loop as shown, iterates through all of the elements of the document and displays the most common items in an RSS feed which are the TITLE, LINK and DESCRIPTION. Some feeds aren’t formed entirely to standard and may have other elements designs, and of course this is not all of the data that you can get from an RSS feed, again it’s just the most common and standard elements returned through RSS. But if the document provided by the RSS feed generator is to standard and well formed, you will see these 3 elements in most every RSS feed.

As you can see displaying an RSS feed in ColdFusion is easy and provides real time up to date information that you can provide to your visitors on your website. You can apply style sheets and html formatting to your feeds and present the information in a pleasant looking way that visitors will enjoy reading.

An example where I use these feeds can be seen in action here http://www.news-junkie.net. This is a personal site that I had developed a while ago that is a simple news aggregation website with a number of different topics. I found it fun to develop and good practice working with xml and ColdFusion. The information is constantly updated and visitors to my site see fresh new articles everyday, and all I have to do now that it is built, is keep the domain name active and maybe switch around a few of my affiliate links which hopefully provide a little income to pay for the hosting of the site.

Thanks for reading and Happy Coding!

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark murdok:

Ben Cortese is a developer and business
analyst for the financial industry and develops affiliate
powered websites. http://www.inetsite.net Copyright 2007.

Table of contents

Read more

Local News