A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help dynamically populating an associative array

  1. #1
    Senior Member
    Join Date
    Aug 2002
    Location
    Philadelphia, PA
    Posts
    274

    Help dynamically populating an associative array

    I am trying to create an associative array and dynamically populate it with information from an XML file. I have no idea how to make a loop that can achieve this, all my attempts thus far have failed. Here is an example of what I need the array to look like.

    code:

    var selectionData:Array = [
    { title:"Thumb 1", thumb:"../images/thumbs/thumb_01.jpg", lowres:"../images/lowres/lowres_01.png", hires:"../images/hires/hires_01.jpg" },
    { title:"Thumb 2", thumb:"../images/thumbs/thumb_02.jpg", lowres:"../images/lowres/lowres_02.png", hires:"../images/hires/hires_02.jpg" },
    { title:"Thumb 3", thumb:"../images/thumbs/thumb_03.jpg", lowres:"../images/lowres/lowres_03.png", hires:"../images/hires/hires_03.jpg" },
    { title:"Thumb 4", thumb:"../images/thumbs/thumb_04.jpg", lowres:"../images/lowres/lowres_04.png", hires:"../images/hires/hires_04.jpg" },
    { title:"Thumb 5", thumb:"../images/thumbs/thumb_05.jpg", lowres:"../images/lowres/lowres_05.png", hires:"../images/hires/hires_05.jpg" },
    { title:"Thumb 6", thumb:"../images/thumbs/thumb_06.jpg", lowres:"../images/lowres/lowres_06.png", hires:"../images/hires/hires_06.jpg" }
    ];



    Here is what my code looks like to populate a normal array.

    code:

    var image:Array = new Array();
    var rootNode:XMLNode;
    var total:Number;
    var xmlData:XML = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("data/images.xml");

    function loadXML(loaded) {
    if (loaded) {
    rootNode = this.firstChild;
    total = rootNode.childNodes.length;
    for (var i:Number = 0; i < total; i++) {
    image[i] = rootNode.childNodes[i].nodeValue;
    }
    } else {
    trace("Ahhhhhhh crap!");
    }
    }

    Ohhhh jeez.......not again.

  2. #2
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    I used node style instead of attribute style XML since your original example hinted as such

    This XML:

    <items>
    <item>
    <title>Thumb 1</title>
    <thumb>../images/thumbs/thumb_01.jpg</thumb>
    <lowres>../images/lowres/lowres_01.png</lowres>
    <highres>../images/hires/hires_01.jpg</highres>
    </item>
    <item>
    <title>Thumb 2</title>
    <thumb>../images/thumbs/thumb_02.jpg</thumb>
    <lowres>../images/lowres/lowres_02.png</lowres>
    <highres>../images/hires/hires_02.jpg</highres>
    </item>
    <item>
    <title>Thumb 3</title>
    <thumb>../images/thumbs/thumb_03.jpg</thumb>
    <lowres>../images/lowres/lowres_03.png</lowres>
    <highres>../images/hires/hires_03.jpg</highres>
    </item>
    <item>
    <title>Thumb 4</title>
    <thumb>../images/thumbs/thumb_04.jpg</thumb>
    <lowres>../images/lowres/lowres_04.png</lowres>
    <highres>../images/hires/hires_04.jpg</highres>
    </item>
    <item>
    <title>Thumb 5</title>
    <thumb>../images/thumbs/thumb_05.jpg</thumb>
    <lowres>../images/lowres/lowres_05.png</lowres>
    <highres>../images/hires/hires_05.jpg</highres>
    </item>
    <item>
    <title>Thumb 6</title>
    <thumb>../images/thumbs/thumb_06.jpg</thumb>
    <lowres>../images/lowres/lowres_06.png</lowres>
    <highres>../images/hires/hires_06.jpg</highres>
    </item>
    </items>

    This actionscript:

    myXML = new XML();
    myXML.ignoreWhite = true;
    myXML.onLoad = function(success) {
    if (success) {
    var selectionData:Array = new Array();
    mainItem = this.firstChild.childNodes;

    for (var i=0; i<mainItem.length; i++) {
    title=mainItem[i].childNodes[0].childNodes
    thumb=mainItem[i].childNodes[1].childNodes
    lowres=mainItem[i].childNodes[2].childNodes
    hires=mainItem[i].childNodes[3].childNodes
    //push these babies
    selectionData.push({title:title,thumb:thumb,lowres :lowres,hires:hires});
    }}
    //let's check the array
    for (var j=0; j<selectionData.length; j++) {
    trace(selectionData[j].title);
    trace(selectionData[j].thumb);
    trace(selectionData[j].lowres);
    trace(selectionData[j].hires);
    }
    //end check
    };
    myXML.load("quovadimus.xml");

  3. #3
    Senior Member
    Join Date
    Aug 2002
    Location
    Philadelphia, PA
    Posts
    274
    Awesome thanks alot for your help. I took your code and modified it to work with XML attributes (seemed a little bit neater for this particular project). Here's what my final code looked like incase anyone else is ever in need. Thanks again Chris!

    Code:
    var selectionData:Array;
    var total:Number;
    var rootNode:XMLNode;
    var myXML:XML = new XML();
    myXML.ignoreWhite = true;
    myXML.onLoad = populateArray;
    myXML.load("../data/photos_02.xml");
    
    function populateArray(success:Boolean) {
    	if (success) {
    		selectionData = new Array();
    		rootNode = this.firstChild;
    		total = rootNode.childNodes.length;
    		for (var i = 0; i < total; i++) {
    			var thumb:String = rootNode.childNodes[i].attributes.thumb;
    			var lowres:String = rootNode.childNodes[i].attributes.lowres;
    			var hires:String = rootNode.childNodes[i].attributes.hires;
    			selectionData.push({thumb:thumb, lowres:lowres, hires:hires});
    		}
    	}
    	//let's check the array 
    	for(var i in selectionData){
    		for(var j in selectionData[i]){
    			trace(j + ": " + selectionData[i][j]);	
    		}
    		trace(newline);
    	}
    	//end check
    }
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <books>
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    	<photo thumb="../images/thumbs/style_ac_01.jpg" lowres="../images/lowres/style_ac_01.jpg" hires="../images/hires/style_ac_01.jpg" />
    </books>
    Ohhhh jeez.......not 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