While working on my second version of RocFolio, I came across a few errors while trying to load multiple xml files in Flash AS3. When loading the files sometimes Flash would randomly put additional characters at the end of my xml file giving me parsing errors. So I had to figure out a way to cut those out, and I’ll try to explain real quick what I did.
So to load your XML you have to create your load and request variables and then you should create a few event listeners for progress and complete events, like this:
1 2 3 4 5 6 | var loader:URLLoader = new URLLoader(); var requestURL:URLRequest = new URLRequest(xmlfilename.xml); //create progress and complete event listeners for the loader, and load the urlrequest loader.addEventListener(ProgressEvent.PROGRESS, getBytesLoop); loader.addEventListener(Event.COMPLETE, loadXML); |
Now in your loadXML function all you have to do is figure out where the xml file actually ends, and then index that spot and then cut the rest of the string out with the substring() command. Here is how you do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //function loadxml loads the xml and handles assigning to places function loadXML(e:Event):void { var dataString:String = e.target.data; var finalString:String = ""; var endofString:uint; //find out where the end of the xml file is, however your set it up it should be the last closing tag, this one is </settings> endofString = dataString.indexOf("</settings>", 0); //cut out the good part of the xml using substring, then use 0 as your starting point to where you got your endOfString //make sure to add whatever amount you need to be at the end of the your last closing tag, </settings> has 11 characters in it so +11 finalString = dataString.substring(0, endofString+11); //then assign the finalstring to your XML variable try { var theXML:XML = new XML(finalString); } catch (e:TypeError) { //loader.load(requestURL); trace("A TypeError has occurred : \r\t" + e); } //then go on with setting up your XMLLists so you can access the data how you want } |
So hopefully this helps those out there that are getting weird parsing errors when trying to load multiple XML files. I know I tried finding the solution out on the web and couldn’t so that’s why I wrote this, if it helps you out please leave a comment.
Cya











