I devised the XMLLoader class to avoid the URLLoader bug that I posted on FlaLab.com.
Related links
예전에 FlaLab에 올렸던 URLLoader의 버그를 회피하기 위해서 만든 XMLLoader 클래스 입니다.
참고 링크
私がFlaLab.comに書いたURLLoaderのバーグを回避するためにXMLLoaderクラスを作りました。
参考リンク
http://www.flalab.com/phpBB2/viewtopic.php?t=1791
http://www.flalab.com/phpBB2/viewtopic.php?t=2228
/**
* XMLLoader
*
* XMLLoader class loads and validates an XML file.
* If you loads a text file encoded in the euc-kr encoding via an URLLoader, frequently, the source data is currupted.
* To avoid this problem, parses the loaded data into an XMLDocument,
* then, parses the first node of the XMLDocument into an XML.
* If an error has been occured, reloads the XML data.
*
*
* @author: Han Sanghun (http://hangunsworld.com, hanguns@gmail.com)
* @created: 2008 02 28
* @last modified: 2008 02 29
*/
package com.hangunsworld.as3.net{
import flash.events.*;
import flash.net.*;
import flash.utils.Timer;
import flash.xml.*;
public class XMLLoader{
private var path:String;
private var returnFunction:Function;
private var errorFunction:Function;
private var timer:Timer;
private var xml:XML;
private var xml_ul:URLLoader;
private var xml_ur:URLRequest;
private var xml_uv:URLVariables;
/**
* XMLLoader constructor
*
* @param pPath A path of an XML file
* @param okFunc A function to call when the XML is loaded successfully
* @param errorFunc A function to call when an IOError has occured
*/
public function XMLLoader(pPath:String, okFunc:Function=null, errorFunc:Function=null){
path = pPath;
returnFunction = okFunc;
errorFunction = errorFunc
XML.ignoreWhitespace = true;
XML.ignoreComments = true;
// Initializes NET objects
xml_ul = new URLLoader();
xml_ul.addEventListener (Event.COMPLETE, xmlCompleteListener);
xml_ul.addEventListener (IOErrorEvent.IO_ERROR, errorListener);
xml_ur = new URLRequest();
xml_ur.url = path;
xml_uv = new URLVariables();
// Initializes Timer object
timer = new Timer(50, 1);
timer.addEventListener(TimerEvent.TIMER, timerListener);
timer.start();
}// end constructor
/**
* Timer EventListener
* Loads an XML file after some time
*/
private function timerListener(evt:TimerEvent):void{
loadXML();
}// end timerListener
/**
* Loads an XML file
*/
private function loadXML():void{
if(path.indexOf("?") >= 0){
// If the url already contains parameters, attaches the random seed to the end of the url
xml_ur.url = path + "&randomSeed=" + Math.floor(Math.random() * 1000000);
}else{
// If the url does not contain parameters, uses an URLVariable object
xml_uv.randomSeed = Math.floor(Math.random() * 1000000);
xml_ur.data = xml_uv;
}
xml_ul.load (xml_ur);
}// end loadXML
/**
* XML Complete EventListener
*/
private function xmlCompleteListener(evt:Event):void{
// Sets the defualt parse state to false
var parsed:Boolean = false;
var str:String = evt.target.data;
var xmldoc:XMLDocument = new XMLDocument();
xmldoc.ignoreWhite = true;
try{
// Parses the loaded data into an XMLDocument
xmldoc.parseXML(str);
// Gets a string from the first node of the XMLDocument
str = xmldoc.firstChild.toString();
// Parses the string into an XML
xml = new XML(str);
// If no error has occured, sets the parse stae to true
parsed = true;
}catch(e){
// If an error occured, loads the XML again
timer.start();
}// end try catch
// If the parse process done without errors and the returnFunction is NOT null, returns the XML data
if(parsed){
if(returnFunction != null){
returnFunction(xml);
}
}
}// end xmlCompleteListener
/**
* XML IOError EventListener
*/
private function errorListener(evt:IOErrorEvent):void{
if(errorFunction != null){
errorFunction(evt);
}
}// end xmlErrorListener
}// end class
}// end package
import com.hangunsworld.as3.net.XMLLoader;
var xloader:XMLLoader;
xloader = new XMLLoader(xmlPath, xmlLoaded, xmlErrorListener);
function xmlLoaded(xx:XML):void{
// xml loaded
}
function xmlErrorListener(evt:IOErrorEvent):void{
trace("xml load error");
}