The XMLLoader class has been revised.
기존에 try…catch 구문을 이용해서 만들었던 XMLLoader(XMLLoaderOld로 변경)는 몇 가지 문제점들을 포함하고 있었습니다. Wooyaggo님이 URLStream 객체를 사용하여 만든 XMLLoader 클래스를 보고서 저도 URLStream 객체를 사용하도록 수정해 보았습니다.
그런데 이벤트 관련해서 이런식으로 사용하는 것이 옳은 것인지 모르겠네요. 많은 지적 부탁드립니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | /** * XMLLoader * XMLLoader class loads an external text file and decodes the data with the provided character set. * 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, uses URLStream instead of URLLoader. * * XMLLoader is the modified version of the XMLLoaderOld class. * This code is inspired by Wooyaggo's article(http://wooyaggo.tistory.com/116). * * * @author: Han Sanghun (http://hangunsworld.com, hanguns@gmail.com) * @created: 2008 03 28 * @last modified: 2008 06 06 */ /* Licensed under the MIT License Copyright (c) 2008 Han Sanghun Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. http://hangunsworld.com/classes/com/hangunsworld/net/ */ package com.hangunsworld.net{ import flash.events.*; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLStream; public class XMLLoader{ private var us:URLStream; private var ul:URLLoader; private var ur:URLRequest; private var encode:String; public var data:String = ""; /** * XMLLoader constructor */ public function XMLLoader(){ us = new URLStream(); ul = new URLLoader(); }// end constructor /** * Adds event listeners to the URLLoader object and the URLStream object. */ public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{ ul.addEventListener(type, listener, useCapture, priority, useWeakReference); us.addEventListener(type, agentListener, useCapture, priority, useWeakReference); }// end addEventListener /** * Removes event listeners from the URLLoader object and the URLStream object. */ public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{ ul.removeEventListener(type, listener, useCapture); us.removeEventListener(type, agentListener, useCapture); }// end removeEventListener /** * Loads external data via the URLStream object, * and sets the character set with the provied string. * * @param pUr The URLRequest object. * @param pEncode The string denoting the character set to use to interpret the bytes. [OPTIONAL] */ public function load(pUr:URLRequest, pEncode:String="utf-8"):void{ encode = pEncode; ur = pUr; us.load(ur); }// end load /** * Dispatches events through the URLLoader object. */ private function agentListener(e):void{ if(e.type == Event.COMPLETE){ // If the event's type is Event.COMPLETE, decodes the loaded data using the provided character set. var str:String = us.readMultiByte(us.bytesAvailable, encode); // Puts the decoded data into the URLLoader object. data = str; ul.data = str; } ul.dispatchEvent(e.clone()); }// end agentListener }// end class }// end package |
Usage example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import com.hangunsworld.net.XMLLoader; var xloader:XMLLoader = new XMLLoader(); xloader.addEventListener(Event.COMPLETE, loaded); xloader.addEventListener(IOErrorEvent.IO_ERROR, errored); xloader.addEventListener(ProgressEvent.PROGRESS, progressed); xloader.load(new URLRequest(path), "euc-kr"); function loaded(e:Event):void{ trace("COMPLETE"); var xml:XML = new XML(xloader.data); trace(xml); } function errored(e:IOErrorEvent):void{ trace("IOERROR"); } function progressed(e:ProgressEvent):void{ trace("PROGRESS - " + e.bytesTotal + " : " + e.bytesLoaded); } |
Tags: Character Set, URLStream, XML, XMLLoader



April 4th, 2008 at 3:35 pm
제꺼보다 훨 깔끔하네요^^
April 4th, 2008 at 3:54 pm
코드야 짧아졌지만…
IEventDispatcher를 implements 하지 않고, 저런식으로 이벤트를 마구 굴려먹어도 되는 건지 모르겠네요. 왠지 이렇게 하면 안될 것 같은데 말이죠…
April 10th, 2008 at 5:09 pm
완전히 은닉을 시키기 위해서면 IEventDispatcher 를 구현하는게 맞을텐데
예외처리라는 명목을 위해서는 기본 이벤트를 그대로 전달해주는것도 한 방법이 아닐까 해요.
April 10th, 2008 at 10:17 pm
언제 시간 날 때 IEventDispatcher를 이용한 버전을 연구해 봐야겠군요
August 19th, 2008 at 11:04 pm
좋은 자료 감사합니다. ^ ㅋ
실례가 안된다면 패키지경로를 개인클래스 모음에 포함시켜버려서 사용해도 될런지요.
물론 원문과 함께 출처는 as파일에 남길 생각입니다. ;;
August 20th, 2008 at 12:45 am
네, 자유롭게 가져다가 사용하세요. MIT 라이센스로 사용, 배포 및 수정 등에 제한이 없습니다.
다만, 가능한 저작권과 라이센스 관련 부분만 삭제하지 말아 주세요. ^^
August 25th, 2008 at 3:02 pm
좋은 배움 얻어 갑니다.
August 26th, 2008 at 1:08 pm
도움이 되었다니 다행이네요 ^^
November 7th, 2008 at 1:02 pm
[…] 참고 URL: http://hangunsworld.com/blog/259 […]