Displaying an RSS Feed with ColdFusion

Technical

In my last post, I talked about a few of the challenges I ran across when consuming an RSS feed using ColdFusion. The whole point of consuming the feed, though, at least in my case, was to display it in some way. That presented a few challenges of its own, although these were all technical in nature and easy to solve once I figured out the problem. The hard part for me was figuring it out. I spent more than a few hours on a couple of these.

Issue I : Valid XML Throws Invalid Errors

I saw this one a lot and it thoroughly kicked my ass a couple of times before I finally learned. What can I say? I'm a little slow sometimes.

In one location, I was simply displaying the feed XML and using CSS to present it nicely. Nothing tricky, it was just a simple preview and I didn't want to spend a lot of time processing the XML content when I could just style the XML directly. Except that it wasn't easy to do because I kept getting invalid XML errors from XML that I knew was valid. It was a valid feed. By definition, that makes it valid XML.

Turns out the processing I was doing to figure out which feed to use and retrieve it was introducing whitespace in front of the XML I was returning. I wasn't doing anything wrong, it's just the way ColdFusion handles these things. It's the same reason there is a lot of white space in the HTML generated by a ColdFusion template. To remove that white space, I used code like this once I had the XML:

The key, of course, is the reset attribute. It clears any output rendered before that which follows the cfcontent tag.

This is the question that I responded to on the CF-Talk mailing list and the question that, at least in part, lead me to blog this stuff.

Issue II : AJaX Errors

Within my interface, I was using AJaX request to return XML that I would process. Except that the request would error every time. I was using ColdFusion to generate the XML and, again, white space was being introduced. This time I wrapped my XML-generation code in a cfproccessingdirective tag and that solved the problem. Since the generated XML wasn't being displayed - at least not on the template that created it, cfcontent wasn't the right answer this time.

Issue III : XSLT Inequalities

When creating XML documents using cfxml, all whitespace is preserved when the toString() method is called on it. I formatted my XML for readability and, because of what makes XML (and code in general readable to me, my XML looked like this:

Because the whitespace was preserved, my value included leading and trailing spaces. I couldn't use trim() because I wasn't using ColdFusion to present the XML. Instead, I was using XSLT (feeds are, after all, XML). I could, however, use XSLT's own function to remove leading and trailing white space, normalize-space(). So I did. And that cleared the problem right up. In fact, it did such a good job that I started using it for all of my XSLT string comparisons just to head off any future problems.

So that's it. Those are the most frustrating concerns and problems I can remember encountering while consuming and displaying RSS feeds using ColdFusion. They're not all huge problems and none were revolutionary, but they did cause me some trouble so maybe they're doing the same for someone else. I hope this will help.

tags:
ColdFusion, Ajax, rss, xml, xslt
Ken said:
 
Thanks very much for the solution to ColdFusion prepending white space. Very helpful.
 
posted 351 days ago
Add Comment Reply to: this comment OR this thread
 
Rob said:
 
Glad it helped, Ken.
 
posted 351 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Rob  Wilkerson