My Account   Contact Us    
  
» HomeProductsSupportDeveloper CommunityCompany
Parsing RSS with CFMX
Author: Pete Freitag
Category: ColdFusion
Usign ColdFusion MX to parse a RSS feed.

Here's some code that will parse RSS 0.91, 0.92 (if well formed), and RSS 2.0 feeds. It results in an query, and uses XPath. It could probably be rewritten to perform better, but here's the first thing I came up with.

<cfhttp url="http://www.cfdev.com/newsrss.cfm" method="get" />
<cfset rss = XMLParse(cfhttp.filecontent)>

<!--- get an array of items --->
<cfset items = XMLSearch(rss, "/rss/channel/item/index.html")>
<cfdump var="#items#">
<cfset rssItems = QueryNew("title,description,link")>

<!--- loop through the array of items --->
<cfloop from="1" to="#ArrayLen(items)#" index="i">
  <cfset row = QueryAddRow(rssItems)>
  <cfset title = XMLSearch(rss, "/rss/channel/item/index.html")>
  <cfif ArrayLen(title)>
    <cfset title = title[1].xmlText>
  <cfelse>
    <cfset title="">
  </cfif>
  <cfset description = XMLSearch(items[i], "/rss/channel/item[#i#]/description")>
  <cfif ArrayLen(description)>
    <cfset description = description[1].xmlText>
  <cfelse>
    <cfset description="">
  </cfif>
  <cfset link = XMLSearch(items[i], "/rss/channel/item[#i#]/link")>
  <cfif ArrayLen(link)>
    <cfset link = link[1].xmlText>
  <cfelse>
    <cfset link="">
  </cfif>
  <!--- add to query --->
  <cfset QuerySetCell(rssItems, "title", title, row)>
  <cfset QuerySetCell(rssItems, "description", description, row)>
  <cfset QuerySetCell(rssItems, "link", link, row)>
</cfloop>

<ul>
<cfoutput query="rssItems">
  <li><a href="#rssItems.link#">#rssItems.title#</a> - #rssItems.description#</li>
</cfoutput>
</ul>



Content Management Powered by ActivEdit  ActivEdit Browser Based WYSIWYG HTML Editor

More Code Samples

Sign up for our newsletter: | Subscribe with RSS: RSS
© ActivSoftware 1999 to 2005 | Privacy Statement