开发者

Flash AS3 - Display an error if the XML if incorrect

开发者 https://www.devze.com 2023-01-03 08:08 出处:网络
I\'m creating a flash application which loads in some XML which is generated dynamically from the CMS. I want to display an error in case the XML file isn\'t formatted correctly. When I test this with

I'm creating a flash application which loads in some XML which is generated dynamically from the CMS. I want to display an error in case the XML file isn't formatted correctly. When I test this with incorrectly 开发者_如何学Goformatted XML, it will just get to the line myXML = XML(myLoader.data); and then just bomb out. How can I catch the error, display a message to the user, but the flash program to continue as normal.

var myXMLURL:URLRequest = new URLRequest(XMLfile); 
var myLoader:URLLoader = new URLLoader(myXMLURL); 
myLoader.addEventListener(Event.COMPLETE, xmlLoaded); 
myLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlFailed);
var myXML:XML;

//--when the xml is loaded, do this
function xmlLoaded(e:Event):void 
{ 
    myXML = XML(myLoader.data);
    trace("XML = "+myXML);
}

//--if the xml fails to load, do this
function xmlFailed(event:IOErrorEvent):void
{
    errorMsg.text = "The XML file cannot be found"  
}


Just put the code that could throw an exception inside a try/catch block

private function xmlLoaded(e:Event):void 
{
     try 
     {
         myXML = XML(myLoader.data); 
         trace("XML = "+myXML); 
     }
     catch (error:Error) 
     {
         errorMsg.text = "The XML file cannot be found.";
     }
}
0

精彩评论

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