Dynamic RSS with XML and Coldfusion

Posted on 2006-05-24
Last Modified: 2013-12-20
Hello Experts,

I realize there are many postings already about this, but I have not found my particular answer.
When I save the code below as a .cfm file, it works just fine.  The database is queried each time and the output is produced in an xml fasion.

Most xml news feeds that I see are in .xml format.  When I save the same code in .xml, it does not validate or produce the xml newsfeeds, but rather gives me an error.  How do I get the newsfeed to work in an .xml file but still use coldfusion??  What are my options?


<cfsetting enablecfoutputonly="yes">
<cfset NumberOfFeedItems = 5>

<cfquery name="getNews"
SELECT TOP #NumberOfFeedItems# *
FROM mytable
ORDER BY somefield DESC

<cfset theDatetime = "#dateformat(now(),
"ddd, dd mmm yyyy")# #timeformat(now(),
"HH:mm:ss")# MST">

<cfsetting enablecfoutputonly="yes">
<cfsavecontent variable="theXML">

<cfoutput><?xml version="1.0" encoding="ISO-8859-1" ?>
      <!--  RSS generated by Me on #theDatetime#-->
      <rss version="2.0">
            <title>Me </title>
            <link>my link</link>
            <description>My description</description>
            <copyright>My copyright</copyright>

      <cfloop FROM="1" to = "#numberOfFeedItems#" index="ctr">
              title = replace(getNews.Title[ctr], "<", "&lt;", "ALL");
              body = replace(getNews.body[ctr], "<", "&lt;", "ALL");
              body = replace(body, "&", "&amp;", "ALL");
              summary = replace(getNews.summary, "&", "&amp;", "ALL");
              newsdate = dateformat(getNews.newsdate[ctr], "ddd, dd mmm yyyy");
              time = timeformat(getNews.newsdate[ctr], "HH:mm:ss")&"MST";
                        <!--- <pubDate>#newsdate# #time#</pubDate> --->

<cfcontent type="text/xml">
Question by:jhmoen
    LVL 7

    Expert Comment

    That is a tricky question. If the CFM file is outputting valid XML now, then it should work as an XML feed. Remember that the client (whether a browser or reader) will only get the XML that is output, not the ColdFusion code. If you view the source of the output and it is valid, it should work as-is.

    You could add an association to your web site that defines .XML as a ColdFusion file type, but that is a hack and may cause problems elsewhere.

    LVL 7

    Accepted Solution

    I think that if no points are to be refunded, then I should get credit for my answer.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Network it in WD Red

    There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Go t…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now