I have created a ColdFusion component for simple creation of RSS Feeds. It seems that the need to create valid RSS 2.0 XML is required in various locations throughout sites and it would be a lot nicer to have a component to hand the creation of the XML. This will make it much easier to upgrade your RSS feeds in the future when new specifications are released. This also lends us the ability to follow the DRY technique, and of course makes our lives a lot easier. Note, this CFC is based off of the RSS 2.0 specifications.
To use rss.cfc is quite simple:
- First, instantiate the object.
- Secondly, pass in the channel data into the init method.
- Third, set each item for the channel using the setItem method.
- Lastly, create the RSS feed by calling the create method
Here is an example script that creates a test feed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22<cfsetting showdebugoutput="false"> <cfscript> rssObj = createobject("component","rss"); channelStr = StructNew(); channelStr.title = "Test"; channelStr.link = 'http://www.brianflove.com'; channelStr.description = 'testing'; channelCategories = ArrayNew(1); catStruct = structNew(); catStruct.category = 'category 1'; catStruct.domain = 'http://www.brianflove.com'; ArrayAppend(channelCategories,catStruct); ArrayAppend(channelCategories,'category 2'); channelStr.category = channelCategories; rss = rssObj.init(channel=channelStr,editor="Brian"); if (isValid('string',rss)){ WriteOutput(rss); }else{ rss.setItem(title="test",link="testing",description="test",category=channelCategories); rss.create(); } </cfscript>
Notice, that you can both pass in each argument into the init and setItem functions to set the value of that property within the channel or item, or you can pass in a struct that contains key-value pairs that match the argument names. This is shown on line 4 through 14, where I am creating the channelStr struct and setting the value of the channel’s title, link, description, and category elements. Also note that categories (for either channels or items) can be set as a single string, an array of strings, or an array of structs containing either just the category key-value or both the category and domain key-value pairs.
To make it easier to use this CFC check out the Component browser output from CF that outlines all 3 methods and their arguments, defaults, return types and hints. Ok, now just download the RSS CFC and give it a try (be sure to remove the txt file extension). If you run into any problems or have any questions, leave me a comment.

No Comments, Comment or Ping
Reply to “rss.cfc for simple rss 2.0 compliant XML”