views:

65

answers:

1

How to know if a XML list have a child? My code below is using hard code stating that I want to run the child of my XML. But I wouldn't want to write write every children() of it if I have a lot of child.

private function dataLevel():void {
  for (var j:Number=0; j<xmlList.length(); j++) {
   createBranch();

   for (var k:int = 0; k<xmlList[j].children().length(); k++) {
    createBranch();
   }

   for (var l:int = 0; l<xmlList[j].children().children().length(); l++) {
    createBranch();
   }
  }
 }
+2  A: 

What does createBranch do? Doesn't it depend on the name of the child? From the given code, it seems that you just wanna call the createBranch as many times as there are children (or grandchildren) - though I'm not sure what xmlList[j].children().children().length() is gonna return. If you just wanna get a list of all the children and grandchildren, use the descendants() method.

If you wanna go through the children in the hierarchical order, write a recursive method.

function traverseChildren(node:XML):void
{
  //calling createBranch here means one call per each xml children
  createBranch();
  var list:XMLList = node.children();
  for each(var child:XML in list)
  {
    if(child.nodeKind() == "element")
      traverseChildren(child);
    //if you wanna call createBranch for each attribute and text node
    //and comments and processing instructions, call it here instead
  }
}
Amarghosh
Well this helps me to figure out the way to solve my question. Thanx!
Hwang