XMLObject Update

posted by: seth :: Apr 11th 2006, 10:03

[updated] I'm psyched for the native XML Object in AS3, but I think this AS2 class still has some life to it until we all migrate. Its evolved quite a bit and was my first Open Source Flash contribution.

What's nice about it:
It parses XML childNodes and looks for Twins, ie, identitical nodes. If it's a twin, its an Array. Otherwise it's an Object. I also commented it out really nicely so its easy to read.

It works really well with XRay for visualizing Objects: http://www.osflash.org/xray

here's the class
Spit an RSS feed into it, I used: http://www.odeo.com/profile/Geekinger/rss
Here's the output

Here's how I use it:
public function loadXML(URL):Void{
var scopeUp = this;
var XMLObj = new XML();
XMLObj.ignoreWhite = true;
XMLObj.onLoad = function() {
scopeUp.convertXML(this);
};
XMLObj.load(URL);
}

private function convertXML(xmlObj):Void{
var obj = {};
obj = XMLObject.getObject(xmlObj,false);

//scope down object one level, not necessary, but easier
//change "rss" to whatever your top XML node name is
obj = obj["rss"];

//helpful for extracting Object
findMyProps(obj);
}

private function findMyProps(obj:Object){
//using XRAY works best here to visualize the object structure
_global.tt(obj);

//basic trace
trace("rss.channel.item[0].title: "+obj.channel.item[0].title);
}

Enjoy,
Seth

be the first to comment!