You can obtain the node immediately preceding or following a certain node using Node.PreviousSibling and Node.NextSibling, respectively. If a node is the last child of its parent, then the NextSibling property is null. Conversely, if the node is a first child of its parent, the PreviousSibling property is null.
Note that because the child nodes are internally stored in a single linked list in Aspose.Words, NextSibling is more efficient than PreviousSibling.
Example RecurseAllNodes
Shows how to efficiently visit all direct and indirect children of a composite node.
[C#]
public void RecurseAllNodes()
{
// Open a document.
Document doc = new Document(MyDir + "Node.RecurseAllNodes.doc");
// Invoke the recursive function that will walk the tree.
TraverseAllNodes(doc);
}
/// <summary>
/// A simple function that will walk through all children of a specified node recursively
/// and print the type of each node to the screen.
/// </summary>
public void TraverseAllNodes(CompositeNode parentNode)
{
// This is the most efficient way to loop through immediate children of a node.
for (Node childNode = parentNode.FirstChild; childNode != null; childNode = childNode.NextSibling)
{
// Do some useful work.
Console.WriteLine(childNode.NodeType);
// Recurse into the node if it is a composite node.
if (childNode.IsComposite)
TraverseAllNodes((CompositeNode)childNode);
}
}
[Visual Basic]
Public Sub RecurseAllNodes()
' Open a document.
Dim doc As Document = New Document(MyDir & "Node.RecurseAllNodes.doc")
' Invoke the recursive function that will walk the tree.
TraverseAllNodes(doc)
End Sub
''' <summary>
''' A simple function that will walk through all children of a specified node recursively
''' and print the type of each node to the screen.
''' </summary>
Public Sub TraverseAllNodes(ByVal parentNode As CompositeNode)
' This is the most efficient way to loop through immediate children of a node.
Dim childNode As Node = parentNode.FirstChild
Do While Not childNode Is Nothing
' Do some useful work.
Console.WriteLine(childNode.NodeType)
' Recurse into the node if it is a composite node.
If childNode.IsComposite Then
TraverseAllNodes(CType(childNode, CompositeNode))
End If
childNode = childNode.NextSibling
Loop
End Sub
[Java]
public void RecurseAllNodes() throws Exception
{
// Open a document.
Document doc = new Document(getMyDir() + "Node.RecurseAllNodes.doc");
// Invoke the recursive function that will walk the tree.
TraverseAllNodes(doc);
}
/// <summary>
/// A simple function that will walk through all children of a specified node recursively
/// and print the type of each node to the screen.
/// </summary>
private void TraverseAllNodes(CompositeNode parentNode)
{
// This is the most efficient way to loop through immediate children of a node.
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling())
{
// Do some useful work.
System.out.println(childNode.getNodeType());
// Recurse into the node if it is a composite node.
if (childNode.isComposite())
TraverseAllNodes((CompositeNode)childNode);
}
}