开发者

Return Parsed XML Value in Actionscript

I am trying to retrieve an XML file off the internet, parse it, and return a value from it, all from one function using Actionscript 3.0.

Here's my code:

public function getValue(aWord:String):开发者_如何学Cvoid
{    
    var xml:XML;
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
    urlLoader.load(new  URLRequest("SOMEXMLFILE.xml")); // I use the aWord parameter in fetching the xml file


    function onXMLLoaded(e:Event):String{
        xml =  new XML(e.target.data);
        trace(xml);
        return (xml.bestmatch.dictionary.text());
    }
    return ""; //THIS NEEDS TO RETURN THE VALUE FOUND IN THE onXMLLoaded FUNCTION
}

Ideally, the local function "onXMLLoaded" could be assigned to a variable; e.g

var text:String = function onXMLLoaded .....

and then I just return the variable "text". However, the compiler complains when I try doing this. How can I return the value found in the onXMLLoaded function to the caller of the getValue function?

Thanks!


Your var text:String = function onXMLLoaded... idea won't work, because what that statement is doing is assigning the onXMLLoaded function itself (of type Function) to the text variable of type String, which is why it won't compile.

The long story short, is that there's no easy way to make synchronous calls to external sources (e.g. XML, web services) in Actionscript. This is pretty infuriating for people coming from a typical C/Java type development environment at first, myself included. A lot of the typical design patterns that are common in C-type languages just aren't possible in Actionscript. You'll never be able to return the result of your URLLoader call from the same function; that's just how the language is built.

One way around this is to pass in the parameter to your function and create an anonymous function that sets a value on an object containing the text, like so:

public function getValue(aWord:String, textToSetObject:Object):void
{    
    var xml:XML;
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE,
        function (e:Event) : void
        {
            textToSetObject.text = new XML(e.target.data).bestmatch.dictionary.text();
        }
    );
    urlLoader.load(new  URLRequest("SOMEXMLFILE.xml"));        
}

You can then call this function with getValue("string", someObject); and after some time someObject.text will equal the result of your XML call.

The key word is that it's after some time. Since it's an asynchronous call (even if it's a local XML file) you'll never be able to know exactly when the call finishes. If you try to do getValue("string", someObject); trace(someObject.text) you'll most likely get an error, since the call won't be finished when the trace statement runs.

Hopefully that made some sense. Understanding the asynchronous architecture of AS3 is a difficult subject and extremely important. But, in summary: many people have tried long and hard to fit synchronous calls into AS3, and they have all been driven to madness. So, don't do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜