A few optimizations for general coding practice:

uint is slower than int for math and should only be used if you're not doing math on it. Number is faster than uint for numbers higher than int's positive limit. Generally speaking, always use int in for/while loops.

There's no need to call p.parent() twice each loop. You can shortcut this by setting p to its parent in the condition check.

Pre-increment is faster than post-increment.

Searching for a Boolean resolve instead of a specific value is faster.


var p:XML = myNode;
var depth:int;
while (p = p.parent())
{
    ++depth;
}



On 3/22/2010 10:26 AM, mark.jonk...@comcast.net wrote:
If you have a node, you can count its depth using one of two methods
calling a recursive function or simply doing something like this:

var p:XML = myNode;
var depth:uint = 0;
while (p.parent() != undefined)
{
depth++;
p = p.parent();
}

// depth here would be the number of parent.

if you only want to count parents where parent().name() == "person" then change 
the while statement to read:
while ((p.parent() != undefined)&&  (p.parent().name() == "person"))
{
depth++;
p = p.parent();
}

----- Original Message -----
From: "Theodore Lehr"<ted_l...@federal.dell.com>
To: "Flash Coders List"<flashcoders@chattyfig.figleaf.com>
Sent: Monday, March 22, 2010 12:27:50 PM GMT -05:00 US/Canada Eastern
Subject: [Flashcoders] xml: counting parents

I am still trying to find a way to see where a child is within the xml - on 
what level... is it possible to count a nodes parents?

So if I had:

<person att="1">
<person att="2">
<person att="3"/>
<person att="4"/>
</person>
</person>

1 would return that it has 0 parents, 2 would say it has 1 parent, 3&  4 would 
say they have 2 parents....

hope that makes sense....

Ted
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to