-
Array push not working??
I've got the following code loading some xml, creating dynamic arrays and pushing the data into the array...
I pulled some pieces out because it seems like each push overwrites the previous entry and I'm not sure why it's not working?! I posted the xml and code (without the trace statements) below, and also attached the fla and xml files in a zip...if anyone has an idea what I've got wrong that would be GREAT!!!
Code:
var counties:XML = new XML();
counties.ignoreWhite = true;
counties.onLoad = function(success) {
xN = counties.firstChild.firstChild;
do {
currID = xN.attributes.ID;
var countyID:Array = new Array();
_root["arr"+currID] = countyID;
loadLocs();
xN = xN.nextSibling;
} while (xN.firstChild != null);
};
// Load the XML
counties.load("locsName.xml");
loadLocs = function () {
nextN = xN.firstChild.firstChild;
var tmp:Object = new Object();
do {
tmp.Name1 = nextN.childNodes[0].childNodes[0];
_root["arr"+currID].push(tmp);
nextN = nextN.nextSibling;
} while (nextN.firstChild != null);
};
and the xml:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<Counties>
<County Name="Cook" ID="031" eRegion="1">
<Locs>
<Loc>
<Name1>Air Cycle Corporation</Name1>
</Loc>
<Loc>
<Name1>Assistive Technology Exchange Network</Name1>
</Loc>
<Loc>
<Name1>Chicago Coalition for Information Access Community</Name1>
</Loc>
</Locs>
</County>
<County Name="Crawford" ID="033" eRegion="8">
<Locs>
<Loc>
<Name1>Crawford test</Name1>
</Loc>
</Locs>
</County>
<County Name="DuPage" ID="043" eRegion="1">
<Locs>
<Loc>
<Name1>Com2 Computers and Technology</Name1>
</Loc>
<Loc>
<Name1>Fortune Plastic and Metal, Inc.</Name1>
</Loc>
</Locs>
</County>
<Counties>
Thanks!!
-
Try initializing the tmp variable inside the do, while loop...
Code:
counties.load("locsName.xml");
loadLocs = function () {
nextN = xN.firstChild.firstChild;
do {
var tmp:Object = new Object();
tmp.Name1 = nextN.childNodes[0].childNodes[0];
_root["arr" + currID].push(tmp);
nextN = nextN.nextSibling;
} while (nextN.firstChild != null);
};
PS. Did you get the file I emailed to you last night?
-
Yeah, unfortunately, that clears the array out each time (since it creates a new array each time it goes through the loop) and I only end up with the final value instead of each value...
And, yes, I did get the file...THANKS!! I replied to your email and forwarded it on to my client...haven't heard from him yet, which is usually a good thing...
Thanks!!
-
Okay.
This is what I tried...
Code:
var counties:XML = new XML();
counties.ignoreWhite = true;
counties.onLoad = function(success) {
xN = counties.firstChild.firstChild;
do {
currID = xN.attributes.ID;
var countyID:Array = new Array();
_root["arr" + currID] = countyID;
loadLocs();
xN = xN.nextSibling;
} while (xN.firstChild != null);
trace(" ---------------- ")
for ( i in _root["arr031"]){
trace(i + " " + _root["arr031"][i])
for ( j in _root["arr031"][i]){
trace(j + " " + _root["arr031"][i][j])
}
}
trace(" ---------------- ")
trace(_root["arr031"][0].Name1)
};
// Load the XML
counties.load("locsName.xml");
loadLocs = function () {
nextN = xN.firstChild.firstChild;
do {
var tmp:Object = new Object();
tmp.Name1 = nextN.childNodes[0].childNodes[0];
_root["arr" + currID].push(tmp);
nextN = nextN.nextSibling;
} while (nextN.firstChild != null);
};
And it traces out the values, weird?
-
ah, in the first do loop...cool...let me try that...
-
SWEET!! That did it!! Thank you!! I think the key was to declare the object within the second do...while.
Whew! I thought I was going to have to pull all my hair out...
:)
Thanks!!