MidnightGun
MidnightGun

Reputation: 1006

How to read XML data from a server using Flex?

I believe the simplest way to request data from a server in XML format is to have a PHP/JSP/ASP.net page which actually generates XML based on HTTP GET params, and to somehow call/load this page from Flex.

How exactly can this be achieved using the Flex library classes?

Upvotes: 2

Views: 8575

Answers (3)

Laura
Laura

Reputation: 366

I'd like to add that you can also use mx:HTTPService. If you specify the returnFormat attribute, you would get an XML document as opposed to simple text:

<mx:HTTPService resultFormat="e4x" ..../> or <mx:HTTPService resultFormat="xml" .../>

Upvotes: 2

Raleigh Buckner
Raleigh Buckner

Reputation: 8393

I know you've already found it, but here is some sample code:

public var dataRequest:URLRequest;
public var dataLoader:URLLoader;
public var allowCache:Boolean;

dataLoader = new URLLoader();
dataLoader.addEventListener(Event.COMPLETE, onComplete);
dataLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
dataLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
dataLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
dataLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);

dataRequest = new URLRequest();
dataRequest.url = "xmlfilelocation.xml" + ((this.allowCache) ? "" : "?cachekiller=" + new Date().valueOf());

dataLoader.load(dataRequest);

public function onComplete(event:Event):void{
    trace("onComplete");
}
public function onProgress(event:ProgressEvent):void{
    trace("onProgress");
}
public function onIOError(event:IOErrorEvent):void{
    trace("onIOError");
}
public function onSecurityError(event:SecurityErrorEvent):void{
    trace("onSecurityError");
}
public function onHTTPStatus(event:HTTPStatusEvent):void{
    trace("onHTTPStatus");
}

I like to add the "allowCache" because Flash/Flex is terrible about caching stuff like this when you don't want it to.

Upvotes: 1

Related Questions