AS3: Array of objects parsed from XML remains with zero length

Posted by Joel Alejandro on Stack Overflow See other posts from Stack Overflow or by Joel Alejandro
Published on 2010-04-09T23:24:42Z Indexed on 2010/04/10 5:53 UTC
Read the original article Hit count: 422

Filed under:
|
|
|

I'm trying to parse an XML file and create an object array from its data. The data is converted to MediaAlbum classes (class of my own). The XML is parsed correctly and the object is loaded, but when I instantiate it, the Albums array is of zero length, even when the data is loaded correctly.

I don't really know what the problem could be... any hints?

import Nem.Media.*;
var mg:MediaGallery = new MediaGallery("test.xml");
trace(mg.Albums.length);

MediaGallery class:

package Nem.Media
{
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.*;

    public class MediaGallery
    {
        private var jsonGalleryData:Object;
        public var Albums:Array = new Array();

        public function MediaGallery(xmlSource:String)
        {
            var loader:URLLoader = new URLLoader();
            loader.load(new URLRequest(xmlSource));

            loader.addEventListener(Event.COMPLETE, loadGalleries);
        }

        public function loadGalleries(e:Event):void
        {
            var xmlData:XML = new XML(e.target.data);
            var album;

            for each (album in xmlData.albums)
            {
                Albums.push(new MediaAlbum(album.attribute("name"), album.photos));
            }
        }
    }
}

XML test source:

<gallery>
    <albums>
        <album name="Fútbol 9">
            <photos>
                <photo width="600" height="400" filename="foto1.jpg" />
                <photo width="600" height="400" filename="foto2.jpg" />
                <photo width="600" height="400" filename="foto3.jpg" />
            </photos>
        </album>
        <album name="Fútbol 8">
            <photos>
                <photo width="600" height="400" filename="foto4.jpg" />
                <photo width="600" height="400" filename="foto5.jpg" />
                <photo width="600" height="400" filename="foto6.jpg" />
            </photos>
        </album>
    </albums>
</gallery>

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about array