Skip to main content
Inspiring
June 7, 2007
Question

XML node has one child?

  • June 7, 2007
  • 2 replies
  • 521 views
Hey guys,

I have a recursive function that search through an XML document and finds each node, whether it has children, etc. When it comes across a block like the following:

<mainitem submenu="true" name="File">
<item submenu="false">Option 1</item>
<item submenu="false">Option 2</item>
<item submenu="false">Option 3</item>
<item submenu="false">Option 4or</item>
<item submenu="false">Option 5</item>
</mainitem>

It correctly states that the first node, "File," has 5 children. But when it recurses to the child nodes it states that each node has 1 child. I am using children().length() to check for number of children....below is my function:

private function parseMenuData(menuData:XML):void{

for each(var menuItem:XML in menuData.elements()){

trace("-----------------------------------------------------------");
trace("Menuitem " + menuItem.@name + "has " + menuItem.children().length() + " children.");

if(menuItem.children().length() > 0){
parseMenuData(menuItem);
}else{
trace("No children here");
}

//parseMenuChildren(menuItem);

}
}

Thanks in advance!
This topic has been closed for replies.

2 replies

Inspiring
June 7, 2007
hciguy wrote:
> Are the contents of a node considered a child?


Yes, it's value can best be retrieved with:

childNodes.nodeValue

place this in a keyframe and test:
var xml:XML = new XML("<test>content</test>");
trace(xml.childNodes.length);
trace(xml.childNodes[0]); // points to the "root"
trace(xml.childNodes[0].childNodes[0]); // first childnode of "root"
trace(xml.childNodes[0].childNodes[0].nodeValue); // it's nodevalue

it should render:
1
<test>content</test>
content
content

ofcourse childNodes[0] can be replaced with: firstChild (maybe even a
few nanoseconds faster)

HTH,
Manno

--
----------
Manno Bult
http://www.aloft.nl
hciguyAuthor
Inspiring
June 7, 2007
When I use xml.childNodes.length I get nothing, as opposed to getting the appropriate number of children when I use children().length() - any ideas why?

Thanks for the help.
hciguyAuthor
Inspiring
June 7, 2007
Actually...it looks like <node>Text</node> has one child because it has "Text" in there....while <node name="text" /> has no children. Are the contents of a node considered a child?

Thanks.