XML parsing in AS3
Parsing XML documents in Actionscript 2 was never much fun. It had the flash.xml.XMLDocument class which did your standard XML DOM traversing and manipulation, but it was always a tedious task to do any sort of complex parsing that way. What a developer really wants is to convert the XML to objects, which would allow us to deal with the parsed data using native data structures such as arrays. I found an excellent AS2 solution written by a guy called Shane MacCartney called Flash XML Remoting which did exactly that, and I have used it successfully in Flash 8 projects which deal with XML data returned from web services.
However, AS3 has made things much easier. We now have access to ECMAScript for XML (E4X), a programming language that makes manipulating XML much more intuitive and appealing. Essentially, it means that we can refer to nodes and attributes within the XML using standard object dot notation without any extra effort. For example, the following example looks up the title attribute of the second book in the BookXml document:
var title:String = BookXml.book[2].@title;
Yet another example of why AS3 is a quantum improvement over AS2. Incidentally, this makes pulling XML data from the server a far more profitable exercise in AS3 than before. It used to be the case that JSON was the preferred method of passing data structures back to Flash, as it took no extra effort to parse JSON in AS. Now, both the JSON and XML options are very close in terms of programming effort. Of course, there may be other valid reasons for choosing one over the other.
