Is 'childNodes' array associate in any way? Can it be made so?
My problem with XML is that the childNodes array does not seem to be associative. Is there an attribute you can give nodes to let childNodes work as associative?
For example, another poster listed this XML example:
Code:
<data>
<flower>
<info name="blah" id="001" desc="/gfx/blah.png" />
<info name="blah2" id="002" desc="/gfx/blah2.png" />
</flower>
<animal>
<info name="dog" id="046" desc="/gfx/dog.png" />
</animal>
</data>
Lets assume for this post that our actionscript has an object 'xml' that is an xml object with the above xml loaded into it.
To get to the array of nodes that are children of the flower node, you could do either of the following:
xml.firstChild.firstChild.childNodes
xml.firstChild.childNodes[0].childNodes
BUT if the XML changed in the future and someone added a node <weed></weed> above <flower></flower>, the code to handle the xml would then return the weeds instead of the flowers! This change in the XML would require all the code that deals with it to be gone over with a fine tooth comb and fix everything.
Logically, I would like to be able to get an array of my flowers by:
xml.firstChild.childNodes['flower'].childNodes
Is there a way to do this?
I realize there is the issue with the fact that XML allows sibling tags to have identical nodeNames, but even if the above would just grab the first flower node it would be nice in many situations and certainly make the code more readable.
The only way I know how to do the above would be (syntax may be off)
Code:
for (a in xml.firstChild.childNodes) {
if (xml.firstChild.childNodes[a].nodeName=="flower") {
flowerarray = xml.firstChild.childNodes[a].childNodes
}
}
That just seems SO much more clunky than
xml.firstChild.childNodes['flower'].childNodes
Am I missing something? Heck, I am surprised you cant navigate XML objects like:
myarray = xml.data.flower.childNodes
The whole series of numerically indexed arrays seems to defeat the purpose of using names for the tags. It seems like attributes get overused in XML data by newbies like myself because they are just easier to access. You can use their names and not some number that will change anytime you modify the xml.