views:

40

answers:

1

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

+2  A: 

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);
Cay
BAM! Just like that.Thanks so much... I wasn't really seeing the fact that I needed to initialize the new XML instance.Much appreciated, Cay.
jml
Along these lines, is there anything like whatIsTypeOf(x) ?
jml
yeah, check the flash.utils package... describeType() will give you an xml with the whole description of the object; there are a few other methods to retrieve class names and such... Also, to check type compatibility the operator "is" is the way to go ;)
Cay