Node class

Node class

Base class for all nodes of a Word document. To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.

Remarks

A document is represented as a tree of nodes, similar to DOM or XmlDocument.

For more info see the Composite design pattern.

The Node class:

  • Defines the child node interface.

  • Defines the interface for visiting nodes.

  • Provides default cloning capability.

  • Implements parent node and owner document mechanisms.

  • Implements access to sibling nodes.

Properties

NameDescription
customNodeIdSpecifies custom node identifier.
documentGets the document to which this node belongs.
isCompositeReturns true if this node can contain other nodes.
nextSiblingGets the node immediately following this node.
nodeTypeGets the type of this node.
parentNodeGets the immediate parent of this node.
previousSiblingGets the node immediately preceding this node.
rangeReturns a Range object that represents the portion of a document that is contained in this node.

Methods

NameDescription
asBody()Cast node to Body.
asBookmarkEnd()Cast node to BookmarkEnd.
asBookmarkStart()Cast node to BookmarkStart.
asBuildingBlock()Cast node to BuildingBlock.
asCell()Cast node to Cell.
asComment()Cast node to Comment.
asCommentRangeEnd()Cast node to CommentRangeEnd.
asCommentRangeStart()Cast node to CommentRangeStart.
asCompositeNode()Cast node to CompositeNode.
asDocument()Cast node to Node.document.
asEditableRangeEnd()Cast node to EditableRangeEnd.
asEditableRangeStart()Cast node to EditableRangeStart.
asFieldEnd()Cast node to FieldEnd.
asFieldSeparator()Cast node to FieldSeparator.
asFieldStart()Cast node to FieldStart.
asFootnote()Cast node to Footnote.
asFormField()Cast node to FormField.
asGlossaryDocument()Cast node to GlossaryDocument.
asGroupShape()Cast node to GroupShape.
asHeaderFooter()Cast node to HeaderFooter.
asOfficeMath()Cast node to OfficeMath.
asParagraph()Cast node to Paragraph.
asRow()Cast node to Row.
asRun()Cast node to Run.
asSection()Cast node to Section.
asShape()Cast node to Shape.
asSmartTag()Cast node to SmartTag.
asSpecialChar()Cast node to SpecialChar.
asStructuredDocumentTag()Cast node to StructuredDocumentTag.
asStructuredDocumentTagRangeEnd()Cast node to StructuredDocumentTagRangeEnd.
asStructuredDocumentTagRangeStart()Cast node to StructuredDocumentTagRangeStart.
asSubDocument()Cast node to SubDocument.
asTable()Cast node to Table.
clone(isCloneChildren)Creates a duplicate of the node.
getAncestor(ancestorType)Gets the first ancestor of the specified NodeType.
getText()Gets the text of this node and of all its children.
nextPreOrder(rootNode)Gets next node according to the pre-order tree traversal algorithm.
nodeTypeToString(nodeType)A utility method that converts a node type enum value into a user friendly string.
previousPreOrder(rootNode)Gets the previous node according to the pre-order tree traversal algorithm.
referenceEquals(other)
remove()Removes itself from the parent.
toString(saveFormat)Exports the content of the node into a string in the specified format.
toString(saveOptions)Exports the content of the node into a string using the specified save options.

Examples

Shows how to clone a composite node.

let doc = new aw.Document();
let para = doc.firstSection.body.firstParagraph;
para.appendChild(new aw.Run(doc, "Hello world!"));

// Below are two ways of cloning a composite node.
// 1 -  Create a clone of a node, and create a clone of each of its child nodes as well.
let cloneWithChildren = para.clone(true).asParagraph();

expect(cloneWithChildren.hasChildNodes).toEqual(true);
expect(cloneWithChildren.getText().trim()).toEqual("Hello world!");

// 2 -  Create a clone of a node just by itself without any children.
let cloneWithoutChildren = para.clone(false).asParagraph();

expect(cloneWithoutChildren.hasChildNodes).toEqual(false);
expect(cloneWithoutChildren.getText().trim()).toEqual('');

Shows how to traverse through a composite node’s collection of child nodes.

let doc = new aw.Document();

// Add two runs and one shape as child nodes to the first paragraph of this document.
let paragraph = doc.getParagraph(0, true);
paragraph.appendChild(new aw.Run(doc, "Hello world! "));

let shape = new aw.Drawing.Shape(doc, aw.Drawing.ShapeType.Rectangle);
shape.width = 200;
shape.height = 200;
// Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime.
shape.customNodeId = 100;
shape.wrapType = aw.Drawing.WrapType.Inline;
paragraph.appendChild(shape);

paragraph.appendChild(new aw.Run(doc, "Hello again!"));

// Iterate through the paragraph's collection of immediate children,
// and print any runs or shapes that we find within.
let children = paragraph.getChildNodes(aw.NodeType.Any, false);

expect(paragraph.getChildNodes(aw.NodeType.Any, false).count).toEqual(3);

for (let child of children)
  switch (child.nodeType)
  {
    case aw.NodeType.Run:
      console.log("Run contents:");
      console.log(`\t\"${child.getText().trim()}\"`);
      break;
    case aw.NodeType.Shape:
      let childShape = child.asShape();
      console.log("Shape:");
      console.log(`\t${childShape.shapeType}, ${childShape.width}x${childShape.height}`);
      expect(shape.customNodeId).toEqual(100);
      break;
  }

Shows how to remove all child nodes of a specific type from a composite node.

let doc = new aw.Document(base.myDir + "Tables.docx");

expect(doc.getChildNodes(aw.NodeType.Table, true).count).toEqual(2);

let curNode = doc.firstSection.body.firstChild;

while (curNode != null)
{
  // Save the next sibling node as a variable in case we want to move to it after deleting this node.
  let nextNode = curNode.nextSibling;

  // A section body can contain Paragraph and Table nodes.
  // If the node is a Table, remove it from the parent.
  if (curNode.nodeType == aw.NodeType.Table)
    curNode.remove();

  curNode = nextNode;
}

expect(doc.getChildNodes(aw.NodeType.Table, true).count).toEqual(0);

See Also