0

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();
            }
        }
    }

1 Answer 1

2

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
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Well this helps me to figure out the way to solve my question. Thanx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.