I recently found a ColdFusion script that allows you parse an RSS feed:
http://www.activsoftware.com/code_samples/code.cfm/CodeID/125/ColdFusion/Parsing_RSS_with_CFMX
However, although I was able to get the example to work with one of my own RSS urls, I routinely (in fact, more often than not) get the following error messages when trying to view the feed:
Document root element is missing
When it works, however, it works fairly well: http://64.139.139.245/parse_rss_test.cfm
(this example shows the feed in an auto-scrolling CSS layer)
Anyways, .. I tried coding around the error with cftry/cfcatch blocks, but the errors still occur and get displayed frequently. I have mainly 2 questions that I'd like to have answered:
1. Why does the script (code is below) routinely fail like that? Is there a better way to parse an xml feed than the method I'm using below, .. or a way to code around the errors? If so how? (Sample replacement code would be ideal)
2. Would it be possible for me to reduce the sizes of the images displayed in the feed to, .. say, 50% of their current display size? If so, how? If this is not possible, then woudl it be possible to just remove the images altogether? If so, how?
Thanks in advance,
- Yvan
p.s. My ColdFusion code is below:
----------------------------------------------------------------------------------
<cfhttp url="http://www.xanadb.com/ticker/MSFT+AAPL+AN+BBX+GPI+LEN+NSANY+ODP+OMC+PZZA+SKFB.PK+TGT" method="get" />
<cfset rss = XMLParse(cfhttp.filecontent)>
<cfset items = XMLSearch(rss, "/rss/channel/item")>
<cfset rssItems = QueryNew("title,description,link")>
<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>
<cfset QuerySetCell(rssItems, "title", title, row)>
<cfset QuerySetCell(rssItems, "description", description, row)>
<cfset QuerySetCell(rssItems, "link", link, row)>
</cfloop>
<table cellpadding='2' border='0' cellspacing='0'>
<tr>
<cfoutput query="rssItems">
<td><a href="#rssItems.link#">#rssItems.title#</a> - #rssItems.description#</td>
</cfoutput>
</tr>
</table>
----------------------------------------------------------------------------------
by: PluckaPosted on 2006-02-13 at 15:07:45ID: 15946326
IDEASDesign,
It means that your request for the XML from the cfhttp is not returning valid xml.
You should have a check for XML after your cfhttp
<cfif isXml(cfhttp.filecontent)>
Output Results
<cfelse>
Do something else, etc.
</cfif>
If you want to see what's happening when you request it and it fails, dump the xml after your cfhttp
<cfdump var="#http.filecontent#" />
<cfabort />
This will probably show you, that every so often the request is failing, the dump will show the actual result.
Regards
Plucka