简体   繁体   English

如何使用Flex从服务器读取XML数据?

[英]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. 我认为以XML格式从服务器请求数据的最简单方法是拥有一个PHP / JSP / ASP.net页面,该页面实际上基于HTTP GET参数生成XML,并以某种方式从Flex调用/加载该页面。

How exactly can this be achieved using the Flex library classes? 使用Flex库类如何完全实现此目的?

I'd like to add that you can also use mx:HTTPService . 我想补充一点,您也可以使用mx:HTTPService If you specify the returnFormat attribute, you would get an XML document as opposed to simple text: 如果指定returnFormat属性,则将获得XML文档,而不是简单文本:

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

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. 我喜欢添加“ allowCache”,因为Flash / Flex在不希望缓存此类内容时非常糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM