PDA

View Full Version : XML: accessing all ancestors.



andriusain
June 11th, 2009, 12:10 AM
I am missing a way to call all xml element's ancestors like in
element.descendants() but the other way round. Any thoughts?

creatify
June 11th, 2009, 11:46 AM
You can use parent(), or you can get a list of all elements as well using elements(). But there isn't an ancestors method. I'm curious as to why you'd need that? You should be able to do node.parent().parent() as well to crawl up the tree.

andriusain
June 11th, 2009, 11:58 AM
Well because the number of ancestors will be unknown, it may vary, and I would like to retrieve all of them until I reach the root.

This is for building a rule for a dynamic application, to be specific a dynamic menu.

wvxvw
June 11th, 2009, 01:17 PM
var xml:XML = <a><b><c><d/></c></b></a>;
var p:Object;
var node:XML = xml..d[0];
do
{
p = node.parent();
if (p && p is XML) node = p as XML;
else break;
trace((p as XML).toXMLString());
trace("----------------------");
}
while (true);

andriusain
June 12th, 2009, 11:34 AM
Thanks for the info!