Ask a Jedi: Handling RSS feeds with custom data

This post is more than 2 years old.

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:

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></link> <guid></guid> <npr:wmaudio></npr:wmaudio> <npr:rmaudio></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("> <cfset item =[x]> <cfoutput> <p> <b><a href="">#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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Steve Withington posted on 4/23/2008 at 6:17 PM

Ahem, it's <a href="">Funky Cold Medina</a>

Comment 2 by Andy Matthews posted on 5/31/2008 at 7:37 AM

I'm having the same issue with Google's Picasa API. In fact I just made a post about it on the HoF mailing list:

So if I understand're saying that I basically can't use CFFEED for feeds that have custom namespaces if I want to get those custom nodes?

Comment 3 by Raymond Camden posted on 5/31/2008 at 4:06 PM


Comment 4 by Andy Matthews posted on 6/1/2008 at 7:14 AM

Suck. Do you have any idea if this is something they'll fix? Seems like it's a pretty significant omission.

Comment 5 by Raymond Camden posted on 6/1/2008 at 7:16 AM

Well, its still pretty trivial to get to the meta data. CF makes working w/ XML pretty easy.