Loading an XML File with URLLoader when HTTPService Doesn't Work

I was trying to load an XML file that lives next to my SWF on a webserver that I run locally.

Example URLs:

I was trying to use HTTPService but I kept getting "URL must be specified with useProxy set to false". Even if I configured my HTTPService.useProxy = false I still got the same error.

I looked around for a resolution but nothing seemed to work, until I stumbled upon the URLLoader. I setup a URLLoader and requested my data, but when I received the Event.COMPLETE event the "event.target.data" property only showed the first line of my XML...I was expecting to cast the "data" to an XML object, but that gave me a null.

A blog post shed some light. Essentially you have to explicitly create a new XML object. Here's a short snippet, keep in mind this snippet only listens for the "COMPLETE" event, in reality you should also listen for the SecurityErrorEvent and IOErrorEvent.

public function getData():void {
  var loader:URLLoader = new URLLoader( new URLRequest('http://localhost/myapp/foo-config.xml' ));
  loader.addEventListener(Event.COMPLETE, onComplete);
}

public function onComplete(event:Event):void {
  // var data:XML = event.target.data as XML;  <--- doesn't work
  var data:XML = new XML(event.target.data);
}

Enjoy!

  • Posted: 2010-01-14 19:30 (Updated: 2010-01-14 19:30)
  • Author: craig
  • Categories: flex
blog comments powered by Disqus