A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Loaded XML file into Associative Array?

  1. #1
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282

    Loaded XML file into Associative Array?

    Hi guys,

    I have a question for you. I have loaded XML into flash and everything is great.
    I have an Associative Array that makes a dynamic menu and everything is great.

    What I would like to do is combine the two and have my loaded XML fill up the Array and the dynamic menu.

    Here is the XML portion:

    Actionscript Code:
    //Imports
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;

    //Create XMl variable
    var teamsXML:XML;
    //Create URL Loader
    var xmlLoader:URLLoader = new URLLoader();
    //Tell the URLLoader named XMlLoader to listen for the xml
    //file to finish loading, then run the function xmlLoaded
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    //Tell the XmlLoader to load in a URLRequest with the path to the
    //xml file
    xmlLoader.load(new URLRequest("assets/teams.xml"));

    //This function runs when the xml file is loaded
    function xmlLoaded(evt:Event):void
    {
        //This tells teamsXML to load in the xml and the new XML
        //tells flash to read the file as HTML
        teamsXML = new XML(xmlLoader.data);
        trace(teamsXML.team.@name);
        trace(teamsXML.team.url);
    }

    Here is the Associative Array portion:

    Actionscript Code:
    import flash.display.Sprite;

    var menuContainer:Sprite = new Sprite();

    var menuItems:Array = new Array();

    menuItems.push({item:"Rams", url:"http://www.stlouisrams.com/", order:"4"});
    menuItems.push({item:"Lakers", url:"http://www.nba.com/lakers/", order:"3"});
    menuItems.push({item:"Yankees", url:"http://www.yankees.com/", order:"2"});
    menuItems.push({item:"Devils", url:"http://devils.nhl.com/", order:"1"});
    menuItems.sortOn("order");

    var buttonCount:int = 0;

    for each (var menuProperty in menuItems)
    {
        var miItem:MenuItem = new MenuItem();
        miItem.tLabel.text = menuProperty.item;
        miItem.url = menuProperty.url;
        miItem.y = (miItem.height * buttonCount);
        menuContainer.addChild(miItem);
        buttonCount++;
    }

    addChild(menuContainer);

    Here is the XML file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?processing instructions?>
    <!-- My Comments1 -->
    
    <teams>
        <team name = "St.Louis Rams">
        <url>http://www.stlouisrams.com/</url>
        </team>
        <team name = "LA Lakers">
            <url>http://www.nba.com/lakers/</url>
        </team>
        <team name = "NY Yankees">
            <url>http://www.yankees.com/</url>
        </team>
        <team name = "NJ Devils">
            <url>http://devils.nhl.com/</url>
        </team>  
    </teams>
    Thank you!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There's no associative array there. In fact, there's no associative array in Actionscript. You can use an Array to map strings to values in a dynamic way but that's just because it's a dynamic class. When you do that, you could just replace Array with Object and get the exact same result.

    But you're not doing that. You have an actual array filled with anonymous object literals. Why do you have an 'order' property on your objects, when they are ordered by the sequence you push them in the array?

    So you want to create your MenuItems from your xml, right?
    Code:
    //Create XMl variable
    var teamsXML:XML;
    //Create URL Loader
    var xmlLoader:URLLoader = new URLLoader();
    //Tell the URLLoader named XMlLoader to listen for the xml 
    //file to finish loading, then run the function xmlLoaded
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    //Tell the XmlLoader to load in a URLRequest with the path to the
    //xml file
    xmlLoader.load(new URLRequest("assets/teams.xml"));
    
    //This function runs when the xml file is loaded 
    function xmlLoaded(evt:Event):void
    {
        teamsXML = new XML(xmlLoader.data);
        createMenuFromXML(teamsXML.team);
    }
    
    function createMenuFromXML(teams:XMLList):void{
      var menuContainer:Sprite = new Sprite();
      var buttonCount:int = 0;
      for each (var team:XML in teams){
        var miItem:MenuItem = new MenuItem();
        miItem.tLabel.text = team.@name.toString();
        miItem.url = team.url.toString();
        miItem.y = (miItem.height * buttonCount);
        menuContainer.addChild(miItem);
        buttonCount++;
      }
      addChild(menuContainer);
    }
    Beware that this uses menuContainer as a local variable in createMenuFromXML. If you need to reference it elsewhere then change its scope.

  3. #3
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    Thank you very much for the help. It has been alot of fun transitioning fully to AS3.

    Your example worked perfectly!!

    There's no associative array there. In fact, there's no associative array in Actionscript. You can use an Array to map strings to values in a dynamic way but that's just because it's a dynamic class. When you do that, you could just replace Array with Object and get the exact same result.

    But you're not doing that. You have an actual array filled with anonymous object literals. Why do you have an 'order' property on your objects, when they are ordered by the sequence you push them in the array?
    I had originally used Object instead of Array, but when I ran the .swf the menu items came in alphabetical order. So I switched to Array. They where still in alphabetical order so I used order.

    But I see what you did and it is awesome. Thank you again!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center