Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

How do I catch lack of file

What should I do to assign a function to the below xml-loading if the xml-file does not exist.
Some kind of if-statement or???
could it be
If( where != what)
var myXML:XML = new XML();
var XML_URL:String = "example.xml"
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myxmlLoader:URLLoader = new URLLoader(myXMLURL);
myxmlLoader.addEventListener("complete", xmlLoaded);

function xmlLoaded(event:Event):void
{
	myXML = XML(myxmlLoader.data);
}

Open in new window

0
petersego
Asked:
petersego
2 Solutions
 
edchipmanCommented:
You will want to listen for IOErrorEvent.IO_ERROR, when that event is dispatched you can handle things from there.
myxmlLoader.addEventListener("ioError", xmlLoadError);

function xmlLoadError(e:IOErrorEvent):void {
   //do something here
}

Open in new window

0
 
deepanjandasCommented:
On adding to @edchipman, also add a try and ctch to be on safer side that the file loading is in acceptable xml format. Like this one:
var myXML:XML = new XML();
var XML_URL:String = "example.xml"
var myxmlLoader:URLLoader = new URLLoader();
myxmlLoader.addEventListener("complete", xmlLoaded);
myxmlLoader.addEventListener("ioError", xmlLoadError);


function xmlLoaded(event:Event):void
{
        myXML = XML(myxmlLoader.data);
}

function xmlLoadError(e:IOErrorEvent):void {
   //do something here
}

try{
     myxmlLoader.load( myXMLURL );
}catch( e:Error){
     trace(e);
}

Open in new window


Warm Regards
Deepanjan Das
0
 
petersegoAuthor Commented:
Thank you both.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now