My Account   Contact Us    
  
» HomeProductsSupportDeveloper CommunityCompany
Parsing RSS 1.0 with CFMX
Author: Pete Freitag
Category: ColdFusion
Code sample to parse RSS 1.0 with coldfusion.

Parsing RSS 1.0 is a bit trickier with XPath because you have namespaces to deal with. If an item is in the default or empty namespace, you have to prefix the element name with a : to denote this. Thanks to Sean Corfield for pointing this out.

<cfset feedUrl = "http://www.fullasagoog.com/xml/ColdFusionMX.xml">
<cfhttp url="#feedUrl#" method="get" />
<cfset rss = XMLParse(cfhttp.filecontent)>

<!--- get an array of items --->
<cfset items = XMLSearch(rss, "//: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, "//:item[#i#]/:title")>
  <cfif ArrayLen(title)>
    <cfset title = title[1].xmlText>
  <cfelse>
    <cfset title="">
  </cfif>
  <cfset description = XMLSearch(items[i], "//:item[#i#]/:description")>
  <cfif ArrayLen(description)>
    <cfset description = description[1].xmlText>
  <cfelse>
    <cfset description="">
  </cfif>
  <cfset link = XMLSearch(items[i], "//: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