Apr 23, 2008

E4X: A better way to deal with XML.

I'm not writing anything that hasn't already been documented out there before, but I did want to mention one of my favorite things about AS3 - E4X. E4X is ECMAScript for XML. You can read more about it here. http://en.wikipedia.org/wiki/E4X.

XML is implemented natively into ActionScript 3... no more parsing XML files form astrong. You can leverage this to traverse XML files for specific pieces of content without the need of running a for loop or converting the XML to an object or anything. The trick is mastering the syntax.



Daniel got me started on it, but here's a great tutorial on E4x that I highly recommend to learn and experiment with. http://www.partlyhuman.com/blog/roger/as3-e4x-rundown

E4X can look pretty advanced at times... Stephen and I got a good laugh out of this line that I wrote yesterday:


return Constants.CONFIG.scenes.*.child(labs_BASESCENE).scene.(@id == $scene_id)[0].parent();


But it's really not all that bad. And way better than the old firstChild.childNode.childNodes of yore.

Share this Post


                           

Comments


Fredrik     Feb 18, 2009
Looking for the official I HEART E4X T-shirt. I would buy one.

rooster     May 16, 2008
Congratulations on winning your D&AD award for the HBO website

Meinaart     Apr 24, 2008
I really love E4X to. No more childNode, parentNode, firstChild crap. The funny thing is that I learned the basics from the same website that you are linking. Good stuff!

Jesse Freeman     Apr 23, 2008
E4X is now the backbone for a lot of my models. Using E4X to retrieve the xml data inside of it and cache it in a dictionary is the way to go. Thats a really good link BTW...

Jamie Kosoy     Apr 23, 2008
Whoops. All fixed.

Joshua Weatherspoon     Apr 23, 2008
for some reason the link has a
in it and doesn't correctly link. FYI


Speak






Submit »