CompositeNode — Aspose.Note FOSS for Python API Reference

Class: CompositeNode

Package: aspose.note Import: from aspose.note import CompositeNode Inherits: Node

CompositeNode extends Node with the ability to own child nodes. All structural node types in the document model (Document, Page, Outline, OutlineElement, RichText, Image, Table, TableRow, TableCell, AttachedFile) inherit from CompositeNode. You never instantiate CompositeNode directly.


Properties

Inherited from Node

PropertyTypeAccessDescription
ParentNodeNode | NoneReadParent node in the document tree
DocumentDocument | NoneReadRoot Document ancestor
NodeTypeNodeTypeReadConcrete node type identifier

Child-access Properties

PropertyTypeAccessDescription
FirstChildNode | NoneReadFirst direct child node, or None if the node has no children
LastChildNode | NoneReadLast direct child node, or None if the node has no children

Methods

AppendChildLast(node)

composite.AppendChildLast(node: Node) -> Node

Adds node as the last direct child. Sets node.ParentNode = self. Returns node.


AppendChildFirst(node)

composite.AppendChildFirst(node: Node) -> Node

Adds node as the first direct child. Returns node.


InsertChild(index, node)

composite.InsertChild(index: int, node: Node) -> Node

Inserts node at position index in the child list. Returns node.


RemoveChild(node)

composite.RemoveChild(node: Node) -> None

Removes node from the child list and sets node.ParentNode = None.


GetChildNodes(node_type)

composite.GetChildNodes(node_type: type) -> list

Recursively searches the entire subtree (depth-first, including self) and returns all nodes matching node_type. This is the primary way to collect all instances of a given class within any subtree.


Iteration

for child in composite:
    ...

Iterating a CompositeNode yields its direct children only. Use GetChildNodes() for a deep search.


Usage Example

Traverse direct children vs deep search

from aspose.note import Document, Page, Outline, RichText

doc = Document("MyNotes.one")
first_page = doc.GetChildNodes(Page)[0]

# Direct children of a page (Outline nodes)
print("Direct children of first page:")
for child in first_page:
    print(f"  {child.NodeType}")

# Deep search — all RichText nodes anywhere under first_page
all_rt = first_page.GetChildNodes(RichText)
print(f"\nAll RichText nodes in first page: {len(all_rt)}")

Build a document programmatically

from aspose.note import Document, Page, Outline, OutlineElement, RichText, SaveFormat, PdfSaveOptions

doc = Document()
page = Page()
outline = Outline()
oe = OutlineElement()
rt = RichText()
rt.Append("Hello, OneNote!")

oe.AppendChildLast(rt)
outline.AppendChildLast(oe)
page.AppendChildLast(outline)
doc.AppendChildLast(page)

doc.Save("output.pdf", PdfSaveOptions(SaveFormat.Pdf))

None-Safety

FirstChild and LastChild are None when the node has no children. Always guard before dereferencing them.


See Also