【问题标题】:How to count total number of nodes in sub tree from XML tree structure using DOM and JS?如何使用 DOM 和 JS 从 XML 树结构中计算子树中的节点总数?
【发布时间】:2012-01-02 08:46:46
【问题描述】:

对于给定节点,我需要找到子树节点的总数来设置该给定节点的权重属性。 我想知道如何使用 DOM 和 JS 从 XML 树结构中递归计算子树中的总节点并设置该节点的权重属性?

【问题讨论】:

    标签: xml dom tree


    【解决方案1】:
    function countNodes(node) {
      var i = 0, c = node.childNodes.length, result = c;
      for (; i<c; i++) result += countNodes(node.childNodes[i]);
      // if you want: node.weight = result;
      return result;
    } 
    

    【讨论】:

    • @ZabedAkbar 我有种下沉的感觉,你的问题是一个家庭作业问题。
    • nope.. 我正在开发一个树 api 项目,它将生成不同的树布局。顺便说一句,感谢您的帮助。
    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 2016-05-31
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多