PDA

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



jwest25
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>
leader>Someone/leader>
/race>

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

race = new XML();
race.load("race.xml");

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

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?

Thanks,

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

VAYKENT
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....

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

http://www.macromedia.com/exchange/flash/

It is powerful tools for using XML

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

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

jwest25
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.

VAYKENT
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?? ;)

beelineuk
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) {
xml=xml.firstChild;
_level0[xml.nodeName]={};
obj = _level0[xml.nodeName];
}

if (xml.firstChild.nodeType == 1) {
// recurse children
for (i in xml.childNodes) {
node=xml.childNodes[i].nodeName;
obj[node]=new Object();
_level0.xml2Flash(xml.childNodes[i],obj[node]);
}
} else if (xml.firstChild.nodeType == 3) {
// get text node
obj.textNode=xml.firstChild.nodeValue;
}
// 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,

Mike

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]