<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2009://34.35824-</id>
  <updated>2009-11-16T15:07:53Z</updated>
  <title>Comments for Flex 101: Consuming A Simple RSS Feed (http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.35824</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=35824" title="Flex 101: Consuming A Simple RSS Feed" />
    <published>2009-04-09T01:38:19Z</published>
    <updated>2009-04-09T18:28:05Z</updated>
    <title>Flex 101: Consuming A Simple RSS Feed</title>
    <summary>In my last &quot;Flex 101&quot; post, we reviewed the basics of consuming data through a web service.   This time around, we are going to review the basics of loading a simple RSS XML feed and displaying the results in a Flex List component.</summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<p>In my <a target="_blank" href="http://www.insideria.com/2009/03/flex-101-accessing-a-web-servi.html">last "Flex 101" post,</a> we reviewed the basics of consuming data through a web service.   This time around, we are going to review the basics of loading a simple RSS XML feed and displaying the results in a Flex List component.

<p>The example below shows what we'll be building in this post.   It is a straight-forward application that consumes an RSS feed from <a href="http://feeds.adobe.com" target="_blank">Adobe Feeds</a>, and displays the results in a Flex <code>List</code> component.   If you single-click on an item in the list, the row in the list will expand and show the RSS item's description.   If you double-click on an item, it will open up the RSS item's link url in a new window.   Take it for a test drive, and then we'll get into how it works...
<br/><br/>


<iframe width="525" height="400" src="http://www.tricedesigns.com/portfolio/simplerss/" ></iframe>

<p>You can see that the application is fairly simple... It is basically just a list that contains data, with some mouse actions applied to.   Let's examine the main application file first.  You can see the code for the main application file below.

<p>
<strong><code>main.mxml</code></strong>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre>&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:<span class="category2">Application</span> 
  xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>" 
  layout="<span class="quote">absolute</span>"
  creationComplete="<span class="quote">httpService.send()</span>"
  <span class="category2">backgroundColor</span>="<span class="quote">#CCCCCC</span>"
  themeColor="<span class="quote">#999999</span>" viewSourceURL="<span class="quote">srcview/index.html</span>"&gt;
  
  &lt;mx:Script&gt;
    &lt;![CDATA[
      <span class="category1">import</span> flash.net.navigateToURL;
      <span class="category1">import</span> mx.events.ListEvent;
      <span class="category1">import</span> mx.controls.Alert;
      <span class="category1">import</span> mx.utils.ObjectUtil;
      <span class="category1">import</span> mx.rpc.events.FaultEvent;
      <span class="category1">import</span> mx.rpc.events.ResultEvent;
      
      namespace rdf = "<span class="quote">http://www.w3.org/1999/02/22-rdf-syntax-ns#</span>";
      use namespace rdf;
      namespace purl = "<span class="quote">http://purl.org/rss/1.0/</span>";
      use namespace purl;
      namespace dc = "<span class="quote">http://purl.org/dc/elements/1.1/</span>";
      use namespace dc;
      
      [Bindable]
      <span class="category1">private</span> <span class="category1">var</span> loading : <span class="category2">Boolean</span> = <span class="category1">true</span>;
      
      <span class="category1">private</span> <span class="category1">function</span> onResult( event : ResultEvent ) : <span class="category1">void</span>
      {
         <span class="category2">list</span>.dataProvider = <span class="category2">XML</span>( event.result )..item;
         loading = <span class="category1">false</span>;
       }
      
      <span class="category1">private</span> <span class="category1">function</span> onFault( event : FaultEvent ) : <span class="category1">void</span>
      {
         Alert.<span class="category2">show</span>( ObjectUtil.<span class="category2">toString</span>( event.fault ), "<span class="quote">Service Error</span>" );
         loading = <span class="category1">false</span>;
       }
      
      <span class="category1">private</span> <span class="category1">function</span> onItemDoubleClick( event : ListEvent ) : <span class="category1">void</span>
      {
         <span class="category1">var</span> <span class="category2">url</span> : <span class="category2">String</span> = event.itemRenderer.<span class="category2">data</span>.link.<span class="category2">toString</span>()
         navigateToURL( <span class="category1">new</span> URLRequest( <span class="category2">url</span> ), "<span class="quote">_blank</span>" );
       }
      
    ]]&gt;
  &lt;/mx:Script&gt;
  
  &lt;mx:HTTPService
    id="<span class="quote">httpService</span>"
    <span class="category2">url</span>="<span class="quote">http://feeds.adobe.com/xml/rss.cfm?query=byMostRecent</span>"
    resultFormat="<span class="quote">e4x</span>"
    result="<span class="quote">onResult( event )</span>" /&gt;
  
  &lt;mx:ApplicationControlBar dock="<span class="quote">true</span>"&gt;
    
    &lt;mx:Text 
      <span class="category2">width</span>="<span class="quote">100%</span>" 
      <span class="category2">text</span>="<span class="quote">Select an item to see the description.   Double-click to open the url in a new window.</span>" 
      <span class="category2">selectable</span>="<span class="quote">false</span>" /&gt;
    
  &lt;/mx:ApplicationControlBar&gt;
  
  &lt;mx:List 
    <span class="category2">width</span>="<span class="quote">100%</span>"
    <span class="category2">height</span>="<span class="quote">100%</span>"
    id="<span class="quote">list</span>"
    variableRowHeight="<span class="quote">true</span>"
    itemRenderer="<span class="quote">ListItemRenderer</span>"
    doubleClickEnabled="<span class="quote">true</span>"
    itemDoubleClick="<span class="quote">onItemDoubleClick(event)</span>" /&gt;
    
  &lt;mx:ProgressBar
    <span class="category2">visible</span>="<span class="quote">{ loading }</span>"
    includeInLayout="<span class="quote">{ loading }</span>"
    indeterminate="<span class="quote">true</span>"
    horizontalCenter="<span class="quote">0</span>"
    verticalCenter="<span class="quote">0</span>"
    labelPlacement="<span class="quote">center</span>" /&gt;
    
&lt;/mx:<span class="category2">Application</span>&gt;</pre>
</code>
 
</div></div> 

<p>The main.mxml file contains a <code>HTTPService</code> that is used to load the RSS feed, a <code>List</code> to display the data, a <code>ProgressBar</code> to show an animation while the RSS feed is loading, and a simple <code>ApplicationControlBar</code> to show instructions how to interact with it.   When the application loads, the <code>httpService.send()</code> method is invoked on the "creationComplete" event.   This tells the <code>HTTPService</code> to request data from the <code>HTTPService</code>'s url property.   When this result is returned to the client, it is handled by the <code>onResult</code> function.    In the <code>onResult</code> function, the list's <code>dataprovider</code> is set, and the <code>loading</code> Boolean value is set to false.   When the <code>loading </code> property is set, it hides the progess bar instance using Flex's data bindings on the <code>visible</code> and <code>includeInLayout</code> properties.   When a user double-clicks on an item in the list, the <code>onItemDoubleClick()</code> function is invoked, which opens the item's URL in a new window.    

<p>One very important thing to note in this example is the usage of namespaces.   The three namespaces that are defined in this code segment allow the values of the RSS feed to be accessed within your application.   These namespaces match those that are in the RSS feed itself.  Without these namespaces, you would not be able to acces the nodes of the XML document.

<p>Now, let's examine the item renderer that is used to show the content of the RSS feed:

<p>
<strong><code>ListItemRenderer.mxml</code></strong>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre>&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:VBox 
  xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>"
  verticalGap="<span class="quote">0</span>"
  horizontalScrollPolicy="<span class="quote">off</span>"
  verticalScrollPolicy="<span class="quote">off</span>"&gt;
  
  &lt;mx:Script&gt;
    &lt;![CDATA[
      <span class="category1">import</span> flash.utils.<span class="category2">setTimeout</span>;
      <span class="category1">import</span> mx.controls.List;
      
      namespace rdf = "<span class="quote">http://www.w3.org/1999/02/22-rdf-syntax-ns#</span>";
      use namespace rdf;
      namespace purl = "<span class="quote">http://purl.org/rss/1.0/</span>";
      use namespace purl;
      namespace dc = "<span class="quote">http://purl.org/dc/elements/1.1/</span>";
      use namespace dc;
      
      [Bindable]
      <span class="category1">private</span> <span class="category1">var</span> <span class="category2">list</span> : List;
      
      override <span class="category1">public</span> <span class="category1">function</span> <span class="category1">set</span> <span class="category2">data</span>(value:<span class="category2">Object</span>):<span class="category1">void</span>
      {
         <span class="category1">super</span>.<span class="category2">data</span> = value;
         <span class="category2">list</span> = owner as List;
       }
    ]]&gt;
  &lt;/mx:Script&gt;
  
  
  &lt;mx:Label 
    <span class="category2">width</span>="<span class="quote">100%</span>"
    fontWeight="<span class="quote">bold</span>"
    fontSize="<span class="quote">14</span>"
    <span class="category2">text</span>="<span class="quote">{ data.title }</span>"
    <span class="category2">selectable</span>="<span class="quote">false</span>"  /&gt;
  
  &lt;mx:Label 
    <span class="category2">width</span>="<span class="quote">100%</span>"
    <span class="category2">text</span>="<span class="quote">subject: { data.subject }</span>"
    <span class="category2">selectable</span>="<span class="quote">false</span>"  /&gt;    
    
  &lt;mx:Label 
    <span class="category2">width</span>="<span class="quote">100%</span>"
    <span class="category2">text</span>="<span class="quote">author: { data.creator }</span>"
    <span class="category2">selectable</span>="<span class="quote">false</span>"  /&gt;
    
  &lt;mx:Text 
    <span class="category2">color</span>="<span class="quote">#333333</span>"
    id="<span class="quote">description</span>"
    <span class="category2">htmlText</span>="<span class="quote">{ data.description }</span>" 
    <span class="category2">width</span>="<span class="quote">{ width }</span>"
    <span class="category2">visible</span>="<span class="quote">{ list.selectedItem == data }</span>"
    includeInLayout="<span class="quote">{ list.selectedItem == data }</span>"
    <span class="category2">selectable</span>="<span class="quote">false</span>"  /&gt;
    
&lt;/mx:VBox&gt;</pre>
</code>
 
</div></div> 

<p>The item renderer used to display the RSS item's information is also very basic.  It consists of a VBox, which will vertically stack it's child components.   Inside, it contains three <code>Label</code> components, and  a <code>Text</code> component.   The labels are used to display the title, categories, and author for each entry in the RSS item using standard data bindings.  The text component is used to display the item's description.  The <code>Text</code> component is used in this instance because it will automatically wrap the description text;  labels are used in the other instances because I did not want the text to wrap to the next line.

<p>The <code>visible</code> and <code>includeInLayout</code> properties on the description text instance control whether or not you can see the description.   You'll notice that these are bound to the selectedItem of the list variable.   The <code>list</code> variable contains a reference to the parent list component, which gets identified by the <code>owner</code> property within the overridden <code>set data</code> function.   Whenever the list's selectedItem property equals the data of the current renderer, the item's description will be displayed, which resizes the renderer to show the full description.

<p>And that's it.   This is all you need to consume and display a RSS feed within your own application!   It can get much more sophisticated than this, but it doesn't always need to be that complicated.

<p><strong>Related Links:</strong>
<p><a href="http://feeds.adobe.com" target="_blank">Adobe Feeds</a><br/>
<a href="http://feeds.adobe.com/RSSMachine.cfm" target="_blank">Adobe Feeds RSS Generator</a>
<br/><br/>


___________________________________<br/>
<strong>Andrew Trice</strong><br/>
Principal Architect<br/>
<a href="http://www.cynergysystems.com" target="_blank">Cynergy Systems<br/>
http://www.cynergysystems.com</a>

]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2057229</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2057229" />
    <title>Comment from Srirangan on 2009-04-08</title>
    <author>
        <name>Srirangan</name>
        <uri>http://srirangan.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://srirangan.net">
        <![CDATA[<p>Looks very nice..</p>]]>
    </content>
    <published>2009-04-09T02:49:12Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2057232</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2057232" />
    <title>Comment from John Gag on 2009-04-08</title>
    <author>
        <name>John Gag</name>
        <uri>http://www.cftips.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.cftips.net">
        <![CDATA[<p>Nice little example. Looks cool</p>]]>
    </content>
    <published>2009-04-09T04:51:54Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2057268</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2057268" />
    <title>Comment from Chris McQueen on 2009-04-09</title>
    <author>
        <name>Chris McQueen</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>The RSS feed I'm using has a namespaced thumbnail, like this: </p>

<p>I've added the yahoo namespace up with all the rest in the main.xml and ListItemRenderer, so that's working, but when I add the mx:Image control and try to display the image, I get nothing.  Any guess what I'm doing wrong?  This is how I've added it:</p>

<p>  
	id="thumbnail"<br />
	source="{ data.thumbnail.@url }"<br />
	width="{ width }" /></p>

<p>Is this a crossdomain issue?  I'm just trying to run the thing on my local box in Flex.</p>]]>
    </content>
    <published>2009-04-09T19:12:59Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2057273</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2057273" />
    <title>Comment from Andrew Trice on 2009-04-09</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>I can't see your XML markup b/c its in HTML format, so it isn't rendererd in the page.  Can you paste it replacing the angle brackets with some other character?</p>]]>
    </content>
    <published>2009-04-09T19:55:19Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2057276</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2057276" />
    <title>Comment from Chris McQueen on 2009-04-09</title>
    <author>
        <name>Chris McQueen</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thanks for trying to help. Here's a screenshot:</p>

<p><a href="http://www.screencast.com/t/rPWnkDDoVL"><a href="http://www.screencast.com/t/rPWnkDDoVL">http://www.screencast.com/t/rPWnkDDoVL</a></a></p>]]>
    </content>
    <published>2009-04-09T20:35:31Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2058262</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2058262" />
    <title>Comment from petekaz on 2009-04-24</title>
    <author>
        <name>petekaz</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Is there an easier way to reference the List item from the ItemRenderer?  It seems somewhat kludgey to set it in this manner.  </p>]]>
    </content>
    <published>2009-04-24T12:42:48Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2058263</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2058263" />
    <title>Comment from Andrew Trice on 2009-04-24</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>You could try binding directly to owner.selectedItem, but I think it gives you compiler warnings about owner not being bindable.   </p>]]>
    </content>
    <published>2009-04-24T12:59:05Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35824-comment:2059622</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35824" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/04/flex-101-consuming-a-simple-rs.html#comment-2059622" />
    <title>Comment from Ryan Canulla on 2009-05-14</title>
    <author>
        <name>Ryan Canulla</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Can you give more info on declaring namespaces? For example, I have </p>

<p>###     result xmlns="http://www.inktomi.com/" </p>

<p>in my RSS feed, and </p>

<p>###     namespace ink = "http://www.inktomi.com/";<br />
###     use namespace ink;</p>

<p>below the imports in my flex app. I made up the ink... Should I not see the xmls="" reference within the debugger?</p>]]>
    </content>
    <published>2009-05-14T15:54:17Z</published>
  </entry>

</feed
