Monday, April 30, 2007

XML changes in FLEX 2.0

I was debugging in an ActionScript 2.0 code using Flex 2.0 SDK before the code was ported to use ActionScript 3.0 (FLEX 2.0). The code uses the wonderful combination of the sendAndLoad method and the onLoad property of the AS2.0 XML class to do communication with an HTTP servlet via XML messages which are sent and received in the HTML body of the requests and responses.
This has been very straight forward:



var login_str:String = "valid xml that contains user info";
var my_xml:XML = new XML(login_str);
var myLoginReply_xml:XML = new XML();
myLoginReply_xml.onLoad = myOnLoad;
my_xml.sendAndLoad("servletURL", myLoginReply_xml);
function myOnLoad(success:Boolean) {
//extract reply message from myLoginReply_xml object
if (success)
{
}
else
{
}


}

The code has been working under FLEX (AS2.0) SDK. However, when running it under FLEX2.0 SDK, I was shocked with the following exception ! : “onLoad property does not exist for object ..” After looking at the FLEX2.0 changes here , it turned out that the XML class has been renamed to XMLDocument and moved under another package (flash.xml) to avoid conflict with the new top-level XML class that implements ECMAScript for XML (E4X).
Not only the class has been moved and renamed, but also it was dramatically changed. For example the sendAndLoad method now does not exist!, and of course the onLoad property that used to set the event handler for the "load" event has been removed (since the new event model of FLEX2.0 now took place, and events could be only set via the addEventListener method).
Now for me to retain the functionality of the above few lines of code, i have to use a combination of 3 classes: URLLoader, URLRequest and of course the new XMLDocument class (The details will be posted in a later post).

The funny thing is that Adobe claims they made the XMLDocument class for backward compatibility with AS2.0 code, horray!

No comments: