Ok, I’m probably last man to the party with this one but I thought I’d share. I can’t remember where I found this bit of code but, it is pretty darn handy! If you want to add your favorite RSS feed to your site and have it update dynamically, then this is for you.
<code>
<cfset maxEntries = 2>
<cfset theURL = “https://www.owenwebs.com/blog/rss.cfm?mode=short”>
<center><u>Latest From the <a href=”https://www.owenwebs.com/blog”>Random Blog</a></u></center>
<br>
<cftry>
<cfhttp url=”#theURL#”>
<cfset xml = xmlParse(cfhttp.filecontent)>
<cfset items = xmlSearch(xml, “//*[local-name() = ‘item’]”)>
<cfloop index=”x” from=”1″ to=”#min(arrayLen(items),maxEntries)#”>
<cfset item = items[x]>
<cfoutput>
<a href=”#item.link.xmlText#”>#item.title.xmlText#</a>
<br>
#item.description.xmlText#
<br>
<hr align=”center” width=”70%” size=”1″ noshade>
</cfoutput>
</cfloop>
<cfcatch>
<cfoutput>
Feed temporarily down.
</cfoutput>
</cfcatch>
</cftry>
</code>
Using cfset we will set the MaxEntries to get and the URL we will get them from. Then it uses cfhttp to goto our URL and parse the xml. Then it loops through to get our max entries.
And, because we use cftry, if there are any errors we can output a message saying the feed is temporarily down.
© 2006 – 2009, Robert Owen. All rights reserved.