Flash AS3 Tutorial – Parse XML using AS3

As many folks reached out to me, I have decided to provide you sample AS3 code to parse an XML file.

I will provide you sample XML file as well as sample code to put into your FLA.

Example – Sample XML file

Save this XML file as “grades.xml” in the same folder where you have your FLA.

<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
    <root>
    <grades>
      <id>007</id>
       <grades1>100</grades1>
       <grades2>90</grades2>
       <grades3>54</grades3>
       <grades4>98</grades4>
       <grades5>70</grades5>
    </grades>
    </root>
    </feed>

Sample AS3 code to parse the above XML file, that you can copy and paste in your flash Actionscript window :

 import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;

    var myXML:XML;
    var xmlLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    xmlLoader.load(new URLRequest("grades.xml"));

    function xmlLoaded(e:Event):void
    {
         myXML = new XML(e.target.data);
         trace(myXML.grades1); 

    }

So we wrote a function xmlLoaded, that parsed our XML content.

1. Read on dGlobalTech : How to create a MovieClip in Flash

2. Read on dGlobalTech : How to link flash file to external webpages using AS3?

3. Read on dGlobalTech : Sample code to validate email address in AS3

If you like this post and helped, please provide us feedback. We’ll make sure to keep them coming your way.