PDA

View Full Version : XML&Actionscript loop


Simbiot
01-18-2003, 03:18 PM
Hi everyone,

I'm trying to alter a piece of actionscript code that handles xml data.
I'm currently using a simple method but I want to expand that.

CURRENT VERSION:
CURRENT DATA
<xml etc.....>
<child title="child1">
<decendant title="decendant1.1.1"></decendant>
<decendant title="decendant1.1.2"></decendant>
</child>
<child title="child1.1">
<decendant title="decendant1.1.1"></decendant>
<decendant title="decendant1.1.2"></decendant>
</child>
</xml>

CURRENT CODE
I'm keeping it basic just to get the idea across. This is a multidimensional function.

for (i=0; i<myXML.firstChild.childNodes.length; i++) {
title= (myXML.firstChild.childNodes[i].attributes.title);
for (j=0;j<myXML[title].childNodes.length;j++){
title = (myXML[title].childNodes[j].attributes.title);
}
}




FUTURE VERSION
FUTURE DATA
<xml etc.....>
<parent title="parent1">
<child title="child1.1">
<decendant title="decendant1.1.1"></decendant>
<decendant title="decendant1.1.2"></decendant>
</child>
<child title="child1.2">
<decendant title="decendant1.2.1"></decendant>
<decendant title="decendant1.2.2"></decendant>
</child>
</parent>
<parent title="parent2">
<child title="child2.1">
<decendant title="decendant2.1.1"></decendant>
<decendant title="decendant2.1.2"></decendant>
</child>
<child title="child2.2">
<decendant title="decendant2.2.1"></decendant>
<decendant title="decendant2.2.2"></decendant>
</child>
</parent>
</xml>

FUTURE CODE

?????? Can anyone help me out??????

jimburton
01-18-2003, 10:47 PM
you need something recursive, so that it doesn't matter what structure the xml has...have a go with this, from sakri:

function recurse_xml_tree(node) {
var a = 0;
var b = node.childNodes.length;
while (a<b) {
trace(node.childNodes[a].nodeName);
if (node.childNodes[a].hasChildNodes() && node.childNodes[a].childNodes[0].nodeType != 3) {
recurse_xml_tree(node.childNodes[a]);
} else {
trace("-----------------> "+node.childNodes[a].firstChild.nodeValue);
}
a++;
}
}

all this does is trace the values but it's easy to adapt to write the values into an array or whatever...

Simbiot
01-20-2003, 09:07 AM
Thanks Jimburton, it works. Now I only need to find a way to make it work with my current code!

Simbiot
01-23-2003, 10:06 PM
Hi everyone,

I'm trying to get this tiny piece of code working. The idea is to get the xml formatted data into flash and start displaying it. I'm using trace() to check if it is working, I do get the pageIndex and i value but the myXML value never shows.
The xml file is generated by php and limits each page with 30 work attributes. WHY you ask, well the xml file is cached each time I update myXML and I rather preformat my xml file using php than doing it realtime in my swf file using actionscript.

My xml file looks like this:
<myXML>
<page>
<work id=\"3\">
<image id=\"10\"></image>
</work>
</page>
<page>
<work id=\"2\">
<image id=\"4\"></image>
</work>
</page>
</myXML>

My actionscript looks like this:

// pageIndex
pageIndex = 0; // default 0 -> get firstChild

// xml loop
for(i=0; i<30; i++){
trace("i:"+i+" xmlValue:"+myXML[pageIndex].childNodes[i].attributes.id);
}


How can I get the work id's and how can I get the image id's?
myXML[pageIndex].childNodes[i].attributes.id doesn't work!!!!

jimburton
01-24-2003, 12:40 PM
to access the first page element use:

myXML.firstChild.childNodes[i].attributes.id - if you have more than one page element (not sure from your post) loop through myXML.childNodes