A recent project called upon us to parse RSS and present the feed in a simple list format. Using the straight-forward XMLParse method in ColdFusion MX 7, this task was a cinch. To speed up performance, the script caches the RSS feed once every fifteen minutes.
So that you don’t have to go through the trouble that we did to perfect it, here’s the script. As always, if you decide to use this in your own project, don’t forget to the leave the GNU GPL license in place.
<!---
RSS Feed Parser
@author Aaron Collegeman
@version 1.0
Copyright (C) 2008 Collegeman.net, LLC aaron@collegeman.net
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
--->
<cfparam name="feed_url" />
<cfparam name="item_limit" default="10" />
<cfparam name="css_class" default="rss_feed" />
<!--- create a cache filename from the URL --->
<cfset cache_name = hash(feed_url) & ".html" />
<!--- does our cache file exist, and if so, how old is the file? --->
<cfset dateLastModified = 0 />
<cfif FileExists(ExpandPath(cache_name))>
<cfdirectory action="list" directory="#ExpandPath("./")#" name="dir" filter="#cache_name#" />
<cfset dateLastModified = dir.dateLastModified />
</cfif>
<!--- if the cache file exists and it is not more than 15 minutes old, send it to the user --->
<cfif FileExists(ExpandPath(cache_name)) AND DateCompare(now(), dateLastModified, "n") LT 15>
<cffile action="read" file="#ExpandPath(cache_name)#" variable="html" />
<cfoutput>
#html#
<!-- cached RSS from #feed_url#; last updated #dateLastModified# -->
</cfoutput>
<cfelse>
<!--- download the feed --->
<cfhttp url="#feed_url#" method="GET" timeout="30" />
<!--- parse the feed --->
<cfset xml = mid(cfhttp.filecontent, Find("<rss", cfhttp.filecontent), len(cfhttp.filecontent)) />
<cfset root = XMLParse(trim(xml)) />
<!--- generate HTML snippet --->
<cfsavecontent variable="html"><cfoutput>
<div class="#css_class#">
<h2>#root.rss.channel.title.xmlText#</h2>
<ul>
<cfif StructKeyExists(root.rss.channel, "item")>
<cfif ArrayLen(root.rss.channel.item) GT 0>
<cfloop from="1" to="#min(ArrayLen(root.rss.channel.item), item_limit)#" index="i">
<cfset item=root.rss.channel.item[i] />
<li>
<div class="date">#DateFormat(item.pubDate.xmlText, "dddd, mmmm d, yyyy")#</div>
<div class="title"><a href="#item.link.xmlText#" target="_blank">#item.title.xmlText#</a></div>
<div class="description">#item.description.xmlText#</div>
</li>
</cfloop>
<cfelse>
<li>There are none.</li>
</cfif>
<cfelse>
<li>There are none.</li>
</cfif>
</ul>
</div>
</cfoutput></cfsavecontent>
<!--- cache the snippet --->
<cffile action="write" file="#ExpandPath(cache_name)#" output="#html#" />
<!--- send to the user --->
<cfoutput>
#html#
<!-- RSS from #feed_url#; just updated at #now()# -->
</cfoutput>
</cfif>
copy code