Click to See Complete Forum and Search --> : Getting data from childnodes

06-25-2001, 09:15 AM
Hey guys, here is my problem simplified.

I want to load an XML file and then dump all the tags into variables within Flash. So far I have been able to load the XML file and I can get to the data in the first tag as long as I store it in an attribute.

My question: How do I get the data into a variable if the data stored in the XML tag is not an attribute? And second, how do I move on to the childnodes? So far I've only been able to access the data in the first tag, but not the tags farther down in the hierarchy.

Here is an example:

First the XML: (Had to delete some brackets to get it to display, that's why it looks so screewed up)

race condition="green">
track lap="one">track one/track>

The Code: (This works, but I don't know how to access the <track> tag or the <leader> tag)

race = new XML();

function myOnLoad () {
test = "XML loaded!";
condition = race.firstChild.attributes.condition;

race.onLoad = myOnLoad;

Do I need to load the other tags into an array? I've tried something like:

track = race.firstChild.firstChild.attributes.leader;

Am I close? What about if I don't want to access the attribute but rather the data in the tag itself?

Is that all clear as mud?


[Edited by jwest25 on 06-25-2001 at 09:18 AM]

06-25-2001, 05:26 PM
If you can wait until tonight, I'll be able to post some code for you. Some of Moocks code, plus some of mine....

06-26-2001, 03:11 AM
Maybe you can go here to search for a Exchange named WDDX


It is powerful tools for using XML

06-26-2001, 07:39 PM
Now that I think of it.... just go buy the friggin book!!

ActionScript: The Definitive Guide... It rocks....

06-27-2001, 04:26 PM
I thought the whole point of forums was to learn information quick without having to shell out money to buy a book. That and getting a little extra help after you've already read about how to do it.

In any case, I figured out my problem. Thanks for directing me to the bookstore, your a life-saver.

06-27-2001, 04:52 PM
Yeah - forums are for answers.. as long as the person typing the reponse is willing to type the response... :) The answer is too long... - how's that for a cop-out?? ;)

06-27-2001, 06:54 PM
We seeing as I obviously have nothing better to do right now...

Here's a quick and dirty recursive function that will convert ur XML into a flash object. I haven't allowed for duplicate node names in this example, but it will give you an idea of how to navigate ur XML.

function xml2Flash (xml,obj) {
var i,node;
if (!obj) {
obj = _level0[xml.nodeName];

if (xml.firstChild.nodeType == 1) {
// recurse children
for (i in xml.childNodes) {
obj[node]=new Object();
} else if (xml.firstChild.nodeType == 3) {
// get text node
// extract attributes
for (i in xml.attributes) {obj[i]=xml.attributes[i];}

Ur onload function should call xml2Flash with the XML source as the only parameter (don't pass in an object). u'll need to rename ur source XML object to something other than race, as the function will push the xml into an object called _level0.race which has a conflicting name.

Hope it works ok, u'll prob need to taylor it to ur own needs.

Best of luck,


P.S ActionScript: The Definitive Guide does indeed rock!

Just figured out the code tag thing!
[Edited by beelineuk on 07-04-2001 at 05:19 PM]