# Recursive Function, XML tree

**URL:** <https://forum.kirupa.com/t/recursive-function-xml-tree/50509>\
**Category:** flash\
**Created:** [May 3, 2004, 9:05pm UTC](https://forum.kirupa.com/t/recursive-function-xml-tree/50509 "2004-05-03T21:05:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RvGaTe](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rvgate/32/126_2.png) [@RvGaTe](https://forum.kirupa.com/u/RvGaTe)\
**Post date:** [May 3, 2004, 9:05pm UTC](https://forum.kirupa.com/t/recursive-function-xml-tree/50509/1 "2004-05-03T21:05:40Z")

</div>

ok guys, im having a problem reading a xml tree in Flash MX…

i got this tree,

**data.xml**

```php

<menu>
	<menuitem label="menu1">
		<item label="menu1_item1" file="file.swf" />
		<item label="menu1_item2" file="file.swf" />
		<menuitem label="sub1">
			<item label="menu1_sub1_item1" file="file.swf" />
			<item label="menu1_sub1_item2" file="file.swf" />
			<item label="menu1_sub1_item3" file="file.swf" />
			<item label="menu1_sub1_item4" file="file.swf" />
		</menuitem>
	</menuitem>
	<menuitem label="menu2">
		<item label="menu2_item1" file="file.swf" />
		<item label="menu2_item2" file="file.swf" />
		<menuitem label="sub1">
			<item label="menu2_sub1_item1" file="file.swf" />
			<item label="menu2_sub1_item2" file="file.swf" />
			<item label="menu2_sub1_item3" file="file.swf" />
			<item label="menu2_sub1_item4" file="file.swf" />
		</menuitem>
	</menuitem>
	<menuitem label="menu3">
		<item label="menu3_item1" file="file.swf" />
		<item label="menu3_item2" file="file.swf" />
		<menuitem label="sub1">
			<item label="menu3_sub1_item1" file="file.swf" />
			<item label="menu3_sub1_item2" file="file.swf" />
			<item label="menu3_sub1_item3" file="file.swf" />
			<item label="menu3_sub1_item4" file="file.swf" />
		</menuitem>
	</menuitem>
</menu>

```

and this Actionscript to just to view the tree…

```php

function addChilds(childs) {
	for (i=0; i<childs.length; i++) {
		if (childs*.hasChildNodes()) {
			addChilds(childs*.childNodes);
		} else {
			trace(childs*.attributes.label);
		}
	}
}
myXML = new XML();
myXML.ignoreWhite = true;
myXML.load("data.xml");
myXML.onLoad = function() {
	addChilds(this.firstChild.childNodes);
};

```

now comes the problem, when running it, i only get this:

> 

menu1\_item1  
menu1\_item2  
menu1\_sub1\_item1  
menu1\_sub1\_item2  
menu1\_sub1\_item3  
menu1\_sub1\_item4

meaning that when it calls itself again, that it will stop searching the next items on the same level from where it was called…

how do i not let it stop searching and complete the tree… please help…

(i hope it maked any sence)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 4, 2004, 11:26am UTC](https://forum.kirupa.com/t/recursive-function-xml-tree/50509/2 "2004-05-04T11:26:04Z")

</div>

Nope, same result, maybe becouse its the exact same code ? 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 4, 2004, 12:12pm UTC](https://forum.kirupa.com/t/recursive-function-xml-tree/50509/3 "2004-05-04T12:12:33Z")

</div>

Maybe this makes it a bit clearer

```php

function addChilds(childs) {
	for (i=0; i<childs.length; i++) {
		if (childs*.hasChildNodes()) {
			currentMenu = childs*.attributes.label;
			trace(newline+"begin "+currentMenu);
			addChilds(childs*.childNodes);
		} else {
			trace(" "+childs*.attributes.label);
		}
	}
	trace("end "+currentMenu+newline);
}
myXML = new XML();
myXML.ignoreWhite = true;
myXML.load("data.xml");
myXML.onLoad = function() {
	addChilds(this.firstChild.childNodes);
};

```

results in:

```php

begin menu1
   menu1_item1
   menu1_item2

begin sub1
   menu1_sub1_item1
   menu1_sub1_item2
   menu1_sub1_item3
   menu1_sub1_item4
end sub1

end sub1

end sub1

```
