Easiest Way to get RSS Query?

I was working with some RSS feeds recently and ran into a few challenges. The first was a "connection failure" error from one RSS feed - even though it loaded up correctly in the browser. Another one yielded the error "An error occured while Parsing an XML document. Content is not allowed in prolog." when I tried to parse the XML.

Fortunately, both of these problems turned out pretty easy to solve.

Russ Michaels solved the "Connection failure" message using an HTTPParam to indicate that the returned XML should not be compressed (apparently ColdFusion/Java can't handle that correctly).

Ben Nadel solved the XML parsing error with a regular expression to remove the BOM from the XML string.

I took both of these solutions and combined them with a little bit of code to transform the relevant XML into a ColdFusion recordset to make a UDF to get a query of items from an RSS feed URL.

Here is the code:

<cffunction name="getRSSItems" access="public" returntype="query" output="no" hint="Returns a query of items from an RSS feed.">
   <cfargument name="source" type="string" required="yes">
   <cfset var CFHTTP = 0>
   <cfset var xData = 0>
   <cfset var axItems = 0>
   <cfset var cols = "title,description,pubDate,link,guid">
   <cfset var qItems = QueryNew(cols)>
   <cfset var ii = 0>
   <cfset var col = "">
   <cfhttp url="#arguments.source#">
      <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
   <cfset xData = XmlParse( REReplace(cfhttp.FileContent, "^[^<]*", "", "ALL") )>
   <cfset axItems = xData.rss.channel.item>
   <cfset QueryAddRow(qItems,ArrayLen(axItems))>
   <cfloop index="ii" from="1" to="#ArrayLen(axItems)#">
      <cfloop list="#cols#" index="col">
         <cfif StructKeyExists(axItems[ii],col)>
            <cfset QuerySetCell(qItems,col,HTMLEditFormat(axItems[ii][col].XmlText),ii)>
   <cfreturn qItems>

Here is how to get a query of my blog entries on frameworks:

I have also submitted this to cflib.org.

It looks like cflib.org already has a few other RSS UDFs that are also worth checking out (alphabetical order).

  • cfRssFeed: Can limit the number of results in the query.
  • createRSSQuery: Returns a structure including meta data and a query of items.
  • feedToQuery: Returns a structure including meta data and a query of items. Works with RSS, ATOM, or RDF feeds.
  • TranslateRSSItems: Works in ColdFusion 5 and above.

I don't think any of these solve the issues I mentioned above, but their other features may make one of them just the solution that you are looking for.

In any event, this solved a lot of headaches for me so maybe it will for you. More importantly, maybe I will run across this next time I run into this problem - after I forget that I wrote this.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hi Steve, I'm pretty sure there is a hotfix for this: http://kb2.adobe.com/cps/403/kb403781.html
# Posted By John Whish | 10/29/10 4:24 PM

Nifty! Thanks for pointing that out. I guess I should install that hotfix on my local dev machine.

# Posted By Steve Bryant | 10/30/10 7:18 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.