PDA

View Full Version : custom XML class


nikefido
04-27-2008, 03:17 PM
I have a class that loads some XML and returns a XML List, but I think it's not as efficient as it could be - seems to be too heavy in that I'm including a lot of resources that I don't need.

For instance, to dispatch an event, I found that I had to extend at least a Sprite to avoid errors (even with importing event stuff).

I was wondering if anyone had any ideas on how to better do this?


package _resources._packages._fidoXML {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.*;

public class fidoXML extends Sprite {
var myXML:XML;
var xmlLoader:URLLoader = new URLLoader;
var myXML_List:XMLList;

public function fidoXML(xmlLoc:String):void {
xmlLoader.load(new URLRequest(xmlLoc));
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}

private function onError(event:IOErrorEvent):void {
trace(event.text);
}

private function xmlLoaded(event:Event):void {
myXML = XML(event.target.data);
myXML_List = myXML.children();
dispatchEvent(new Event('xmlLoaded'));
}

public function get fidoXMLList():XMLList {
return myXML_List;
}
}
}

Sleeve
04-27-2008, 03:25 PM
All you need to do to dispatch an event is extend the EventDispatcher class. Unless your object is a Sprite, you're adding bulk by extending the Sprite to gain event dispatching function.

In your case, it looks like you can just extend the URLLoader class. That will allow you to dispatch events and you will have local access to all the methods and properties of the URLLoader.

nikefido
04-27-2008, 05:41 PM
I assume you mean "import" the EventDispath class and then extend the URLLoader.

I tried that only without extending URLLoader. I'll give that variation a try (probably with a few others as well).

Thanks -

(I know that extending Sprite was way overkill :D )

EDIT: that did the trick - without extending URLLoader, (and with importing EventDispatcher) I get an error saying DispatchEvent is an undefined method. However, with extending URLLoader, it works fine.

Thanks (can you give a quick explanation why this works while extending URLLoader? Is it because of what sub-classes URLLoader has?)