How to determine the end of rucursive loops
July 6, 2005 12:01 AM   Subscribe

In Flash how do i determine the end of a bunch of recursive loops through an xml document of unknown size? [mi]

I havn't written the code yet - but i can show the flow:

xml.onLoad=generateMenu(xml);

function generateMenu(xmlParent){
for(x=0;x<xmlParent.childNodes.length;x++){
make menuItem from xmlParent.childNode[x];
if(xmlParent.childNode[x].hasChild){
generateMenu(xmlParent.childNode[x].);
}
}
}

Not knowing the extent of xml - that is how many childrens childrens children etc. - how do i determine when the generateMenu has finished looping in all its instances?
posted by FidelDonson to Technology (8 answers total)
 
Simple: it's done when the original call to generateMenu returns. In your code, the original call is made when xml.LoadOff is called ("triggered").

It's just a recursive tree traversal (in your example, a pre-order traversal).
posted by orthogonality at 1:03 AM on July 6, 2005


Perhaps wrap the generateMenu(xmlParent) function inside another function that looks like

function generateMenuAndNotify(xmlParent)
{
    generateMenu(xmlParent);
    callSomeOtherFunction();
}

where callSomeOtherFunction() is whatever you're looking to trigger when the XML document has been parsed.
posted by chrismear at 1:06 AM on July 6, 2005


In a sense, every node has a child node, even the last one. The last node's child is called null. So just test fot that:

if (thisNode.firstChild != null) { ... continue the loop }
posted by grumblebee at 4:42 AM on July 6, 2005


grumblebee's solution, while elegant, will only detect whether you've reached the end of a tree branch, and will thus trigger once for every leaf node in the tree.

It looks like you're asking for a way to detect the end of the generateXML process when called upon an XML node explicitly; I'd go with the wrapper solution as well. (I've never seen an event handler called loadOff before, but if it exists, orthogonality's solution looks good too.)
posted by chrominance at 6:55 AM on July 6, 2005


if the answer isn't above, please explain more clearly what the problem is - as orthogonality said, loops finish when they finish. do you think that the code will loop indefinitely? xml documents are trees - they don't have closed loops in the structure, so any traversal will end eventually.
posted by andrew cooke at 7:12 AM on July 6, 2005


sorry, not a direct answer, but I'm sure you'd know that the parameter passed from the onload event will be a success/failure boolean, so you'll need to change that aspect for the example to work...

ie...
xml.onLoad=generateMenu;
function generateMenu(success){
  if (success) {
    for(var x=0;x
    // etc etc...

posted by bruceyeah at 7:23 AM on July 6, 2005


oops, the last line of that was meant to be...

for (var x=0; x<this.firstChild.childNodes; x++) {
posted by bruceyeah at 7:24 AM on July 6, 2005


Response by poster: Thanks a lot - i think ill try out chrismears answer, but i got some ideas from all of you that i want to thank you for.
Best answer all round!
posted by FidelDonson at 9:27 AM on July 6, 2005


« Older 1850 German Culture   |   The fight between Whoomp and Whoot? Newer »
This thread is closed to new comments.