Skip to content

E4X XML and AS3.0

A while ago I did a post on E4X formatted XML, I took it down for some reason, and I can’t find where I put it. So I am re-doing it here.

But first it might be good to read up some tutorials with XML and E4X formatting: http://www.actionscript.org/forums/showthread.php3?t=195916

Today we will be pulling data in from an ATOM RSS feed. Mainly because ATOM RSS feeds are stupidly chuck-full of E4X. (If you can’t tell, I think E4X is moronic)


Here is a small sample of the Atom Feed I am pulling in:

var feed:String = "http://gdata.youtube.com/feeds/api/playlists/";
feed += "4C8F7CFA78063102"; //<--- your playlist id
feed += "?";
feed += "&start-index=1";
feed += "&max-results=3";
feed += "&v=2";
<aaa:feed
gd:etag="W/&quot;C0YFRX47eCp7ImA9WxNUEkQ.&quot;" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005"
xmlns:yt="http://gdata.youtube.com/schemas/2007"
xmlns:aaa="http://www.w3.org/2005/Atom">
 <aaa:entry>
    <aaa:author>
      <aaa:name>TurboTax</aaa:name>
      <aaa:uri>http://gdata.youtube.com/feeds/api/users/turbotax</aaa:uri>
    </aaa:author>
    <gd:comments>
      <gd:feedLink href="http://gdata.youtube.com/feeds/api/videos/-AEYgGUlm7I/comments?v=2" countHint="3"/>
    </gd:comments>
    <media:group>
      <media:category label="Education" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Education</media:category>
      <media:content url="http://www.youtube.com/v/-AEYgGUlm7I?f=playlists&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="75" yt:format="5"/>
      <media:content url="rtsp://rtsp2.youtube.com/CiULENy73wIaHAmymyVlgBgB-BMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="75" yt:format="1"/>
      <media:content url="rtsp://rtsp2.youtube.com/CiULENy73wIaHAmymyVlgBgB-BMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="75" yt:format="6"/>
      <media:credit role="uploader" scheme="urn:youtube">TurboTax</media:credit>
      <media:description type="plain">This video describes the new IRS e-file rules and why you need your adjusted gross income (AGI) in order to e-file with the IRS.  When you e-file, the IRS needs to make sure that you are the one filing your tax return.  The AGI is your Adjusted Gross Income from 2007.  The easiest way to get your AGI is by looking at a copy of your last year's tax return.</media:description>

  </aaa:entry>

Seriously? Everything needed to be associated to a name-space? Ricockulous.
But that is the game they want to play. And yes, there is a TON of other unneeded information that bloats this XML and I did take it out for your sanity. So what I would LIKE to do is remove the Name-Spaces all together…

var xmlSource:String = xml.toXMLString();
xmlSource = xmlSource.replace(/<[^!?]?[^>]+?>/g, removeNamspaces);
xml = XML(xmlSource);
function removeNamspaces(...rest):String
{
    rest[0] = rest[0].replace(/xmlns[^"]+\"[^"]+\"/g, "");
    var attrs:Array = rest[0].match(/\"[^"]*\"/g);
    rest[0] = rest[0].replace(/\"[^"]*\"/g, "%attribute value%");
    rest[0] = rest[0].replace(/(<\/?|\s)\w+\:/g, "$1");
    while (rest[0].indexOf("%attribute value%") > 0)
    {
        rest[0] = rest[0].replace("%attribute value%", attrs.shift());
    }
    return rest[0];
}
trace(xml.toXMLString());

But if I do that it’s going to take even longer to parse…
So I am left with this:

package com.drpunchlogic
{
	// standard events
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.xml.XMLDocument;
	import flash.display.MovieClip;

	public class youTubeXML
	{
// START --------------------------

		// VARS -------------------
		private var _xmlDoc:XMLDocument;
		private var _xmlList:XMLList;
		private var _contentXML:XML;
		private var _xmlLoader:URLLoader;
		private var url:URLRequest;

		// XML ----------------------
		public var 	_xml:Object;
		public var 	_itemArr:Array; 

		// NAMESPACE ----------------
		private var	_media_ns 		: Namespace;
		private var	_openSearch_ns	: Namespace;
		private var	_gd_ns			: Namespace;
		private var	_yt_ns			: Namespace;
		private var	_etag_ns			: Namespace;
		private var	_aaa_ns  			: Namespace;

		private var _str:String;
		private var TempObj:Object;
		private var _container:Object;

		// LAUNCH -----------------
		public function youTubeXML( container:Object, str:String) : void
		{
			trace("[ XML ]" );
			_container = container;
			_str = str;
			init(); // <-- start it up
		}

		/*
		-------------------------------------------------
			INIT
		-------------------------------------------------
		*/
		public function init( ) : void
		{
			trace("[ INIT XML = " + _str + " ] " );
			url = new URLRequest( _str );
			_xmlLoader = new URLLoader(url);
			_xmlLoader.addEventListener("complete", startXML );
		}
		// ==============================================
		/*
		-------------------------------------------------
			START XML
		-------------------------------------------------
		*/
		private function startXML( e:Event ) : void
		{
			try{
				XML.ignoreWhitespace = true;
				_contentXML = XML(_xmlLoader.data);
				_xmlList = new XMLList(_contentXML.toXMLString());
				parseXML();
			} catch( e:Error ) {
				trace( "[ XML ERROR = " + e + " ] " );
			}
		}

		/*
		-------------------------------------------------
			PARSE XML
		-------------------------------------------------
		*/
		private function parseXML() : void
		{
			trace( "[ PARSING ] " );

			// name spaces
			_media_ns		= new Namespace( "http://search.yahoo.com/mrss/" );
			_openSearch_ns	= new Namespace( "http://a9.com/-/spec/opensearch/1.1/" );
			_gd_ns			= new Namespace( "http://schemas.google.com/g/2005" );
			_yt_ns			= new Namespace( "http://gdata.youtube.com/schemas/2007" );
			_etag_ns		= new Namespace( "W/&quot;C0YFRX47eCp7ImA9WxNUEkQ.&quot;" );
			_aaa_ns 		= new Namespace( "http://www.w3.org/2005/Atom");

			// ------------------------------
			_xml 			= {};
			// -- Text
			_xml.id				= _xmlList._aaa_ns::id;
			_xml.updated		= _xmlList._aaa_ns::updated
			_xml.title 			= _xmlList._aaa_ns::title;
			_xml.subtitle 		= _xmlList._aaa_ns::subtitle;
			_xml.logo	 		= _xmlList._aaa_ns::logo;
			_xml.author         = _xmlList._aaa_ns::author._aaa_ns::name.child(0);

			// -- Results
			_xml.numResults		= _xmlList._openSearch_ns::totalResults;
			_xml.numStart		= _xmlList._openSearch_ns::startIndex;
			_xml.numPerPage		= _xmlList._openSearch_ns::itemsPerPage;

			_itemArr = [];
			var i:uint;
			var _length:Number  = _xmlList._aaa_ns::entry.length();
			for( i = 0; i < _length; i++ )
			{
				TempObj 	= {};

TempObj.title	= _xmlList._aaa_ns::entry[i]._media_ns::group._media_ns::title.child(0);
TempObj.thumb 	= _xmlList._aaa_ns::entry[i]._media_ns::group._media_ns::thumbnail[0]['@url'];
TempObj.video	= _xmlList._aaa_ns::entry[i]._media_ns::group._media_ns::content[0]['@url'];
TempObj.desc	= f_trim( _xmlList._aaa_ns::entry[i]._media_ns::group._media_ns::description.child(0) );
TempObj.rating	= Math.floor( Number( _xmlList._aaa_ns::entry[i]._gd_ns::rating['@average'] ) );
TempObj.views	= Number( _xmlList._aaa_ns::entry[i]._yt_ns::statistics['@viewCount'] );

TempObj.date 	= _xmlList._aaa_ns::entry[i]._media_ns::group._yt_ns::duration['@seconds'];
TempObj.time	= _xmlList._aaa_ns::entry[i]._media_ns::group._yt_ns::uploaded.child(0);
TempObj.id		= _xmlList._aaa_ns::entry[i]._media_ns::group._yt_ns::videoid.child(0);

				_itemArr.push( TempObj );
			}
			trace("[ SHOW XML PARCED ]");
			_container.dispatchEvent( new Event( "XMLFIN" ) );
		}
		// remove white space
		public function f_trim( str:String):String
		{
			return( str.replace( /(\t|\n|\s{2,})/g, "") );
		}
// END ----------------------------
	}
}

[/as3]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*