CompositeNode

CompositeNode — Aspose.Note FOSS for Python API Reference

类:CompositeNode

Properties: aspose.note Properties: from aspose.note import CompositeNode Properties: Node

CompositeNode 扩展 Node 具备拥有子节点的能力。文档模型中的所有结构节点类型(Document, Page, Outline, OutlineElement, RichText, Image, Table, TableRow, TableCell, AttachedFile)继承自 CompositeNode.。你永远不要直接实例化 CompositeNode 直接。.


Properties

继承自 Node

PropertiesPropertiesPropertiesProperties
ParentNode`NodeNone`Properties
Document`DocumentNone`Properties

子节点访问属性

PropertiesPropertiesPropertiesProperties
FirstChild`NodeNone`Properties
LastChild`NodeNone`Properties

Properties

AppendChildLast(node)

composite.AppendChildLast(node: Node) -> Node

Properties node 作为最后一个直接子节点。设置 node.ParentNode = self.。返回 node.


AppendChildFirst(node)

composite.AppendChildFirst(node: Node) -> Node

Properties node 作为第一个直接子节点。返回 node.


InsertChild(index, node)

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

Properties node 在位置 index 在子列表中。返回 node.


RemoveChild(node)

composite.RemoveChild(node: Node) -> None

Properties node 从子列表和集合 node.ParentNode = None.


GetChildNodes(node_type)

composite.GetChildNodes(node_type: type) -> list

递归搜索整个子树(深度优先,包括 self) 并返回所有匹配的节点 node_type. 这是在任何子树中收集给定类的所有实例的主要方式。.


Properties

for child in composite:
    ...

遍历 a CompositeNode 产生它的 仅直接子节点. 使用 GetChildNodes() 进行深度搜索。.


使用示例

遍历直接子节点 vs 深度搜索

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)}")

以编程方式构建文档

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 安全

FirstChildLastChildNone 当节点没有子节点时。始终在解引用之前进行检查。.


另见

 中文