views:

495

answers:

2

I have a function that loads in some XML that looks like this:

private function onXMLLoad(e:Event):void
      {
       trace(e.target.data);
       hideLoading();
       DataModel.instance.data = XML(e.target.data);
       updateSelections();
       toggleExpand();
      }

If the XML data that is loading is not well formated for example an open tag is not closed. I get an error telling me that XML must be well formatted. I don't really ever plan on loading XML that is not well formatted but in case it ever does happen I would prefer to be able to handle it someway. First of all is there a way to tell if the loaded data is well formated before casting it as XML, and if possible try to fix it?

+1  A: 

AFAIK, there are no official Flex/AS3 libraries that do XML validation and I don't know of any custom ones either.

If you can validate the XML on the server side (assuming that is applicable), that'd probably be your best bet as you'll have many more options for most server side languages.

You can write your own class/function to validate the XML in AS3 prior to loading it into the XML class. Read in the data variable as a String and parse through it, performing the operations necessary to verify that the XML meets your requirements, including matching up tags, and make any corrections needed.

Stiggler
+1  A: 

you could just wrap the cast in try/catch statement:

try {
  DataModel.instance.data = XML(e.target.data);
} catch (err:TypeError) {
  //handle error
}
misterduck