R – as3: xml loads but doesn’t seem to be coerced to native flash xml

actionscript-3apache-flex

I have a class that loads some xml data via php scripting.
I am trying to grab the formatted xml (which i have verified is formatted correctly), and stuff it into a number of variables.
I try:

package utils.php
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;

    public class DirectoryReader extends EventDispatcher
    {
        public var fileList:XMLList;
        public var totalBytes:int;

        private var loader:URLLoader;

        public function DirectoryReader(url:String)
        {           
            var urlreq:URLRequest = new URLRequest(url);
            trace(url);

            urlreq.contentType = "text/xml";
            urlreq.method = URLRequestMethod.POST;

            loader = new URLLoader(urlreq);
            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.load(urlreq); 
        }

        protected function completeHandler(e:Event):void 
        {
                trace("seems to have worked...");
                var loaded:XML = loader.data;
                fileList = loaded.child("filelist").attribute("file");
                trace("file list: " + fileList);

                totalBytes = loaded.child("totalsize");
                trace("total size: " + totalBytes);

                dispatchEvent(new Event("directoryLoaded"));
        }

    }
}

Am I doing anything obvious that is wrong?
Basically I get an error that the loaded variable cannot be propagated correctly as if there is a type mismatch.
FWIW, my class extends EventDispatcher so that I can notify other classes that it has loaded. I am sending in a URL, including php variables ala ? and &.

Thanks,
jml

Best Answer

Actually I think that URLRequest.contentType refers only to the request headers, not the response. And AFAIK, even if you are getting a text/xml response header, Flash will indeed treat it as a String, so you need to create a new XML object using the response string as its "expression" parameter...

I think this would solve your problem:

var loaded:XML = new XML(loader.data);
Related Topic