September 25, 2012 / IST / Language, Web Development.

Moving from ActionScript 2 to ActionScript 3 forces us developers to change some of the habits we have been practicing for quite some time. Recently I had the opportunity to work with XML and ActionScript 3. I soon realized that things had changed – I felt like a n00b as I attempted to parse my XML the “old way”. Yesterday I heard some yells coming from across the room and soon found that I was not the only one who had this issue.

Actionscript 3 actually contains two XML related classes, XML and XMLDocument. From a high level you can look at it like this: XML is XML that is parsed using E4X (new to Actionscript 3 – and very sweet to boot) and XMLDocument is what you were used to using in Flash 8 to a large extent.

Let’s look at how we would parse the following xml in Flash 8:
XML

<animals>
  <animal type="cat">
  <animal type="dog">
  <animal type="fish">
</animal>

AS2

xml = new XML();
xml.ignoreWhite = true;
xml.load("animals.xml");
xml.onLoad = function(){
  trace(xml.firstChild.childNodes[0]);
}

Now let’s take a look at what this would look like in AS3, the example below should trace<animal type=”cat” />:

AS3

var xmlString:URLRequest = new URLRequest("animals.xml");
var xmlLoader:URLLoader = new URLLoader(xmlString);
xmlLoader.addEventListener("complete", init);

function init(event:Event):void{
  var xDoc:XMLDocument = new XMLDocument();
  xDoc.ignoreWhite = true;
  var animalsXML:XML = XML(xmlLoader.data);
  xDoc.parseXML(animalsXML.toXMLString());
  trace(xDoc.firstChild.childNodes[0]);
}

Now this is a bit different. As you can see we load our XML using the new URLRequestand URLLoader methods. Once the XML is loaded it fires the init function. We create a new XMLDocument and tell it to ignoreWhite. We then create a new XML instance. We pass the data coming from the the URLLoader that loaded the XML into our new XMLinstance (animalsXML). We can then safely use our XMLDocument to parse the XML using the parseXML method. Notice we pass the animalsXML variable while performing atoXMLString method on it. As in the AS2 version, this should trace <animal type=”cat” />.

So that is not all so bad once you know what methods you need to use. Most of you should be able to figure out what exactly is going on there. For those of you who want to simplify things – or complicate them, however you look at it – I have created a simple class that will allow you to do the AS3 version like this:

AS3:

var xmlString:URLRequest = new URLRequest("animals.xml");
var xmlLoader:URLLoader = new URLLoader(xmlString);
xmlLoader.addEventListener("complete", init);

function init(event:Event):void{
  var animalsXML:XML = XML(xmlLoader.data);
  var parseXML = new pXML(animalsXML);
  trace(parseXML.xmlDocument().firstChild.childNodes[0]);
}

This uses a simple class called pXML. It looks like this:
AS3:
package{
  import flash.xml.XMLDocument;
  import flash.xml.XMLNode;
  import flash.xml.XMLNodeType;

  public class pXML{
    private var xDoc:XMLDocument;

    function pXML(x:XML){
      xDoc = new XMLDocument();
      xDoc.ignoreWhite = true;
      xDoc.parseXML(x.toXMLString());
    }

    public function xmlDocument():XMLDocument{
     return xDoc; 
    }
  } 
}

Taken From :http://pixelfumes.blogspot.in