Odd TypeErrors When Loading and Parsing XML in AS3

Recently I was running into this issue with three TypeErrors when loading an XML file. The following errors were:

  • TypeError: Error #1088: The markup in the document following the root element must be well-formed.
  • TypeError: Error #1089: Assignment to lists with more than one item is not supported.
  • TypeError: Error #1090: XML parser failure: element is malformed.

The errors that were getting thrown were not consistent and made no sense as the XML file was, in fact, well formed.I did a bit of research and found some interesting things out on the erros. The most common errors were #1088 and #1090, but each time the Flash was published, it hit a little differently. At times it would even parse correctly and may data could be used as desired.

At first I thought it was occuring upon the load of my XML file. I attempted to use
xmlLoader.addEventListener(ErrorEvent.ERROR, xmlError)

However, the error was never being traced within the Loader Error handler. I then did further tests to see when at which point it was actually happening. It was definitely not an IO_Error that was occuring either, for the XML file was being seen and always loaded. The error turned out to be not on the load of the XML, but after it was fully loaded.

So, on the xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded), I found that something was happening when putting the data into the XML Object:

function xmlLoaded(evt:Event):void {
	trace(evt.target.data) // data shows up
	programXML = XML(evt.target.data);
	// error gets thrown here
}

My Solution for Bad Parsing

I thought there was an Asynchronous way to add an event listener to an XML object llike you can with a Loader object to listen for an error, but there is not. It must be done in a synchronous way. In other words you cannot put a listener on an XML Object to listen for an Error Event. It must be done as a synchronous catch event:

function xmlLoaded(evt:Event):void {
    try{
	programXML = XML(evt.target.data);
        setData(programXML);
    } catch (error:TypeError){
        xmlLoader.load(xmlURL);
        //trace("A TypeError has occurred : \r\t" + error);
    }
}

Since it made no sense to me why it was so buggy I decided to reload the XML file and try to parse it into the XML Object until there was no errors. Granted, this could end up being an infinite loop if the XML at some point ended up being malformed. Therefore, it would be a good idea to put a timout or counter on the loop to stop trying to reload the XML file and do something else. I’m not sure this is best practice, but it was a way around this crazy error that would sometimes hit and sometimes run just fine.

Conclusion

Strange things happen when trying to parse a simple XML file. It is a good idea to use a catch in your code, especially when the code must be run in a syncrynous order. When you have a bug, the error is not always where you think it is coming from.

  • scottrockers

    Hey Great post, I have come across this error showing up only sometimes as well. I got it when I started to load two separate xml files. Still don't know what the problem is but at least this makes sure it loads it.

  • GraphNickDesign

    Thanks for the comment Scott. It is such a strange one that I now use a custom class on every XML file that I load. This way it will always catch this error and attempt to re-parse the XML file. I have it timeout after a number of tries so that it doesn't end in an infinite loop if it always throws the error.