Skip to content
March 30, 2011 / Danii Oliver

Migrating from AS3 XML loading to GreenSock XMLLoader

In as AS3 to access XML data you would write this code:

var siteXML:XML = <survey/> ;
XML.ignoreWhitespace = true;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onLoadXML);**
var xmlReq:URLRequest = new URLRequest("xmlFolder/default.xml");
xmlLoader.load(xmlReq);
function onLoadXML(e:Event):void
{
siteXML = new XML(xmlLoader.data);
trace(siteXML);//parses then traces the entire xml
var Name:DataType = siteXML.itemList.question.length();
//parses the xml
}

In GreenSock you’d write this code:

xmlLoader = new XMLLoader("xmlFolder/default.xml", {name:"configDoc", requireWithRoot:this.root,onComplete:completeHandler,estimatedBytes:1400,**});
XML.ignoreWhitespace = true;
LoaderMax.activate([ImageLoader, SWFLoader, DataLoader, MP3Loader]);//optional if using autoLoader nodes
configLoader.load();
function completeHandler(event:LoaderEvent):void {
var siteXML:XML = <survey/> ;
siteXML = new XML(configLoader.content);
trace(siteXML);//parses then traces the entire xml
var Name:DataType = siteXML.itemList.question.length();//parses the xml
}


The great big difference …
method XML(xmlLoader.content) vs. XML(xmlLoader.data);
these methods are what would make you pull your hair out. So, don’t just read this article.

The other major benefit is the GreenSock LoaderMax.activate allows for a reduction in code. It will automatically parse the XML and load the activated nodes with attribute load=”true”

**Note: it is recommended to use progress and error handlers when loading objects, handlers not used here b/c they are outside the scope of this article.

Happy Coding

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: