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?

CODE:

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

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

<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">
      <channel>
            <title>Me </title>
            <link>my link</link>
            <description>My description</description>
            <language>en-us</language>
            <copyright>My copyright</copyright>
</cfoutput>

      <cfloop FROM="1" to = "#numberOfFeedItems#" index="ctr">
      
            <cfscript>
              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";
            </cfscript>
      
            <cfoutput>
                  <item>
                        <title>#title#</title>        
                        <link>mylink.cfm?ID=#getNews.ID[ctr]#</link>
                        <!--- <pubDate>#newsdate# #time#</pubDate> --->
                  </item>
            </cfoutput>
      </cfloop>
<cfoutput>
</channel>
</rss>
</cfoutput>
</cfsavecontent>


<cfcontent type="text/xml">
<cfoutput>#theXML#</cfoutput>
jhmoenAsked:
Who is Participating?
 
ExpertAdminCommented:
I think that if no points are to be refunded, then I should get credit for my answer.

M@
0
 
ExpertAdminCommented:
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.

M@
0
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.