闪存AS3 - 显示一个错误,如果不正确,如果在XML [英] Flash AS3 - Display an error if the XML if incorrect
问题描述
我要创建,加载在其中来自CMS动态生成的一些XML Flash应用程序。我想显示的情况下XML文件的格式不正确的错误。当我测试此与格式不正确的XML,它只是站上罚球线与myXML = XML(myLoader.data);然后就弹了出来。我如何能捕获错误,显示一条消息给用户,但闪存程序继续运行正常。
VAR myXMLURL:的URLRequest =新的URLRequest(XMLFILE);
VAR myLoader:的URLLoader =新的URLLoader(myXMLURL);
myLoader.addEventListener(引发Event.COMPLETE,xmlLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR,xmlFailed);
VAR与myXML:XML;
// - 当XML被加载,这样做
功能xmlLoaded(五:事件):无效
{
与myXML = XML(myLoader.data);
跟踪(XML =+与myXML);
}
// - 如果XML加载失败,这样做
功能xmlFailed(事件:IOErrorEvent):无效
{
errorMsg.text =XML文件无法找到
}
只要把code,可以抛出一个try / catch块中的异常
私有函数xmlLoaded(五:事件):无效
{
尝试
{
与myXML = XML(myLoader.data);
跟踪(XML =+与myXML);
}
赶上(错误:错误)
{
errorMsg.text =XML文件不能被发现。;
}
}
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 formatted 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.";
}
}
这篇关于闪存AS3 - 显示一个错误,如果不正确,如果在XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!