Here's XML2AS as an AS2 static class for her pleasure.

class com.stevensacks.data.XML2AS 
{
        public static function parse(n, r) 
        {
                var a, d, k;
                if (r[k=n.nodeName] == null) r = ((a=r[k]=[{}]))[d=0];
                else r = (a=r[k])[d=r[k].push({})-1];
                if (n.hasChildNodes()) {
                        if ((k=n.firstChild.nodeType) == 1) {
                                r.attributes = n.attributes;
                                for (var i in k=n.childNodes)
XML2AS.parse(k[i], r);
                        } else if (k == 3) {
                                a[d] = new
String(n.firstChild.nodeValue);
                                a[d].attributes = n.attributes;
                        }
                }else r.attributes = n.attributes;
        }
}

Usage:
<config>
        <test foo="bar"/>
        <items>
                <item><![CDATA[Some text]]></item>
                <item><![CDATA[More text]]></item>
                <item><![CDATA[Other text]]></item>
        </items>
</config>

var xml:XML = someXML;
var xmlObj:Object = {};
XML2AS.parse(someXML.firstChild, xmlObj);

trace(xmlObj.config[0].test[0].attributes.foo);
-- bar

var nodes:Array = xmlObj.config[0].items[0].item;
var i:Number = nodes.length;
while (i--) {
        trace(nodes[i]);
}
-- Some text
-- More text
-- Other text
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to