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 and enclosures as well as guid info for an item in a feed because I think specifying these 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 enclosures and guid info for an item and removing some non-essential code so as not to overly complicate things:

<cfsetting showdebugoutput="no">

<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].enclosure = ArrayNew(1);
   myStruct.item[1].enclosure[1] = StructNew();
   myStruct.item[1].enclosure[1].url = "http://" & CGI.HTTP_HOST & "enc/myenclosure.mp3";
   myStruct.item[1].enclosure[1].length = 1212;
   myStruct.item[1].enclosure[1].type = "audio/mp3";
   myStruct.item[1].description = StructNew();
   myStruct.item[1].description.value = "The first item in the feed";
   myStruct.item[1].guid = StructNew();
   myStruct.item[1].guid.isPermaLink = "Yes";
   myStruct.item[1].guid.value = "http://" & CGI.HTTP_HOST;
   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].guid = StructNew();
   myStruct.item[2].guid.isPermaLink = "Yes";
   myStruct.item[2].guid.value = "http://" & CGI.HTTP_HOST;
   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.
  • If you're looking for information on the metadata properties for the feed itself, see Ray Camden's blog post on the subject.

Comments
DeeAnn's Gravatar Preach it brotha josh! seriously, please post more. I'm fascinated!
# Posted By DeeAnn | 8/9/08 7:16 PM
Brett's Gravatar Nice post. This is exactly what I was looking for.
# Posted By Brett | 12/11/08 4:01 PM
Josh Adams's Gravatar @Brett--I am glad that my post helped you and I very much appreciate you taking the time to post a comment to let me know that it did help you.
# Posted By Josh Adams | 12/19/08 6:40 PM
Jonathan van Zuijlekom's Gravatar Thanks Josh, this put me in the right direction.
# Posted By Jonathan van Zuijlekom | 10/29/09 4:28 AM
todd sharp's Gravatar Hey Josh - the enclosure bit doesn't seem to work on CF 9 - have you tested it on 9 at all?
# Posted By todd sharp | 12/26/09 2:59 PM
todd sharp's Gravatar nevermind - user error ;)
# Posted By todd sharp | 12/26/09 3:02 PM
Ty Whalin's Gravatar Trying to figure out why you did just not use [currentRow] for the Array(1). It would eliminate the need to place secondary structures. Not sure what the advantage would be by creating it as separate structures for each item.
# Posted By Ty Whalin | 1/11/13 10:44 AM
Josh Adams's Gravatar @Ty: the reason I personally didn't use [currentRow] for Array(1) is stated at the beginning of the post: I started with the example on the Adobe site; if you check out that example, you'll see the same code used there. As to why Adobe wrote the code that way...your guess is as good as mine! Thanks for your suggestion of how to do it more efficiently!
# Posted By Josh Adams | 1/11/13 1:34 PM
Ty Whalin's Gravatar Right on, just seemed to make more sense that way. One thing I thought of though. I am using a cfloop query with the structures placed inside the cffloop. They may have also been just trying to show how to only output two items as shown with [1] or [2] as separate items as well. This seems like it would be appropriate since they were not utilizing a cfloop which would allow for a start and end.

Have been working on a Twitter Feed and a Direct Feed for a content management system I created and wanted to gain complete control of the feeds. Especially since FeedBurner has been said to be going away in the near future. Got about three day's worth of research and coding wrapped up with the cffeed tag. One of the most useful tags yet. Thanks for the response.
# Posted By Ty Whalin | 1/11/13 1:51 PM
Führerschein kaufen's Gravatar What a nice post! I'm so happy to read this. What you wrote was very helpful to me. Thank you. Actually, I run a site similar to yours. If you have time, could you visit my site? Please leave your comments after reading what I wrote. If you do so, I will actively reflect your opinion. I think it will be a great help to run my site. Have a good day.

https://healthandpills.com/

https://cannagreenlifes.com/

https://solidarmss.com/

https://xn--skandinaviskkrkort-p3b.com/ Köpa körkort

https://kobekorekort.com/ Køb kørekort

https://acquistapatentediguida.com/ Comprare patente

https://kjopeforerkort.com/ Kjøp førerkort

https://dokumenteanzeigen.com/ Führerschein kaufen
# Posted By Führerschein kaufen | 11/4/23 7:52 PM
Kjøp førerkort's Gravatar https://solidarmss.com/product/henry-homesteader-9...

https://solidarmss.com/product/mcx-spear/

https://solidarmss.com/product/sig-sauer-mcx-spear...

https://solidarmss.com/product/henry-long-ranger-e...

https://solidarmss.com/product/smith-wesson-mp-fpc...

https://solidarmss.com/product/kimber-kds9c-for-sa...

https://solidarmss.com/product/big-boy-revolver/

https://solidarmss.com/product/marlin-1895-trapper...

https://solidarmss.com/product/p320-spectre-comp-b...

https://solidarmss.com/product/model-336-classic/

https://solidarmss.com/product/romeo-m17/

https://solidarmss.com/product/taurus-th45/

https://solidarmss.com/product/sig-romeo-x/

https://healthandpills.com/product/mdpv-in-vendita...

https://healthandpills.com/product/mefedrone-4-mmc...

https://healthandpills.com/product/capsule-di-nemb...

https://healthandpills.com/product/pentobarbital-s...

https://healthandpills.com/product/polvere-di-anfe...

https://healthandpills.com/product/pillole-di-fent...

https://healthandpills.com/product/fentanil-polver...

https://healthandpills.com/product/percocet-pillol...

https://healthandpills.com/product/pillole-di-ossi...

https://cannagreenlifes.com/product/buy-quebec-gol...

https://cannagreenlifes.com/product/buy-gorilla-co...

https://cannagreenlifes.com/product/poison-og-stra...

https://cannagreenlifes.com/product/sour-bubble-st...

https://cannagreenlifes.com/product-tag/white-runt...

https://cannagreenlifes.com/product-tag/platinum-o...

https://cannagreenlifes.com/product/buy-blue-dream...

https://cannagreenlifes.com/product/buy-purple-lam...

https://cannagreenlifes.com/product/buy-blackwater...

https://k2paperincenseforsale.com/product/buy-magi...

https://k2paperincenseforsale.com/product/buy-lsd-...

https://k2paperincenseforsale.com/product/buy-liqu...

https://k2paperincenseforsale.com/product/buy-chan...

https://k2paperincenseforsale.com/product/k2-infus...
# Posted By Kjøp førerkort | 11/4/23 7:55 PM
Köpa körkort's Gravatar https://acquistapatentediguida.com/2023/04/24/comp...

https://acquistapatentediguida.com/2023/04/24/comp...

https://acquistapatentediguida.com/2023/04/25/comp...

https://acquistapatentediguida.com/2023/04/27/dove...

https://acquistapatentediguida.com/2023/04/28/comp...

https://acquistapatentediguida.com/2023/04/29/comp...

https://acquistapatentediguida.com/2023/04/30/comp...

https://acquistapatentediguida.com/2023/04/30/comp...

https://acquistapatentediguida.com/2023/04/30/comp...

https://acquistapatentediguida.com/2023/05/02/comp...

https://acquistapatentediguida.com/2023/09/29/comp...

https://acquistapatentediguida.com/comprare-patent...

https://dokumenteanzeigen.com/index.php/2023/06/02...

https://dokumenteanzeigen.com/index.php/2023/06/01...

https://dokumenteanzeigen.com/index.php/2023/06/01...

https://dokumenteanzeigen.com/index.php/2023/05/31...

https://dokumenteanzeigen.com/index.php/2023/05/31...

https://dokumenteanzeigen.com/index.php/2023/05/30...

https://dokumenteanzeigen.com/index.php/2023/05/27...

https://dokumenteanzeigen.com/index.php/2023/05/27...

https://dokumenteanzeigen.com/index.php/2023/05/21...

https://dokumenteanzeigen.com/index.php/2023/05/23...

https://dokumenteanzeigen.com/index.php/2023/10/25...

https://kjopeforerkort.com/2023/06/07/kjop-falsk-f...

https://kjopeforerkort.com/2023/06/06/kjop-forerko...

https://kjopeforerkort.com/2023/06/06/kjop-svensk-...

https://kjopeforerkort.com/2023/06/05/forerkort-kl...

https://kjopeforerkort.com/2023/06/05/tester-ved-f...

https://kjopeforerkort.com/2023/06/05/amerikansk-f...

https://kjopeforerkort.com/2023/06/04/bestill-nytt...

https://kjopeforerkort.com/2023/06/04/norsk-forerk...

https://kjopeforerkort.com/2023/06/04/kjope-forerk...

https://kjopeforerkort.com/2023/06/04/kjop-forerko...

https://kobekorekort.com/2023/09/10/sadan-finder-d...

https://kobekorekort.com/2018/04/20/kob-korekort-k...

https://kobekorekort.com/2021/02/07/kob-internatio...

https://kobekorekort.com/2018/06/22/kob-falsk-kore...

https://kobekorekort.com/2015/05/10/kob-nyt-koreko...

https://kobekorekort.com/2019/04/13/kob-korekort-b...

https://xn--skandinaviskkrkort-p3b.com/kop-korkort...

https://xn--skandinaviskkrkort-p3b.com/kop-spanskt...

https://xn--skandinaviskkrkort-p3b.com/kopa-korkor...

https://xn--skandinaviskkrkort-p3b.com/korkortonli...

https://xn--skandinaviskkrkort-p3b.com/kopa-korkor...

https://xn--skandinaviskkrkort-p3b.com/hur-mycket-...

https://xn--skandinaviskkrkort-p3b.com/kopa-korkor...

https://xn--skandinaviskkrkort-p3b.com/kopa-korkor...

https://xn--skandinaviskkrkort-p3b.com/kopa-danskt...

https://xn--skandinaviskkrkort-p3b.com/kop-svenskt...

https://xn--skandinaviskkrkort-p3b.com/kop-norge-k...
# Posted By Köpa körkort | 11/4/23 7:56 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner