开发者

Dynamically detect XML child

开发者 https://www.devze.com 2022-12-08 19:11 出处:网络
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.

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开发者_如何学JAVA 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();
            }
        }
    }


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
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号