Home  |  Contact         

Zrinity is the leader in enterprise-class email marketing management solutions and content management solutions for marketing professionals and developers worldwide.
Products Services Solutions Support Resources Partners Company
Java, PHP, CF Code Samples

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")>
<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[#i#]/title")>
  <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>



More Code Samples



  
home products services partners company support contact
Copyright 2006 © Zrinity Inc. All rights reserved.     View our privacy policy         March 14, 2010