Name

XML.nextSibling Property — a reference to the node after this node

Availability

Flash 5

Synopsis

theNode.nextSibling

Access

Read-only

Description

The nextSibling property returns the node object after theNode in the current level of the XML object hierarchy. If there is no node after theNode, nextSibling returns null. In the following XML source fragment, the CONTENT node is the nextSibling of the USER node:

<MESSAGE><USER>gray</USER><CONTENT>hi</CONTENT></MESSAGE>

Example

The nextSibling property is typically used to traverse (move through) an XML object hierarchy. For example, to view all the children of theNode in the order they appear, we may use:

for (var child = theNode.firstChild; child != null; child = child.nextSibling) {
  trace("found node: " + child.nodeName);
}

By extending our loop into a function, we can recursively traverse every node in an XML object hierarchy, as follows:

function showNodes (node) {
  trace(node.nodeName + ": " + node.nodeValue);
  for (var child = node.firstChild; child != null; child = child.nextSibling) {
    showNodes(child);
  }
}

// Invoke the function on our node or document
showNodes(myDoc);

Note that in both traversal examples shown, text nodes show up without a name as described under the nodeName entry.

See Also

XML .childNodes, XML .firstChild, XML .lastChild, XML .nodeName, XML .nodeValue, XML .previousSibling

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.