Take the 2-minute tour ×  For a given node I need to find the
total number of sub tree node to set the weight property of that
given node. I am wondering how can we recursively count total
nodes in sub tree from XML tree structure using DOM and JS and
set the weight property of that node?  share|improve this
question add comment  1 Answer  up vote 0 down vote accepted
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; share|improve this answer thanks a lot
man.. that really solves my problem :) –  ZabedAkbar Nov 20 '11
at 16:28 @ZabedAkbar I have the sinking feeling that your
problem was a homework question. –  Tomalak Nov 20 '11 at 18:46
nope.. I am working on a project for tree api which will
generate different tree layout. Thanks for your help btw. – 
ZabedAkbar Nov 21 '11 at 2:12 add comment  Your Answer