Creating your very own RSS XML Feeds with ColdFusion MX!

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!

 




All ColdFusion Tutorials By Author: Pablo Varando