Aspose::Words::CompositeNode class

CompositeNode class

Base class for nodes that can contain other nodes. To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.

class CompositeNode : public Aspose::Words::Node,
                      public System::Collections::Generic::IEnumerable<System::SharedPtr<Aspose::Words::Node>>,
                      public Aspose::Words::INodeCollection

Methods

MethodDescription
virtual Accept(System::SharedPtr<Aspose::Words::DocumentVisitor>)Accepts a visitor.
virtual AcceptEnd(System::SharedPtr<Aspose::Words::DocumentVisitor>)
virtual AcceptStart(System::SharedPtr<Aspose::Words::DocumentVisitor>)
AppendChild(T)
Clone(bool)Creates a duplicate of the node.
get_Count()Gets the number of immediate children of this node.
get_CustomNodeId() constSpecifies custom node identifier.
virtual get_Document() constGets the document to which this node belongs.
get_FirstChild() constGets the first child of the node.
get_HasChildNodes()Returns true if this node has any child nodes.
get_IsComposite() overrideReturns true as this node can have child nodes.
get_LastChild() constGets the last child of the node.
get_NextNode() const
get_NextSibling()Gets the node immediately following this node.
virtual get_NodeType() constGets the type of this node.
get_ParentNode()Gets the immediate parent of this node.
get_PreviousSibling()Gets the node immediately preceding this node.
get_PrevNode() const
get_Range()Returns a Range object that represents the portion of a document that is contained in this node.
GetAncestor(Aspose::Words::NodeType)Gets the first ancestor of the specified NodeType.
GetAncestorOf()
GetChild(Aspose::Words::NodeType, int32_t, bool)Returns an Nth child node that matches the specified type.
GetChildNodes(Aspose::Words::NodeType, bool)Returns a live collection of child nodes that match the specified type.
GetEnumerator() overrideProvides support for the for each style iteration over the child nodes of this node.
GetText() overrideGets the text of this node and of all its children.
GetType() const override
IndexOf(const System::SharedPtr<Aspose::Words::Node>&)Returns the index of the specified child node in the child node array.
InsertAfter(T, const System::SharedPtr<Aspose::Words::Node>&)
InsertBefore(T, const System::SharedPtr<Aspose::Words::Node>&)
Is(const System::TypeInfo&) const override
IsAncestorNode(const System::SharedPtr<Aspose::Words::Node>&)
NextPreOrder(const System::SharedPtr<Aspose::Words::Node>&)Gets next node according to the pre-order tree traversal algorithm.
static NodeTypeToString(Aspose::Words::NodeType)A utility method that converts a node type enum value into a user friendly string.
PrependChild(T)
PreviousPreOrder(const System::SharedPtr<Aspose::Words::Node>&)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild(T)
RemoveSmartTags()Removes all SmartTag descendant nodes of the current node.
SelectNodes(const System::String&)Selects a list of nodes matching the XPath expression.
SelectSingleNode(const System::String&)Selects the first Node that matches the XPath expression.
set_CustomNodeId(int32_t)Setter for Aspose::Words::Node::get_CustomNodeId.
set_NextNode(const System::SharedPtr<Aspose::Words::Node>&)
set_PrevNode(const System::SharedPtr<Aspose::Words::Node>&)
SetParent(const System::SharedPtr<Aspose::Words::Node>&)
SetTemplateWeakPtr(uint32_t) override
ToString(Aspose::Words::SaveFormat)Exports the content of the node into a string in the specified format.
ToString(const System::SharedPtr<Aspose::Words::Saving::SaveOptions>&)Exports the content of the node into a string using the specified save options.
static Type()

Remarks

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

For more info see the Composite design pattern.

The CompositeNode class:

  • Provides access to the child nodes.
  • Implements Composite operations such as insert and remove children.
  • Provides methods for XPath navigation.

Examples

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

auto doc = MakeObject<Document>();

// Add two runs and one shape as child nodes to the first paragraph of this document.
auto paragraph = System::ExplicitCast<Paragraph>(doc->GetChild(NodeType::Paragraph, 0, true));
paragraph->AppendChild(MakeObject<Run>(doc, u"Hello world! "));

auto shape = MakeObject<Shape>(doc, ShapeType::Rectangle);
shape->set_Width(200);
shape->set_Height(200);
// Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime.
shape->set_CustomNodeId(100);
shape->set_WrapType(WrapType::Inline);
paragraph->AppendChild(shape);

paragraph->AppendChild(MakeObject<Run>(doc, u"Hello again!"));

// Iterate through the paragraph's collection of immediate children,
// and print any runs or shapes that we find within.
SharedPtr<NodeCollection> children = paragraph->GetChildNodes(Aspose::Words::NodeType::Any, false);

ASSERT_EQ(3, paragraph->GetChildNodes(Aspose::Words::NodeType::Any, false)->get_Count());

for (const auto& child : System::IterateOver(children))
{
    switch (child->get_NodeType())
    {
    case NodeType::Run:
        std::cout << "Run contents:" << std::endl;
        std::cout << "\t\"" << child->GetText().Trim() << "\"" << std::endl;
        break;

    case NodeType::Shape: {
        auto childShape = System::ExplicitCast<Shape>(child);
        std::cout << "Shape:" << std::endl;
        std::cout << String::Format(u"\t{0}, {1}x{2}", childShape->get_ShapeType(), childShape->get_Width(), childShape->get_Height()) << std::endl;
        ASSERT_EQ(100, shape->get_CustomNodeId());
        break;
    }

    default:
        break;
    }
}

See Also