Home Tutorials Forums Articles Blogs Movies Library Employment Press

<< Prev 5 |

xml to flash array object

XML.prototype.toObject = function () {
        var $xparse = function (n) {
                var o = new String (n.firstChild.nodeValue), s, i, t
                for (s = (o == "null") ? n.firstChild : n.childNodes[1]; s !=
                null; s = s.nextSibling) {
                        t = s.childNodes.length > 0 ? arguments.callee (s) :
                        new String (s.nodeValue)
                        for (i in s.attributes) t[i] = s.attributes[i]
                        if (o[s.nodeName] != undefined) {
                                if (!(o[s.nodeName] instanceof Array)) o
                                [s.nodeName] = [o[s.nodeName]]
                                o[s.nodeName].push (t) }
                        else o[s.nodeName] = t }
                return o }
        return $xparse (this) }

using the function:

xmlFile = "config.xml";
x = new XML();
x.ignoreWhite = true;
x.onLoad = function(success) {
        if(success) {
                _root.xmlData = this.toObject();
        }
}

example of output:
XML file:

"12">bob
"16">jim

accessing age:
for(i=0;i<_root.xmlData.myxml.person.length;i++) {
        trace(_root.xmlData.myxml.person[i]); //gives bob and jim
        trace(_root.xmlData.myxml.person[i].age); //gives 12 and 16
}

Posted by: Tony Myatt | website http://tony.myatt.com.au
XMLnode.moveNode() - move nodes around easily
//XMLnode.moveNode(num)
//	num - move node up(positive num, towards beginning of parentNode child list)
//		or down(negative num, towards end of of parentNode child list)
XMLnode.prototype.moveNode = function(num){
        var num=int(num);
        var n=this;
        if(num>0){ //move upward num times
                var i=num;
                while(--i>-1){
                        var n=n.moveUp();
                }
        }else if(num<0){ //move downward num times
                var i=num;
                while(++i<1){
                        var n=n.moveDown();
                }
        }
}
//move XMLnode up
XMLnode.prototype.moveUp = function(){
        if(this.previousSibling){ //if not already at top
                this.parentNode.insertBefore(this.cloneNode(true),this.previousSibling); //clone this before node above this
                var n = this.previousSibling.previousSibling; //reference to new moved/cloned node
                this.removeNode(); //remove self
                return n;
        }
}
//move XMLnode down
XMLnode.prototype.moveDown = function(){
        if(this.nextSibling.nextSibling){  //if not at second to bottom or closer
                this.parentNode.insertBefore(this.cloneNode(true),this.nextSibling.nextSibling); //clone this before node 2 below this
                var n = this.nextSibling.nextSibling; //reference to new moved/cloned node
        }else{ //if this at second to last or earlier position
                this.parentNode.appendChild(this.cloneNode(true)); //clone this and put at end
                var n = this.nextSibling.nextSibling; //reference to new moved/cloned node
        }
        this.removeNode(); //remove old node
        return n;
}

Posted by: Aaron Beall | website http://abeall.com

<< Prev 5 |

Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.