hi Andreas,

recursion is your friend. this isn't tested, so let me know if there
are any problems:

var countSubNodes = function ( node:XMLNode ):Number {
  var children:Array = node.childNodes;
  var numSubNodes:Number = 0;
  for( var i:String in children ) {
    if( children[ i ].nodeName == "node" ) {
      // recursion: call the same function on each childnode, add their children
      // to the number of subchildren of the current one before returning
      numSubNodes += arguments.callee( children[ i ] );
    }
  }
  node.attributes[ "numSubNodes" ] = numSubNodes;
  node.attributes[ "isLeafNode" ] = numSubNodes ? false : true;

  return numSubNodes;
}

just pass it the root node, after that they all should have an
attribute with the total number of sub nodes.

hth,
mark


On 11/24/05, Andreas Rønning <[EMAIL PROTECTED]> wrote:
> Ok, i'm tired and my head hurts, and this problem is making me itch
> horribly.
>
> I have a bit of xml in this format:
>
> <node>
>     <text plainText="First topic"/>
>     <node>
>        <text plainText="Subtopic"/>
>     <node>
>     <node>
>        <text plainText="Subtopic"/>
>        <node>
>            <text plainText="SubSubtopic"/>
>        </node>
>        <node>
>            <text plainText="SubSubtopic"/>
>        </node>
>        <node>
>            <text plainText="SubSubtopic"/>
>        </node>
>        <node>
>            <text plainText="SubSubtopic"/>
>        </node>
>     <node>
>     <node>
>        <text plainText="Subtopic"/>
>     <node>
>     <node>
>        <text plainText="Subtopic"/>
>     <node>
> </node>
>
> In short, there's one topic at the bottom of the tree, which branches
> out into 4 sub-topics. Then one of those subtopics has 4 subtopics under
> that again, and any one of those subtopics could branch further.
> The problem here is that the vertical scale of the visual representation
> of one of these topics grows with every addition to any tree under them.
> So for instance the second subtopic, which has 4 more topics nested,
> would push the first, third and fourth subtopic up and down by about
> half the height of its own tree.
>
> For an illustration, check out http://andreas.rayon.no/temp/tree.jpg
>
> Now how the hell do i get the scale of a tree like that. The first topic
> could be 20 px high, but has 2 topics nested, making it 40 px tall
> (since they spread vertically and horizontally). Then one of those
> topics again could have 3 topics nested, and one of those could have one
> nested, and then that could have 8 nested again. How do i go through the
> tree to get the height and align like that? I've never had to work with
> xml with this depth before and it's really tearing at my head.
> Anyone done anything like this before?
>
> - Andreas
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>


--
http://snafoo.org/
jabber: [EMAIL PROTECTED]
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to