Dynamic RSS with XML and Coldfusion

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">
Who is Participating?
I think that if no points are to be refunded, then I should get credit for my answer.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.