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.

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
custom_node_id Specifies custom node identifier.
document Gets the document to which this node belongs.
is_composite Returns True if this node can contain other nodes.
next_sibling Gets the node immediately following this node.
node_type Gets the type of this node.
parent_node Gets the immediate parent of this node.
previous_sibling 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

Name Description
accept(visitor) Accepts a visitor.
as_body() Cast node to Body.
as_bookmark_end() Cast node to BookmarkEnd.
as_bookmark_start() Cast node to BookmarkStart.
as_building_block() Cast node to BuildingBlock.
as_cell() Cast node to Cell.
as_comment() Cast node to Comment.
as_comment_range_end() Cast node to CommentRangeEnd.
as_comment_range_start() Cast node to CommentRangeStart.
as_composite_node() Cast node to CompositeNode.
as_document() Cast node to Node.document.
as_editable_range_end() Cast node to EditableRangeEnd.
as_editable_range_start() Cast node to EditableRangeStart.
as_field_end() Cast node to FieldEnd.
as_field_separator() Cast node to FieldSeparator.
as_field_start() Cast node to FieldStart.
as_footnote() Cast node to Footnote.
as_form_field() Cast node to FormField.
as_glossary_document() Cast node to GlossaryDocument.
as_group_shape() Cast node to GroupShape.
as_header_footer() Cast node to HeaderFooter.
as_office_math() Cast node to OfficeMath.
as_paragraph() Cast node to Paragraph.
as_row() Cast node to Row.
as_run() Cast node to Run.
as_section() Cast node to Section.
as_shape() Cast node to Shape.
as_smart_tag() Cast node to SmartTag.
as_special_char() Cast node to SpecialChar.
as_structured_document_tag() Cast node to StructuredDocumentTag.
as_structured_document_tag_range_end() Cast node to StructuredDocumentTagRangeEnd.
as_structured_document_tag_range_start() Cast node to StructuredDocumentTagRangeStart.
as_sub_document() Cast node to SubDocument.
as_table() Cast node to Table.
clone(is_clone_children) Creates a duplicate of the node.
get_ancestor(ancestor_type) Gets the first ancestor of the specified object type.
get_ancestor(ancestor_type) Gets the first ancestor of the specified NodeType.
get_text() Gets the text of this node and of all its children.
next_pre_order(root_node) Gets next node according to the pre-order tree traversal algorithm.
node_type_to_string(node_type) A utility method that converts a node type enum value into a user friendly string.
previous_pre_order(root_node) Gets the previous node according to the pre-order tree traversal algorithm.
remove() Removes itself from the parent.
to_string(save_format) Exports the content of the node into a string in the specified format.
to_string(save_options) Exports the content of the node into a string using the specified save options.

Examples

Shows how to clone a composite node.

doc = aw.Document()
para = doc.first_section.body.first_paragraph
para.append_child(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.
clone_with_children = para.clone(True)

self.assertTrue(clone_with_children.as_composite_node().has_child_nodes)
self.assertEqual("Hello world!", clone_with_children.get_text().strip())

# 2 -  Create a clone of a node just by itself without any children.
clone_without_children = para.clone(False)

self.assertFalse(clone_without_children.as_composite_node().has_child_nodes)
self.assertEqual("", clone_without_children.get_text().strip())

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

doc = aw.Document()

# Add two runs and one shape as child nodes to the first paragraph of this document.
paragraph = doc.get_child(aw.NodeType.PARAGRAPH, 0, True).as_paragraph()
paragraph.append_child(aw.Run(doc, "Hello world! "))

shape = aw.drawing.Shape(doc, aw.drawing.ShapeType.RECTANGLE)
shape.width = 200
shape.height = 200
# Note that the 'custom_node_id' is not saved to an output file and exists only during the node lifetime.
shape.custom_node_id = 100
shape.wrap_type = aw.drawing.WrapType.INLINE
paragraph.append_child(shape)

paragraph.append_child(aw.Run(doc, "Hello again!"))

# Iterate through the paragraph's collection of immediate children,
# and print any runs or shapes that we find within.
children = paragraph.child_nodes

self.assertEqual(3, paragraph.child_nodes.count)

for child in children:
    if child.node_type == aw.NodeType.RUN:
        print("Run contents:")
        print(f"\t\"{child.get_text().strip()}\"")

    elif child.node_type == aw.NodeType.SHAPE:
        child_shape = child.as_shape()
        print("Shape:")
        print(f"\t{child_shape.shape_type}, {child_shape.width}x{child_shape.height}")

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

doc = aw.Document(MY_DIR + "Tables.docx")

self.assertEqual(2, doc.get_child_nodes(aw.NodeType.TABLE, True).count)

cur_node = doc.first_section.body.first_child

while cur_node is not None:

    # Save the next sibling node as a variable in case we want to move to it after deleting this node.
    next_node = cur_node.next_sibling

    # A section body can contain Paragraph and Table nodes.
    # If the node is a Table, remove it from the parent.
    if cur_node.node_type == aw.NodeType.TABLE:
        cur_node.remove()

    cur_node = next_node

self.assertEqual(0, doc.get_child_nodes(aw.NodeType.TABLE, True).count)

See Also