Using XML Namespaces with E4X and ActionScript 3

| No Comments | No TrackBacks

Here is a simple example demonstrating how to use namespaces to access element nodes using E4X syntax. Create a new Flex Project and paste the code in the main .mxml file:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="vertical"
	creationComplete="onCreationComplete();">
	
	<mx:HTTPService id="latest" url="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/newreleases/limit=10/rss.xml"
		showBusyCursor="true" resultFormat="e4x" result="onResult( event );" />
		
	<mx:Script>
		<![CDATA[
			
			import mx.rpc.events.ResultEvent;
						
			[Bindable]
			public var rockArtists:XMLList;
			
			// Define the namespace used in the rss feed for "itms".  Note that the
			// namespace we define is "items" instead.  These values do not need
			// to be the same, but generally should be for readability purposes.
			namespace items = "http://phobos.apple.com/rss/1.0/modules/itms/";
			
			private function onCreationComplete():void {
				// Load the RSS feed
				latest.send();	
			}
			
			private function onResult( event:ResultEvent ):void {
				var rss:XML = event.result as XML;

				// Filter the feed by the Rock category
				var rock:XMLList = rss.channel.item.(category == "Rock");
				
				// Use "items" namespace prefix (corresponding to the namespace we
				// defined above) to access the artist elements
				//rockArtists = rock.items::artist;
				// Or, alternatively:
				use namespace items;
				rockArtists = rock.artist;
			}
			
		]]>
	</mx:Script>
	
	<mx:Label text="Rock Artists:" />
	<mx:List dataProvider="{rockArtists}" />
	
</mx:Application>

The above code will grab the 10 New Releases from iTunes, filter the results by the Rock category, and display all of the artist names inside of a List.

The key here is that the artist element nodes are created with <itms:artist> in the RSS feed. In the XML, the "itms" namespace is defined as xmlns:itms="http://phobos.apple.com/rss/1.0/modules/itms/". This namespace is re-created in ActionScript using the namespace keyword in ActionScript, but instead of calling it "itms" it is defined as "items". In general, you'd want to keep the prefix the same for both, but for illustration purposes you can see that they're allowed to be different.

Accessing the artist element nodes then is simply a matter of using the namespace via use namespace items and asking for just the artist (without the namespace prefix). Another approach is to omit the use namespace ... code and use the namespace prefix directly: rock.items::artist. In both cases, the namespace to use is the one defined in ActionScript, and not the one defined in the XML file (so "items" instead of "itms").

Special thanks to Peter Hall, mostly just for his dry wit and sarcastic humor (or humour?).

Tags: , , , ,

No TrackBacks

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/105

Leave a comment



About this Entry

This page contains a single entry by darron published on April 6, 2006 8:57 AM.

JSON Tutorial was the previous entry in this blog.

PopUpManager and Cairngorm 2 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02