Create A Mini Multiple RSS Reader With Flex 2.0 and ColdFusion 7

Introduction

I recently needed to create a small widget that would allow users to select one of 3 RSS feeds, view the titles and descriptions in each feed, and be able to click on a button to go to the link for an item. Since I had previously read Sas Jacobs excellent article about using Flex and ActionScript 3 to load an RSS feed (available on DMX Zone) and had just completed reading Professional Adobe Flex 2 (Wrox), I understood the basics of using an RSS XML file in Flex. What I wasn't clear on was how to allow the user to select 1 of 3 different RSS feeds. However, the combination of ActionScript 3 and Flex's LinkBar control made it easy.

Example - Flex Widget

You can view an example of the multi RSS Reader Flex widget and right click on the widget to view the source code. Please keep in mind that I had to make this reader small as it will go on a web page that is already bit crowded. The code is extensively commented to assist in understanding what's happening.

Backend

Because of Flash security restrictions, I needed to use a ColdFusion CFC to pull in the RSS XML file. You can view that CFC as part of the source code. You just send the CFC the URL of the RSS XML file and ColdFusion gets it and returns the XML to the Flex application.

User Interface in Flex

Once the XML is returned by the CFC, I create an XMLList object that holds all the item nodes in the XML. Each item node has a title, link, and description child node. I use the title nodes to populate a mx:List control. The user can click on a button to load the link for that title in a new browser window. I also display the description below the mx:List control.

I use a mx:LinkBar control to display the three RSS feed choices. When the application first loads, I create an ArrayCollection that is the data provider for the LinkBar. The ArrayCollection stores generic objects and each object has a label, feed, and newsTitle property. The label property stores the text that is displayed in the LinkBar and the feed property is the RSS feed URL. When the user clicks on one of the LinkBar items, the feed for that RSS is sent to the CFC. When the XML for that feed is returned to the Flex application, the titles in that XML are put into mx:List control.

Important Notes

There is no error handling in this version. So if the RSS XML cannot be loaded by the CFC and returned to Flex, only an Alert box displays. I'll work on adding robust error handling in the next version. Another change is to remove the hard-coded RSS feed information from within the MXML and put it into its own XML file that the application can read in and use as the LinkBar's data provider. That will make it easier to swap in new RSS feeds later.

Lastly, the application will only work with RSS XML that follow a specific formatting: channel node - item node and each item node has a title, description, and link child nodes.

Update

A newer version that uses an XML file to determine which RSS feeds to display in the link bar, additional source code commenting and also has improved error handling in the CFC is available here (with source code).

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
I like whats going on here. But I cannot get it to work. I have all the right stuff I think. When I go to complie the MXML I get 2 errors.

One on this line that says syntax errors:
var request:URLRequest = new URLRequest('feeds.xml');

I have the feed.xml on my server in the right spot.

This line says syntax errors:
navigateToURL(new URLRequest(lstHeadlines.selectedItem.link), '_blank');

What could I be missing?
# Posted By George Smith | 8/22/07 9:25 AM
Ok I figured the above part out. It compiles fine. Im very close. The app runs and when it comes up it says news not available and in the Description: Connection Failure: Status code unavailable.

What am I missing?
Thanks

George
# Posted By George Smith | 8/22/07 10:31 AM
Just so others know -- if you copy and paste from the source code view, you must replace all the ' with '

George - can you send me the RSS URL you're trying to use? The error you're getting indicates a problem with the RSS URL.
# Posted By Bruce | 8/22/07 10:37 AM
Bruce,
Thank for getting back to me.
I tried yours with the same result. but I want to use these:

http://www.winespectator.com/RSS/headlines_rss/0,3...

http://www.just-drinks.com/alerts/rssarticle.aspx
# Posted By George Smith | 8/22/07 10:44 AM
George:

I put the above URLs in my feeds.xml file and ran the news reader and everything worked fine.

Below is the feeds.xml I used:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<channel>
   <item>
      <label>Wine Spectator</label>
      <feed>http://www.winespectator.com/RSS/headlines_rss/0,3...;
    <newsTitle>Wind Spectator (click on a headline)</newsTitle>
   </item>
   <item>
      <label>Just Drinks</label>
      <feed>http://www.just-drinks.com/alerts/rssarticle.aspx&...;
    <newsTitle>Just Drinks (click on a headline)</newsTitle>
   </item>
</channel>
# Posted By Bruce | 8/22/07 11:21 AM
Im thinking I must have something wrong. Its reading the feeds.xml for sure because the label and newsTitle shows up in the app. I cant think of anything that might stop the feed. like a mapping in CF or maybe my crossdomain file is in the wrong place. This will be so cool if I can get it to work.

Thanks for your help, anyother thoughts?
George
# Posted By George Smith | 8/22/07 11:28 AM
George - in function handleResult add this line:

trace( xmlResult.toXMLString() );

after this line:

xmlResult = new XML(event.result);

Run the newsreader2 flex app in debug mode in FlexBuilder. You should see the XML returned in the console. If you don't see correct XML then test the RSSFeedService CFC separately with your RSS URLs.
# Posted By Bruce | 8/22/07 11:37 AM
George - ALSO check the path to the RSSFeedService CFC. In my code the RSSFeedService CFC is in /flex/newsreader/ directory under my web root.

If you don't have the RSSFeedService in the same place then you need to change the <mx:RemoteObject> tag to specify the correct value for the source attribute.
# Posted By Bruce | 8/22/07 11:42 AM
hum, I get this in the console:

[SWF] /youngsmarket/programs/rss/rss/bin/RSS-debug.swf - 857,535 bytes after decompression
<rss version="2.0">
<channel>
<item>
<title>News Not Available</title>
<link>http://www.winespectator.com/RSS/headlines_rss/0,3...;
<description>Connection Failure: Status code unavailable</description>
</item>
</channel>
</rss>

I do have the RemoteObject pointing to the RSSFeedService, I did rename the mxml to RSS but I dont think that maters. When I try the testrssfeedservice I get pretty much the same as above but in the xml dump format. Im thinking this could be a server setting. my app is here:

http://www.youngsmarket.com/youngsmarket/programs/...

Just a note: I have been working for a year on flex 2 w CF and your site is by far the most informative. Thanks
# Posted By George Smith | 8/22/07 11:59 AM
I think you have a problem connecting the Flex app to the RSSFeedService CFC. In the flex app uncomment this line:

//Alert.show( ObjectUtil.toString(event.fault) );

in function handleError. Function handleError is called automatically if there is a problem with connecting to or getting back data from the CFC. In the production app the method just shows News Not Available.

I'm assuming your web host is using CF 7.0.2 or above and that you've successfully used Flex Remote Objects with CFCs before on that web host.
# Posted By Bruce | 8/22/07 12:05 PM
George:

Try testing the RSSFeedService CFC. Put the following in a CFM page in the same directory as the RSSFeedService CFC. Then run the page. When I do this I get a dump of the XML returned by the RSS.

If you don't get the XML dump then change the code in the getRSSXML of the RSSFeedService cfc to output = true and then in the cfcatch block dump the cfcatch variable. That will help find any problems the CFC is having in using cfhttp to pull in the RSS URL.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...;
<html xmlns="http://www.w3.org/1999/xhtml">;
<head>
<InvalidTag http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test RSSFeedService CFC</title>
</head>

<body>
<cfscript>

   RSSFeedServiceObj = createObject("component","RSSFeedService");
   
   rssXML = RSSFeedServiceObj.getRSSXML('http://www.winespectator.com/RSS/headlines_rss/0,3...');

</cfscript>
</body>

<cfdump var="#rssXML#">
</html>
# Posted By Bruce | 8/22/07 12:18 PM
I am the host, I am running CF 7.0.2 and I have 12 programs that use RO's and CFC. I thought this would be a snap.
I did notice that in the application it runs and says "News Not Available" but if I press "Wine Spectator" and then the Read More button it open up the xml from that site, same if I press "Just Drinks" and press Read More it opens that xml doc.

I just tried the test that you gave me and it dumps the same xml doc as the TestRSSFeedService.cfm

basically it says in the description: Connection Failure: Status code unavailable

Im stumped - server setting?
# Posted By George Smith | 8/22/07 12:45 PM
try this:

If you don't get the XML dump then change the code in the getRSSXML of the RSSFeedService cfc to output = true and then in the cfcatch block dump the cfcatch variable. That will help find any problems the CFC is having in using cfhttp to pull in the RSS URL
# Posted By Bruce | 8/22/07 1:24 PM
Thanks for all the help Bruce, but Im not smart. I have tried all kinds of stuff. Im not sure what I should see when I get the dump. When I do what you say the dump looks exactly like the TestRSSFeedService.cfm. They both just say:
Connection Failure: Status code unavailable
So that does not help me that much. I can tell the Flex is talking to the RSSFeedService.cfc because I can change the title "News Not Available" and I refresh the app and it changes.
It looks like its all hooked together. If I change the feeds.xml then refresh my app in the browser the titles changes; and the Read More button opens a proper xml page from the site I want but its not the article its the xml doc that I think should be showing up in the headline window. What gives?

George
# Posted By George Smith | 8/22/07 2:41 PM
There is something wrong with the cfhttp call in function getRSSXML of the RSSFeedService. The cfhttp doesn't appear to be working correctly on your setup.

Try using the cfhttp tag with one of your URLs in a normal CFM page and then dump cfhttp. See if an exception is generated and if you get any information.
# Posted By Bruce | 8/22/07 2:59 PM
Bruce,
I tried dumping the cfhttp and I get the same error:
Connection Failure: Status code unavailable
I tried a few urls but no go.

Sorry for clogging up your blog. Im just wondering, when I go to the sample app and right click view source I see the filles that are used to put it together; when I look at the feed xml a message pops up saying that the page maynot be displayed correctly. Is this what it should look like?

<?xml version="1.0" encoding="ISO-8859-1" ?>
<channel>
   <item>
      <label>Wine Spectator</label>
      <feed>http://www.winespectator.com/RSS/headlines_rss/0,3...;
    <newsTitle>Wine Spectator (click on a headline)</newsTitle>
   </item>
   <item>
      <label>Just Drinks</label>
      <feed>http://www.just-drinks.com/alerts/rssarticle.aspx&...;
    <newsTitle>Just Drinks (click on a headline)</newsTitle>
   </item>
</channel>

Im thinking maybe some of the content is blocked and I dont have all the properties of the xml nodes. just a wild thought.

Thanks again
George
# Posted By George Smith | 8/22/07 3:11 PM
The problem isn't with the feed.xml (yes what you put in your message is how it should look).

If you tested just using cfhttp on a plain CFM page and got the error then the problem is with your server and cfhttp.

Check out this http://kb.adobe.com/selfservice/viewContent.do?ext...

Adobe tech note.
# Posted By Bruce | 8/22/07 5:26 PM
I figured the above part out. It compiles fine. Im very close. The app runs and when it comes up it says news not available and in the Description: Connection Failure: Status code unavailable.what should I do?
# Posted By Giyontin Kesim | 1/25/08 1:03 PM
Giyontin: That message means there is either a problem with the RSS feed or ColdFusion could not parse the RSS XML file. Try validating the RSS XML file with an RSS validation service.
# Posted By Bruce | 1/25/08 3:32 PM
Hello,

Where is the source code for your app? I can't find it. Thanks!
# Posted By Simon | 5/6/10 9:25 AM
When you are viewing the example application, right cick directly on the yellow background. The select view source
# Posted By Bruce | 5/6/10 9:32 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner