Creating an RSS Feed Using <cffeed> with a Structure
The Adobe ColdFusion 8 documentation has a ton of great information on the <cffeed> tag, including a nice example of how to create an RSS feed using <cffeed> with a structure. There's no need to rehash that information, but I do want to expound on it to explain how you specify categories for an item in a feed because I think specifying categories for an item in a feed is a little tricky. So what I'm going to do is take the above example as a starting point, adding in the code that demonstrates how to specify categories and removing some non-essential code so as not to overly complicate things:
<cfscript>
/* Create the feed data structure and add the metadata. */
myStruct = StructNew();
myStruct.link = "http://" & CGI.HTTP_HOST & CGI.SCRIPT_NAME;
myStruct.title = "My RSS Feed";
myStruct.description = "A demonstration of <cffeed>";
myStruct.pubDate = Now();
myStruct.version = "rss_2.0";
/* Add the feed items. A more sophisticated application would use dynamic variables
and support varying numbers of items. */
myStruct.item = ArrayNew(1);
myStruct.item[1] = StructNew();
myStruct.item[1].category = ArrayNew(1);
myStruct.item[1].category[1] = StructNew();
myStruct.item[1].category[1].value = "Cat1";
myStruct.item[1].description = StructNew();
myStruct.item[1].description.value = "The first item in the feed";
myStruct.item[1].title = "Item 1";
myStruct.item[2] = StructNew();
myStruct.item[2].category = ArrayNew(1);
myStruct.item[2].category[1] = StructNew();
myStruct.item[2].category[1].value = "Cat1";
myStruct.item[2].category[2] = StructNew();
myStruct.item[2].category[2].value = "Cat2";
myStruct.item[2].description = StructNew();
myStruct.item[2].description.value = "The second item in the feed";
myStruct.item[2].title = "Item 2";
</cfscript>
<cffeed action = "create"
name = "#myStruct#"
xmlVar = "myXML">
<cfcontent type="text/xml" reset="true"><cfoutput>#myXML#</cfoutput>
Please note the following:
- The code was specifically written without external dependencies so that it will work on any ColdFusion 8 installation.
- <cfsetting showdebugoutput="no"> can be a lifesaver when working with <cffeed>--I was tearing my hair out for a bit until I had that head-slapping moment when it dawned on me why I kept being told by the browser that the feed was invalid.
- I consider it good coding practice to always scope variables, even those in the Variables scope. The only reason the above code doesn't use scoped variables is to keep it as similar as possible to the example code referenced at the top of this post.

