Ask a Jedi: Handling RSS feeds with custom data

Neal asks:

I'm trying to use cffeed to read RSS feeds provided by National Public Radio (I manage a few websites for community radio stations that purchase NPR programming). Everything works great, except that NPR uses a custom namespace for certain elements. For example, npr:rmaudio provides a link to a Real audio file, but cffeed won't read this element. When I try to loop through the feed having captured it with cfhttp, coldfusion chokes on the colon. Any ideas? I can't seem to find much about this on the web. Finally, thanks for the many interesting articles you've written over the years.

So your right. The CFFEED tag ignores any custom elements in an RSS feed. This is rather unfortunate as I know I’ve run into multiple feeds that use custom fields. Technically you can get it via CFFEED if you use xmlVar to return the XML, but at that point, why bother with CFFEED. I’d just use CFHTTP. (And if you do so, you can get a bit fancy.)

As for the colon issue - you are the second person in the past 24 hours who hasn’t recognized a simple fact about structures. (Remember that when dealing with an XML object, you can treat parts of it as arrays and structs.) When it comes to keys that have colons, or other odd characters, all you need to do is use bracket notation. So consider this simple structure:

<cfset s = {}> <cfset s.age = 9> <cfset s["funky cold madina"] = 21>

The first key I set, age, is a simple string with no spaces in it, and I can use dot notation. The second key, “funky cold madina”, can’t be used with dot notation, but bracket notation works just fine.

So let’s look at the XML Neal was working with. The feed may be found here:

http://www.npr.org/rss/rss.php?id=2&station=KSJD_FM

And if you get this data via XML, you will see data like so:

<item> <title>Pennsylvania Primary Roundup</title> <description>Barack Obama improved his showing among white, middle-class voters, but not enough to beat Hillary Clinton in the Pennsylvania primary on Tuesday. NPR's National Political Correspondent Mara Liasson analyzes the race with Robert Seigel.</description> <pubDate>Tue, 22 Apr 2008 21:53:01 -0400</pubDate> <link>http://www.npr.org/stations/force/force_localization.php?station=KSJD_FM&url=http://www.npr.org/templates/story/story.php?storyId=89862645&ft=1&f=2</link> <guid>http://www.npr.org/templates/story/story.php?storyId=89862645&ft=1&f=2</guid> <npr:wmaudio>http://www.npr.org/templates/dmg/dmg_wmref_em.php?id=89863241&type=1&date=22-Apr-2008&mtype=WM</npr:wmaudio> <npr:rmaudio>http://www.npr.org/templates/dmg/dmg_rpm.rpm?id=89863241&type=1&date=22-Apr-2008&mtype=RM</npr:rmaudio> </item>

Note the NPR tags. Working with these tags is a simple matter. Consider this code:

<cfloop index="x" from="1" to="#arrayLen(xmlResult.rss.channel.item)#"> <cfset item = xmlResult.rss.channel.item[x]> <cfoutput> <p> <b><a href="#item.link.xmlText#">#item.title.xmlText#</a></b><br /> <cfif structKeyExists(item, "npr:wmaudio")> <a href="#item["npr:wmaudio"].xmlText#">WM Audio Link</a><br /> </cfif> <cfif structKeyExists(item, "npr:rmaudio")> <a href="#item["npr:rmaudio"].xmlText#">RM Audio Link</a><br /> </cfif> </p> </cfoutput> </cfloop>

All I do is loop over each item, output the things I know exist (link and title), and check to see if the WM or RM audio links exist. If so, I output them using bracket notation.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments