Have you ever wanted to create your very own RSS Feed? DO you have anything on your site you would like others to have access to? Well, RSS (another form of XML) is easy to implement and can be very beneficial for your site and its users.

I implemented RSS/XML feeds in a few areas of EasyCFM to allow people to syndicate the tutorials, our news and more. This tutorial will guide you through the steps I used and teach you how to build the actual RSS feed.

Let's begin,

The first thing we will do is to strip out WHITESPACE from our page, this is so the pages load faster when generating the RSS feeds (Also, remember that XML must have no preceding SPACES before the <?xml> tag. If it foes, the XML will not be usable. To clear up whitespace, we use the tag below:

<cfsetting enablecfoutputonly="yes">

Next we will connect to our database and get the tutorials (or whatever you want to syndicate):

<CFQUERY NAME="qGetTutorials" datasource="MyDSN">
   SELECT *
   FROM    Tutorials
   WHERE  Tutorial_status = <cfqueryparam cfsqltype="CF_SQL_INTEGER" null="no" value="1">
   ORDER   BY tutorial_id
</CFQUERY>

<cfsavecontent variable="theXML">
<cfoutput>
<?xml version="1.0" encoding="ISO-8859-1" ?>
  <!-- RSS generated by EasyCFM.COM, LLC. on #now()# -->
  <rss version="2.0">
    <channel>
       <title>
EasyCFM.COM Tutorials</title>
       <link>
http://www.easycfm.com</link>
       <description>
All the EasyCFM.COM Tutorials!</description>
       <language>
en-us</language>
       <copyright>
Copyright 2003 EasyCFM.COM, LLC.</copyright>
       <docs>
http://backend.userland.com/rss/</docs>
       <lastBuildDate>
#dateformat(now(), "ddd, dd mmm yyyy")# #timeformat(now(), "HH:mm:ss")# EST</lastBuildDate>
       <image>
            <title>
EasyCFM.COM</title>
            <url>
http://www.easycfm.com/images/logo.gif</url>
            <link>
http://www.easycfm.com</link>
       </image>

</cfoutput>

<cfloop from="1" to="#qGetTutorials.RecordCount#" index="ctr">
    <!--- Here let's clean up and ensure that all values are XML Compliant --->
    <cfscript>

       title = replace(qGetTutorials.tutorial_title[ctr], "<", "&lt;", "ALL");
       
description = replace(qGetTutorials.tutorial_description[ctr], "<", "&lt;", "ALL");
    
   description = replace(description, "&", "&amp;", "ALL");
       description = replace(description, '"', "'", "ALL");
       date = dateformat(qGetTutorials.tutorial_posted_date[ctr], "ddd, dd mmm yyyy");
       time = timeformat(qGetTutorials.tutorial_posted_date[ctr], "HH:mm:ss") & " EST";
       author = replace(qGetTutorials.tutorial_author[ctr], "<", "&lt;", "ALL");
       author_email = replace(qGetTutorials.tutorial_author_email[ctr], "at>", "@", "ALL");
       author_email = replace(author_email, "<", "&lt;", "ALL");
   </cfscript>

   <!--- this is the area your users will really want, these are the actual RSS items.. where you have your news or your content itself ---> 
   <cfoutput>

   
<item>
         <title>#title#</title>
         <description>#description#</description>
         <link>http://tutorial#qGetTutorials.tutorial_id[ctr]#.easycfm.com</link>
         <author>#author_email# (#author#)</author>
         <pubDate>#date# #time#</pubDate>
   </item>

   </cfoutput>
</cfloop>
<cfoutput>

</channel>
</rss>

</cfoutput>
</cfsavecontent>

Now you have the option of load this from this file or you can save it to an actual .XML file, that choice is up to you! What's the difference you ask?

It's in the way you want your users to have access to your data... if you do it with the CFML file then they will be querying your database and generating the latest data every time, but they will be processing databases and COldFusion (which could slow your machine down). If you put it in an XML file, the data will not be the most recent, but no processing will be required at time of call by user. So the choice is up to you!

If you want to write, simply do this:
<cffile action="write" file="#expandPath(".")#\EasyFeed.xml" output="#theXml#">

If you want to process with the CFML file, simply add this:

<cfcontent type="text/xml">
<cfoutput>#theXml#</cfoutput>

You now have your very own RSS Feed... the next to last step in your development is to authenticate and validate your feed. This step is crucial in ensuring that what you have built is actually USABLE :)

To validate your feed, simply go to:
http://www.feedvalidator.org/

Enter the direct URL to your feed and click the submit button... then you will get a full report of problems or a message stating that is is validated and usable.

The final step is to syndicate your site and let the world know where they can get your XML/RSS feed to use.. to achieve this there are many sites out there to syndicate. THe biggest one out there is:

http://www.syndic8.com

Submit your site and you'll be ready to go! Before you know it.. people will start using your content and sending YOU traffic!

Some last minute mentions, if you want to see the generated content (and example of what this HAS generated) you can access the EasyCFM.COM Tutorials RSS Feed, here:
http://www.easycfm.com/syndication/EasyFeed.xml

If you want to then, learn how to process the data this generates, then check out my tutorial titled "Processing XML/RSS feeds with ColdFusion MX".

EIther way, you now know how to generate and parse your XML RSS feeds... Questions? Comments? Let me hear from you!

 


About This Tutorial
Author: Pablo Varando
Skill Level: Advanced 
 
 
 
Platforms Tested: CF5,CFMX,BlueDragon
Total Views: 189,572
Submission Date: January 15, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • How do you get the client machine name in coldfusion? Thankz in advance, Lionheart

  • The feed is working great, but I want to do real-time updates. If I add a document to my Web site at 1:00 p.m., and then another at 1:15 p.m., won't this RSS feed-creator just overwrite the first document? Isn't that a problem for folks whose feed readers aren't continuously grabbing content? I'd really appreciate hearing from anyone who can advise me on this. --diane

  • ...watch out for white space. If you get an XML error (like I did on CF7), remove the and bring all the code up so there are no empty lines in the resulting XML file.

  • dear sir, i am curious if my webpage is written in html, where do i put the rss feed information? can i put it anywhere, or does it have to be in the description, title? thank you, chief9898@msn.com

  • i am new to this rss feeds, i am very good at html coding, i read your article on rss feeds and it is confusing to someone who has no knowledge of this, could you give a sample of such feeds and what it would look like some of the writing is in red color some in blue, which would i enter to create a rss feed? any and all help is greatly appreciated. thank you, chief9898@msn.com

  • Instead of using a replace function within a cfscript (which is missing the >). Why not just use the string function #XMLFormat(string)# for the title, description, etc.. areas instead? It would also seem that if you are syndicating long blocks of content (such as full articles formatted in HTML) it may be wise to parse the data to just an excerpt of 50 words or less. Just a thought.

  • Mind posting some info on how we can use CFSchedule to automatically and routinely update the XML file created by cffile?

  • I never used this tutorial because I thought it would only work in CFMX, when in fact it does work in CF 5.0

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.