PDA

View Full Version : Array prototype not working as I'd expect..help please



codergirl
February 10th, 2005, 12:53 PM
Hi, I created a prototype function for the Array object. It's supposed to load and parse an XML file, then take all the values from the XML file and convert them to objects, which I then want to push to the array that called the function. However, I can't seem to get it to work. The function itself seems to work, but the push to the array does not. Can anyone take a look at this code and give me hand? (code below)

Thanks!
Sarah

Array.prototype.xmltoArrObj = function( path ) {

var xmlurl:String = "nav/" + path + ".xml";
var attr:String = "";
var val:String = "";
var my_xml = new XML;

my_xml.ignoreWhite = true;
my_xml.onLoad = function() {
if ( success == false ) {
return;
}

// create an array of all the childNodes of the firstChild
thisTree = this.firstChild.childNodes;

for ( var n=0; n<thisTree.length; n++ ) {

// extract attributes
attr = thisTree[n].attributes.id;

// extract nodeValues
val = thisTree[n].childNodes[0].nodeValue;
// trace( "Attr: " + attr );
// trace( "Val: " + val );

// create an object to store the key/value pairs
odata = { id: attr, val: val };
trace ( "Object " + n + " is " + odata.val );

// add the object to the array
this.push(odata);

}
}

my_xml.load( xmlurl );
};

tofuisonmyside
February 11th, 2005, 03:23 AM
hi Sarah,

maybe nothing is wrong except using this function like this as an Array prototype.

your code seems very good, in fact, you do not need to make a proto for Arrays, the only line to be useful in the proto, is this.push(odata)

so my suggest is to get rid of the proto, make an array, and your function xml2arrObj should handle it