I've been having some trouble parsing various types of XML within flash (specifically FeedBurner RSS files and YouTube Data API responses). I'm using a URLLoader
to load a XML file, and upon Event.COMPLETE
creating a new XML object. 75% of the time this work fine, and every now and again I get this type of exception:
TypeError: Error #1085: The element type "link" must be terminated by the matching end-tag "</link>".
We think the problem is that The XML is large, and perhaps the Event.COMPLETE
event is fired before the XML is actually downloaded from the URLLoader
. The only solution we have come up with is to set off a timer upon the Event, and essentially "wait a few seconds" before beginning to parse the data. Surely this can't be the best way to do this.
Is there any surefire way to parse XML within Flash?
Update Sept 2 2008 We have concluded the following, the excption is fired in the code at this point:
data = new XML(mainXMLLoader.data);
// calculate the total number of entries.
for each (var i in data.channel.item){
_totalEntries++;
}
I have placed a try/catch statement around this part, and am currently displaying an error message on screen when it occurs. My question is how would an incomplete file get to this point if the bytesLoaded == bytesTotal
?
I have updated the original question with a status report; I guess another question could be is there a way to determine wether or not an XML
object is properly parsed before accessing the data (in case the error is that my loop counting the number of objects is starting before the XML is actually parsed into the object)?
@Theo: Thanks for the ignoreWhitespace tip. Also, we have determined that the event is called before its ready (We did some tests tracing mainXMLLoader.bytesLoaded + "/" + mainXMLLoader.bytesLoaded