# Basic Recursion Problem

**URL:** <https://forum.kirupa.com/t/basic-recursion-problem/245371>\
**Category:** flash\
**Created:** [November 28, 2007, 11:27pm UTC](https://forum.kirupa.com/t/basic-recursion-problem/245371 "2007-11-28T23:27:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmotts](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@madmotts](https://forum.kirupa.com/u/madmotts)\
**Post date:** [November 28, 2007, 11:27pm UTC](https://forum.kirupa.com/t/basic-recursion-problem/245371/1 "2007-11-28T23:27:54Z")

</div>

[SIZE=1][SIZE=2]I’m writing a xml search function using recursion and I’m having problems stopping it when it finds what it’s looking for. It parses the WHOLE tree even after it has found a node, which causes the function to return “undefined”. A hack would be to put it in a temp variable, assign it when it’s found and just let it run through the whole tree. Anythoughts?[/SIZE]

[SIZE=2][COLOR=Blue]function **findNodeByName** (theName, theXmlTree ){  
[/COLOR][/SIZE][/SIZE][INDENT][SIZE=1][SIZE=2][COLOR=Blue] trace(“NODE \>”+theXmlTree.attributes.name);[/COLOR][/SIZE][/SIZE]  
[SIZE=1][SIZE=2][COLOR=Blue] if(theXmlTree.attributes.name == theName){[/COLOR][/SIZE][/SIZE][INDENT][SIZE=1][SIZE=2][COLOR=Blue] trace(“FOUND IT”);[/COLOR][/SIZE][/SIZE]  
[SIZE=1][SIZE=2][COLOR=Blue] return (theXmlTree);[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }else{[/COLOR][/SIZE][/SIZE][INDENT][SIZE=1][SIZE=2][COLOR=Blue] if(theXmlTree.hasChildNodes() != false){[/COLOR][/SIZE][/SIZE][INDENT][SIZE=1][SIZE=2][COLOR=Blue] for(var i=0; i \< theXmlTree.childNodes.length; i++){[/COLOR][/SIZE][/SIZE]  
[INDENT][SIZE=1][SIZE=2][COLOR=Blue] **findNodeByName** (theName,theXmlTree.childNodes\*);[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }else{[/COLOR][/SIZE][/SIZE][INDENT][SIZE=1][SIZE=2][COLOR=Blue] trace(“leaf node”);[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }[/COLOR][/SIZE][/SIZE]  
[/INDENT][SIZE=1][SIZE=2][COLOR=Blue] }[/COLOR][/SIZE]  
[/SIZE]
