|
-
Senior Member
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.
Last edited by Alluvian; 07-27-2007 at 12:01 PM.
-
but node- names on the other hand can blow up the whole XML file making it redicules large,- e.g:
PHP Code:
<websites>
<type>
<id>mercedes Benz</id>
<name>a-Klasse</name>
</type>
</websites>
could be easier imo. to read (not just AS <3 wise) with something like:
PHP Code:
<web>
<type id="mercedes Benz" name="a-Klasse" />
</web>
the lucky answer for you however is that AS3 has a different approach on handling XML and you can write something very similar to what you suggested in AS3.
It´s been a while since I touched AS3 again ,- but it was imo. something like this:
PHP Code:
var attributes_name = xml.websites.type[0].id@name;
but in prior flash versions I did the same just like you,- wrote myself some functions that provide me the nodes I need in case of an unpredictable xml strtuctre.
Though personally I don´t like structures that can mixed in order at least not in most scenarios - as usually the xml file rather gives me the amount of elements and I take the order as they are listed in the XML - because that is what I usually want.
-
Senior Member
Interesting change in AS3. I'm using AS2 and flash 8 right now, I don't know anything about AS3 yet, thanks for the response/info!
I think I have decided to go attribute heavy and node light. Less farting around on the AS side, and only a little more annoyance on the PHP side to generate the xml. In the end I don't really care if the php is ugly looking as I should not have to deal with it very often once it does what it is told.
In the end I think I was overthinking it, and getting way too verbose in what I wanted to include in the xml. I had map nodes, and location nodes, and initialization nodes (since this flash will start out differently depending on some url data), etc...
Instead I think I will break it into a few xml files and just request the ones I need when I need them instead of sending a ton of data that the user may not even request.
I went from thinking I would have to do an elaborate loop with loadvars as needed to learning some xml and that ballooned into this insane urge to dump my entire database into the xml file whether I really need that data or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|