Sunday, September 14, 2008

RSS Feed to CF Query

Simple RSS to CF Query
Overview
This quick and easy function converts an RSS feed XML document into a coldfusion query for you to easily loop through and use on your website.
Files Included in this ZIP (download zip file)
rssFeed.xml An example RSS feed for this tutorial
xml.cfc CFC containing the function required
queryDump.html Dump of the resultant CF Query
Usage
1. Use CFHTTP to obtain the XML packet.
2. Then make sure it’s a valid XML packet
3. Simply invoke the CFC method and pass the function the parameters required as listed below:
• xmlPacket: A valid XML packet returned from the RSS Feed
• parentTree: The parent tree item in the RSS XML Feed
• childElement The child item containing the appropriate data required
The data will be returned to you in a CF query with the column names named exactly the same as the XML tree items.
Example
//Get your RSS Data as you normally would<b>

<cfhttp url="http://www.spunkycash.com/rss-thumb/125x150/0/856346"
method="GET"
timeout="20">
// Make sure it’s a valid XML Packet
<cfscript>
xmlContent = Trim(cfhttp.filecontent);
xmlContent = XMLParse(xmlContent);
</cfscript>
// Method call to convert XML into CF query
<cfinvoke component="xml.cfc"
method="xmlToQuery"
xmlPacket="#xmlContent#"
parentTree="rss.channel"
childElement="item"
returnVariable="qXML">
<cfdump var="#qXML#"> </b>

In Action
If you use the exact code above, it will fetch the RSS feed as seen in the included file (rssFeed.xml). It will then return the query as seen in the included file (queryDump.html).

No comments: