ColdFusion: generating and parsing RSS feeds

      3 Comments on ColdFusion: generating and parsing RSS feeds

One of my current projects at work has an RSS requirement. I’m redesigning the university’s policies website. My customers are concerned that other departments at Purdue have previously copied the content of a policy and placed it on a different site, which is problematic when policies are revised, superseded, or retired.

Implementing an RSS XML file for each policy will allow anyone else to embed a policy in their own site using any web technology while keeping the policies site owners in complete control of the content, no matter where it’s used.

Unfortunately, I had a pretty hard time finding a clear-cut solution for creating XML files dynamically with ColdFusion and SQL. It turns out that it’s not that difficult; it just took a little time to figure it out. One solution involved creating an actual XML file and writing it to the server. This would work for content that never gets updated, but this is a situation where the user needs to be able to implement the RSS in their site and not worry about it again.

The problem is, the XML specification is quite strict. You must be able to pass the file as XML in the header information, which will allow RSS readers (and embedding codes or scripts) to see it and parse it. This is accomplished quite easily through the cfcontent tag. Once you’ve identified the content as XML, you generate the file just like you would a regular HTML-formatted page.

Some quick Googling got me the information I needed on how to create an RSS feed to current specifications – you can check it out on Pete Freitag’s site here.

This is pretty code-heavy, so hit the jump to read the rest.

The code for the feed itself is pretty easy. Since I’m dynamically generating the feed based on what policy is selected, I’m passing the ID of the policy (from the database) via a URL parameter. My link looks something like this: http://www.thesite.com/rss/?id=1. All my code is in index.cfm in a directory called rss on my server.

While you need the basic RSS elements in order for your feed to work with a reader (title, link, description, date, etc.), you can add your own tags for other data. In my case, I’m also going to use tags for the policy number, revision date, issuing office(s), etc.

I also discovered that the first line after the opening cfoutput tag must have the and XML declaration. This annoys my OCD side in regards to how organized my markup is, but if you put the XML declaration on the next line, it adds a blank line at the top of the generated source, which is against the XML specification.

I needed to make sure my XML was properly generated, so I also went ahead and put together a test page that allowed me to output the contents of my feed. It turns out that ColdFusion (starting with at least MX 6, since that’s what we have on our server) has built-in functions for dealing with XML. It, too, is quite simple.

The basic process goes like this:

  1. Get the URL of the feed you want via cfhttp and the GET method.
  2. Put the contents of the feed into a variable.
  3. Turn the content into an XML-formatted array with the XMLParse function that is native to ColdFusion.
  4. Output the content of the XML in any format desired.

The code looks something like this:

I’m only dealing with one item in my feed, since I’m using this for individual policies. If you were parsing a blog feed, for instance, you’d just need to add a for loop around the output section, with the “to” value being the length of the array – ArrayLen(TheXML).

Now. As you can see, the structured array that the XmlParse function in ColdFusion creates is fairly complex – there are many levels to it. I wrote a small cfscript to create a function that I could pass different elements through to create the full XmlContent.rss.channel.item[1].element.xmlText. This makes it much more efficient when coding a page that deals with a lot of calls to your XML array.

Actually using the function is extremely easy. For instance, to get the title of the item in your feed:

None of this code is particularly complex; it’s just that there doesn’t appear to be a whole lot of documentation readily available on how to work with RSS in ColdFusion. Once I found the solution, implementation was fast and surprisingly easy.

3 thoughts on “ColdFusion: generating and parsing RSS feeds

    1. Claire Post author

      Thanks for the tip!

      Yeah…I’ve noticed myself that I’ve mostly been relying on Adobe’s own online CF documentation to figure things out. ColdFusion seems to be more expensive to run than other options (even ASP.NET, although I’d argue that one), so it’s just not as popular. I’m starting to prefer it to PHP, though.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *