Class xml_node

Synopsis

#include <src/pugixml.hpp>

class PUGIXML_CLASS xml_node

Description

A light-weight handle for manipulating nodes in DOM tree.

Mentioned in

Inheritance

Decsendents: xml_document

Methods

xml_node overloadDefault constructor. Constructs an empty node.
xml_node overloadConstructs node from internal pointer.
append_attributeAdd attribute with specified name. Returns added attribute, or empty attribute on errors.
append_bufferParses buffer as an XML document fragment and appends all nodes as children of the current node
append_child overloadAdd child node with specified type. Returns added node, or empty node on errors.
append_child overloadAdd child element with specified name. Returns added node, or empty node on errors.
append_copy overloadAdd a copy of the specified attribute. Returns added attribute, or empty attribute on errors.
append_copy overloadAdd a copy of the specified node as a child. Returns added node, or empty node on errors.
append_moveMove the specified node to become a child of this node. Returns moved node, or empty node on errors.
attribute overloadGet attribute, starting the search from a hint (and updating hint so that searching for a sequence of attributes is fast)
attributes

Mentioned in

attributes_begin

Mentioned in

attributes_end

Mentioned in

begin
childGet child, attribute or next/previous sibling with the specified name.
child_value overloadGet child value of current node; that is, value of the first child node of type PCDATA/CDATA.
child_value overloadGet child value of child with specified name. Equivalent to child(name).child_value().
children overloadRange-based for support.
emptyCheck if node is empty.
end
find_attributeFind attribute using predicate. Returns first attribute for which predicate returned true.
find_childFind child node using predicate. Returns first child for which predicate returned true.
find_child_by_attribute overloadFind child node by attribute name/value.
find_nodeFind node from subtree using predicate. Returns first node from subtree (depth-first), for which predicate returned true.
first_attributeGet attribute list.
first_childGet children list.
first_element_by_pathSearch for a node by path consisting of node names and . or .. elements.
hash_valueGet hash value (unique for handles to the same object)
insert_attribute_after
insert_attribute_before
insert_child_after overload

Mentioned in

insert_child_before overload

Mentioned in

insert_copy_after overload

Mentioned in

insert_copy_before overload

Mentioned in

insert_move_after

Mentioned in

insert_move_before

Mentioned in

internal_objectGet internal pointer.
last_attribute

Mentioned in

last_child

Mentioned in

nameGet node name, or "" if node is empty or it has no name.
next_sibling overloadGet next/previous sibling in the children list of the parent node.
offset_debugGet node offset in parsed file/string (in char_t units) for debugging purposes.
operator unspecified_bool_typeSafe bool conversion operator.
operator!Borland C++ workaround.
operator!=
operator<
operator<=
operator==Comparison operators (compares wrapped node pointers)
operator>
operator>=
parentGet parent node.
pathGet the absolute node path from root as a text string.
prepend_attribute

Mentioned in

prepend_child overload

Mentioned in

prepend_copy overload

Mentioned in

prepend_move

Mentioned in

previous_sibling overload

Mentioned in

print overloadPrint subtree using a writer object.
print overloadPrint subtree to stream.
remove_attribute overloadRemove specified attribute.
remove_attributesRemove all attributes.
remove_child overloadRemove specified child.
remove_childrenRemove all children.
rootGet root of DOM tree this node belongs to.
select_node overloadSelect single node by evaluating XPath query. Returns first node from the resulting node set.
select_nodes overloadSelect node set by evaluating XPath query.
select_single_node overload(deprecated: use select_node instead) Select single node by evaluating XPath query.
set_nameSet node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value)
set_value
textGet text object for the current node.
traverseRecursively traverse subtree with xml_tree_walker.
typeGet node type.
valueGet node value, or "" if node is empty or it has no value Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes.

Source

Lines 468-720 in src/pugixml.hpp.

class PUGIXML_CLASS xml_node
{
    friend class xml_attribute_iterator;
    friend class xml_node_iterator;
    friend class xml_named_node_iterator;
protected:
    xml_node_struct* _root;
    typedef void (*unspecified_bool_type)(xml_node***);
public:
    // Default constructor. Constructs an empty node.
    xml_node();
    // Constructs node from internal pointer
    explicit xml_node(xml_node_struct* p);
    // Safe bool conversion operator
    operator unspecified_bool_type() const;
    // Borland C++ workaround
    bool operator!() const;
    // Comparison operators (compares wrapped node pointers)
    bool operator==(const xml_node& r) const;
    bool operator!=(const xml_node& r) const;
    bool operator<(const xml_node& r) const;
    bool operator>(const xml_node& r) const;
    bool operator<=(const xml_node& r) const;
    bool operator>=(const xml_node& r) const;
    // Check if node is empty.
    bool empty() const;
    // Get node type
    xml_node_type type() const;
    // Get node name, or "" if node is empty or it has no name
    const char_t* name() const;
    // Get node value, or "" if node is empty or it has no value
    // Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes.
    const char_t* value() const;
    // Get attribute list
    xml_attribute first_attribute() const;
    xml_attribute last_attribute() const;
    // Get children list
    xml_node first_child() const;
    xml_node last_child() const;
    // Get next/previous sibling in the children list of the parent node
    xml_node next_sibling() const;
    xml_node previous_sibling() const;
    // Get parent node
    xml_node parent() const;
    // Get root of DOM tree this node belongs to
    xml_node root() const;
    // Get text object for the current node
    xml_text text() const;
    // Get child, attribute or next/previous sibling with the specified name
    xml_node child(const char_t* name) const;
    xml_attribute attribute(const char_t* name) const;
    xml_node next_sibling(const char_t* name) const;
    xml_node previous_sibling(const char_t* name) const;
    // Get attribute, starting the search from a hint (and updating hint so that searching for a sequence of attributes is fast)
    xml_attribute attribute(const char_t* name, xml_attribute& hint) const;
    // Get child value of current node; that is, value of the first child node of type PCDATA/CDATA
    const char_t* child_value() const;
    // Get child value of child with specified name. Equivalent to child(name).child_value().
    const char_t* child_value(const char_t* name) const;
    // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value)
    bool set_name(const char_t* rhs);
    bool set_value(const char_t* rhs);
    // Add attribute with specified name. Returns added attribute, or empty attribute on errors.
    xml_attribute append_attribute(const char_t* name);
    xml_attribute prepend_attribute(const char_t* name);
    xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr);
    xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr);
    // Add a copy of the specified attribute. Returns added attribute, or empty attribute on errors.
    xml_attribute append_copy(const xml_attribute& proto);
    xml_attribute prepend_copy(const xml_attribute& proto);
    xml_attribute insert_copy_after(const xml_attribute& proto, const xml_attribute& attr);
    xml_attribute insert_copy_before(const xml_attribute& proto, const xml_attribute& attr);
    // Add child node with specified type. Returns added node, or empty node on errors.
    xml_node append_child(xml_node_type type = node_element);
    xml_node prepend_child(xml_node_type type = node_element);
    xml_node insert_child_after(xml_node_type type, const xml_node& node);
    xml_node insert_child_before(xml_node_type type, const xml_node& node);
    // Add child element with specified name. Returns added node, or empty node on errors.
    xml_node append_child(const char_t* name);
    xml_node prepend_child(const char_t* name);
    xml_node insert_child_after(const char_t* name, const xml_node& node);
    xml_node insert_child_before(const char_t* name, const xml_node& node);
    // Add a copy of the specified node as a child. Returns added node, or empty node on errors.
    xml_node append_copy(const xml_node& proto);
    xml_node prepend_copy(const xml_node& proto);
    xml_node insert_copy_after(const xml_node& proto, const xml_node& node);
    xml_node insert_copy_before(const xml_node& proto, const xml_node& node);
    // Move the specified node to become a child of this node. Returns moved node, or empty node on errors.
    xml_node append_move(const xml_node& moved);
    xml_node prepend_move(const xml_node& moved);
    xml_node insert_move_after(const xml_node& moved, const xml_node& node);
    xml_node insert_move_before(const xml_node& moved, const xml_node& node);
    // Remove specified attribute
    bool remove_attribute(const xml_attribute& a);
    bool remove_attribute(const char_t* name);
    // Remove all attributes
    bool remove_attributes();
    // Remove specified child
    bool remove_child(const xml_node& n);
    bool remove_child(const char_t* name);
    // Remove all children
    bool remove_children();
    // Parses buffer as an XML document fragment and appends all nodes as children of the current node.
    // Copies/converts the buffer, so it may be deleted or changed after the function returns.
    // Note: append_buffer allocates memory that has the lifetime of the owning document; removing the appended nodes does not immediately reclaim that memory.
    xml_parse_result append_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
    // Find attribute using predicate. Returns first attribute for which predicate returned true.
    template <typename Predicate> xml_attribute find_attribute(Predicate pred) const
    {
        if (!_root) return xml_attribute();
        for (xml_attribute attrib = first_attribute(); attrib; attrib = attrib.next_attribute())
            if (pred(attrib))
                return attrib;
        return xml_attribute();
    }
    // Find child node using predicate. Returns first child for which predicate returned true.
    template <typename Predicate> xml_node find_child(Predicate pred) const
    {
        if (!_root) return xml_node();
        for (xml_node node = first_child(); node; node = node.next_sibling())
            if (pred(node))
                return node;
        return xml_node();
    }
    // Find node from subtree using predicate. Returns first node from subtree (depth-first), for which predicate returned true.
    template <typename Predicate> xml_node find_node(Predicate pred) const
    {
        if (!_root) return xml_node();
        xml_node cur = first_child();
        while (cur._root && cur._root != _root)
        {
            if (pred(cur)) return cur;
            if (cur.first_child()) cur = cur.first_child();
            else if (cur.next_sibling()) cur = cur.next_sibling();
            else
            {
                while (!cur.next_sibling() && cur._root != _root) cur = cur.parent();
                if (cur._root != _root) cur = cur.next_sibling();
            }
        }
        return xml_node();
    }
    // Find child node by attribute name/value
    xml_node find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const;
    xml_node find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const;
#ifndef PUGIXML_NO_STL
    // Get the absolute node path from root as a text string.
    string_t path(char_t delimiter = '/') const;
#endif
    // Search for a node by path consisting of node names and . or .. elements.
    xml_node first_element_by_path(const char_t* path, char_t delimiter = '/') const;
    // Recursively traverse subtree with xml_tree_walker
    bool traverse(xml_tree_walker& walker);
#ifndef PUGIXML_NO_XPATH
    // Select single node by evaluating XPath query. Returns first node from the resulting node set.
    xpath_node select_node(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const;
    xpath_node select_node(const xpath_query& query) const;
    // Select node set by evaluating XPath query
    xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const;
    xpath_node_set select_nodes(const xpath_query& query) const;
    // (deprecated: use select_node instead) Select single node by evaluating XPath query.
    PUGIXML_DEPRECATED xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const;
    PUGIXML_DEPRECATED xpath_node select_single_node(const xpath_query& query) const;
#endif
    // Print subtree using a writer object
    void print(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
#ifndef PUGIXML_NO_STL
    // Print subtree to stream
    void print(std::basic_ostream<char, std::char_traits<char> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
    void print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, unsigned int depth = 0) const;
#endif
    // Child nodes iterators
    typedef xml_node_iterator iterator;
    iterator begin() const;
    iterator end() const;
    // Attribute iterators
    typedef xml_attribute_iterator attribute_iterator;
    attribute_iterator attributes_begin() const;
    attribute_iterator attributes_end() const;
    // Range-based for support
    xml_object_range<xml_node_iterator> children() const;
    xml_object_range<xml_named_node_iterator> children(const char_t* name) const;
    xml_object_range<xml_attribute_iterator> attributes() const;
    // Get node offset in parsed file/string (in char_t units) for debugging purposes
    ptrdiff_t offset_debug() const;
    // Get hash value (unique for handles to the same object)
    size_t hash_value() const;
    // Get internal pointer
    xml_node_struct* internal_object() const;
};





Add Discussion as Guest

Log in