views:

448

answers:

2

Hey,

I have a reference to a XML node which is part of a bigger XML tree. Is there a way to get that elements next/previous sibling without accessing the parent and looking for it? Something like DOM's nextSibling would be what I look for.

edit: Given that there is no natural way to do this with E4X, I'll just stick to the following (except that in my case, I'll store the actual index somewhere instead):

// next sibling:
node.parent().children()[ node.childIndex() + 1 ]
// previous sibling:
node.parent().children()[ node.childIndex() - 1 ]
A: 

Is this what you are looking for?

http://livedocs.adobe.com/flex/3/langref/flash/xml/XMLNode.html#nextSibling

Thomas
Not really, because I can't convert a XML object to a XMLNode. (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XML.html)
poke
Sure you can: new XMLDocument( xml.toString() );
Matt W
Fair enough :P – But I doubt that is efficient enough to warrant having a E4X XML tree *and* a DOM tree.
poke
+1  A: 

I don't see any such methods in ECMA-357, 2nd edition. Moreover, "sibling" doesn't seem to appear anywhere in my copy of the standard. If any such methods exist, they exist because Adobe added them as extensions to the spec.

Jeff Walden
Too bad. I guess I'll have to store the index of my node then somewhere to efficiently browse through it. Thanks for looking it up in the standard :)
poke
To be fair, the DOM itself is algorithmically inefficient. Notice how two different methods of accessing child elements exist in it in the W3C DOM, for sibling-wise and index-wise access. Supporting either gives you fast element access (sibling-wise with next, previous, parent, first child, last child, say; index-wise with parent and an array of children) with certain idioms. Supporting both means you inevitably speed up one idiom and sometimes (modulo tricks) slow down the other. E4X only supporting index-wise access avoids that problem (at cost of requiring index-based algorithms).
Jeff Walden