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
Name | Description |
---|---|
customNodeId | Specifies custom node identifier. |
document | Gets the document to which this node belongs. |
isComposite | Returns true if this node can contain other nodes. |
nextSibling | Gets the node immediately following this node. |
nodeType | Gets the type of this node. |
parentNode | Gets the immediate parent of this node. |
previousSibling | Gets the node immediately preceding this node. |
range | Returns a Range object that represents the portion of a document that is contained in this node. |
Methods
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
- module Aspose.Words