xmlpp::Node Class Reference

Represents XML Nodes. More...

`#include <libxml++/nodes/node.h>`

Inheritance diagram for xmlpp::Node:

## Public Types

using | const_NodeList = std::list<const Node*>
using | const_NodeSet = std::vector<const Node*>
using | NodeList = std::list<Node*>
using | NodeSet = std::vector<Node*>
using | PrefixNsMap = std::map<ustring, ustring>
| A map of namespace prefixes to namespace URIs.

## Public Member Functions

| Node (_xmlNode * node)
| ~Node () override
| Destructor.
const _xmlNode * | cobj () const noexcept
| Access the underlying libxml implementation.
_xmlNode * | cobj () noexcept
| Access the underlying libxml implementation.
bool | eval_to_boolean (const ustring & xpath, const PrefixNsMap & namespaces, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
bool | eval_to_boolean (const ustring & xpath, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
double | eval_to_number (const ustring & xpath, const PrefixNsMap & namespaces, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
double | eval_to_number (const ustring & xpath, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
ustring | eval_to_string (const ustring & xpath, const PrefixNsMap & namespaces, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
ustring | eval_to_string (const ustring & xpath, XPathResultType * result_type=nullptr) const
| Evaluate an XPath expression.
std::variant< NodeSet, bool, double, ustring > | eval_xpath (const ustring & xpath, const PrefixNsMap & namespaces={})
| Evaluate an XPath expression.
std::variant< const_NodeSet, bool, double, ustring > | eval_xpath (const ustring & xpath, const PrefixNsMap & namespaces={}) const
| Evaluate an XPath expression.
NodeSet | find (const ustring & xpath)
| Find nodes from an XPath expression.
const_NodeSet | find (const ustring & xpath) const
| Find nodes from an XPath expression.
NodeSet | find (const ustring & xpath, const PrefixNsMap & namespaces)
| Find nodes from an XPath expression.
const_NodeSet | find (const ustring & xpath, const PrefixNsMap & namespaces) const
| Find nodes from an XPath expression.
NodeList | get_children (const ustring & name=ustring())
| Obtain the list of child nodes.
const_NodeList | get_children (const ustring & name=ustring()) const
| Obtain the list of child nodes.
Node * | get_first_child (const ustring & name=ustring())
| Get the first child of this node.
const Node * | get_first_child (const ustring & name=ustring()) const
| Get the first child of this node.
int | get_line () const
| Discover at what line number this node occurs in the XML file.
ustring | get_name () const
| Get the name of this node.
std::optional< ustring > | get_name2 () const
| Get the name of this node.
ustring | get_namespace_prefix () const
| Get the namespace prefix of this node.
std::optional< ustring > | get_namespace_prefix2 () const
| Get the namespace prefix of this node.
ustring | get_namespace_uri () const
| Get the namespace URI of this node.
std::optional< ustring > | get_namespace_uri2 () const
| Get the namespace URI of this node.
Node * | get_next_sibling ()
| Get the next sibling for this node.
const Node * | get_next_sibling () const
| Get the next sibling for this node.
Element * | get_parent ()
| Get the parent element for this node.
const Element * | get_parent () const
| Get the parent element for this node.
ustring | get_path () const
| Get the XPath of this node.
std::optional< ustring > | get_path2 () const
| Get the XPath of this node.
Node * | get_previous_sibling ()
| Get the previous sibling for this node.
const Node * | get_previous_sibling () const
| Get the previous sibling for this node .
Node * | import_node (const Node * node, bool recursive=true)
| Import node(s) from another document under this node, without affecting the source node.
void | set_name (const ustring & name)
| Set the name of this node.
void | set_namespace (const ustring & ns_prefix)
| Set the namespace prefix used by the node.
Public Member Functions inherited from xmlpp::NonCopyable
| NonCopyable (const NonCopyable &)=delete
| NonCopyable (NonCopyable &&)=delete
NonCopyable & | operator= (const NonCopyable &)=delete
NonCopyable & | operator= (NonCopyable &&)=delete

## Static Public Member Functions

static void | create_wrapper (_xmlNode * node)
| Construct the correct C++ instance for a given libxml C struct instance.
static void | free_wrappers (_xmlNode * node)
| Delete the C++ instance for a given libxml C struct instance, and also recursively destroy the C++ instances for any children.
static void | remove_node (Node * node)
| Remove a node and its children.

## Additional Inherited Members

Protected Member Functions inherited from xmlpp::NonCopyable
| NonCopyable () noexcept
virtual | ~NonCopyable ()

## Detailed Description

Represents XML Nodes.

You should never new and delete Nodes. The Parser will create and manage them for you. Furthermore, Document and Element have methods for adding Nodes to a Document.

## Member Typedef Documentation

## const_NodeList

using xmlpp::Node::const_NodeList = std::list<const Node*>

## const_NodeSet

using xmlpp::Node::const_NodeSet = std::vector<const Node*>

## NodeList

using xmlpp::Node::NodeList = std::list<Node*>

## NodeSet

using xmlpp::Node::NodeSet = std::vector<Node*>

## PrefixNsMap

using xmlpp::Node::PrefixNsMap = std::map<ustring, ustring>

A map of namespace prefixes to namespace URIs.

## Constructor & Destructor Documentation

## Node()

xmlpp::Node::Node | ( | _xmlNode * | node | ) |

explicit

Exceptions

  xmlpp::internal_error | If node is nullptr.

## ~Node()

xmlpp::Node::~Node | ( |  | ) |

override

Destructor.

Does not destroy the underlying xmlNode. The xmlNode is owned by a xmlDoc document. If you want to also destroy the xmlNode, use remove_node().

## Member Function Documentation

## cobj() [1/2]

const _xmlNode * xmlpp::Node::cobj | ( |  | ) | const

noexcept

Access the underlying libxml implementation.

## cobj() [2/2]

_xmlNode * xmlpp::Node::cobj | ( |  | ) |

noexcept

Access the underlying libxml implementation.

## create_wrapper()

void xmlpp::Node::create_wrapper | ( | _xmlNode * | node | ) |

static

Construct the correct C++ instance for a given libxml C struct instance.

This is only for use by the libxml++ implementation.

Parameters

  node | A pointer to an xmlNode or a "derived" struct, such as xmlDoc, xmlAttr, etc.

## eval_to_boolean() [1/2]

bool xmlpp::Node::eval_to_boolean | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  | namespaces | A map of namespace prefixes to namespace URIs to be used while evaluating.
  [out] | result_type | Result type of the XPath expression before conversion to boolean. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type boolean, it is converted to boolean.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_to_boolean() [2/2]

bool xmlpp::Node::eval_to_boolean | ( | const ustring & | xpath,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  [out] | result_type | Result type of the XPath expression before conversion to boolean. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type boolean, it is converted to boolean.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_to_number() [1/2]

double xmlpp::Node::eval_to_number | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  | namespaces | A map of namespace prefixes to namespace URIs to be used while evaluating.
  [out] | result_type | Result type of the XPath expression before conversion to number. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type number, it is converted to number.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_to_number() [2/2]

double xmlpp::Node::eval_to_number | ( | const ustring & | xpath,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  [out] | result_type | Result type of the XPath expression before conversion to number. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type number, it is converted to number.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_to_string() [1/2]

ustring xmlpp::Node::eval_to_string | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  | namespaces | A map of namespace prefixes to namespace URIs to be used while evaluating.
  [out] | result_type | Result type of the XPath expression before conversion to string. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type string, it is converted to string.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_to_string() [2/2]

ustring xmlpp::Node::eval_to_string | ( | const ustring & | xpath,
|  | XPathResultType * | result_type = nullptr ) const

Evaluate an XPath expression.

Parameters

  | xpath | The XPath expression.
  [out] | result_type | Result type of the XPath expression before conversion to string. If nullptr, the result type is not returned.

Returns
  The value of the XPath expression. If the value is not of type string, it is converted to string.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error |

Since libxml++ 2.36

## eval_xpath() [1/2]

std::variant< NodeSet, bool, double, ustring > xmlpp::Node::eval_xpath | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces = {} )

Evaluate an XPath expression.

Parameters

  xpath | The XPath expression.
  namespaces | A map of namespace prefixes to namespace URIs to be used while evaluating.

Returns
  The resulting NodeSet (XPathResultType::NODESET), bool (XPathResultType::BOOLEAN), double (XPathResultType::NUMBER) or ustring (XPathResultType::STRING).

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset, boolean, number or string.

Since libxml++ 5.4

## eval_xpath() [2/2]

std::variant< const_NodeSet, bool, double, ustring > xmlpp::Node::eval_xpath | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces = {} ) const

Evaluate an XPath expression.

Parameters

  xpath | The XPath expression.
  namespaces | A map of namespace prefixes to namespace URIs to be used while evaluating.

Returns
  The resulting const_NodeSet (XPathResultType::NODESET), bool (XPathResultType::BOOLEAN), double (XPathResultType::NUMBER) or ustring (XPathResultType::STRING).

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset, boolean, number or string.

Since libxml++ 5.4

## find() [1/4]

NodeSet xmlpp::Node::find | ( | const ustring & | xpath | ) |

Find nodes from an XPath expression.

Parameters

  xpath | The XPath of the nodes.

Returns
  The resulting NodeSet.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset.

## find() [2/4]

const_NodeSet xmlpp::Node::find | ( | const ustring & | xpath | ) | const

Find nodes from an XPath expression.

Parameters

  xpath | The XPath of the nodes.

Returns
  The resulting const_NodeSet.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset.

## find() [3/4]

NodeSet xmlpp::Node::find | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces )

Find nodes from an XPath expression.

Parameters

  xpath | The XPath of the nodes.
  namespaces | A map of namespace prefixes to namespace URIs to be used while finding.

Returns
  The resulting NodeSet.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset.

## find() [4/4]

const_NodeSet xmlpp::Node::find | ( | const ustring & | xpath,
|  | const PrefixNsMap & | namespaces ) const

Find nodes from an XPath expression.

Parameters

  xpath | The XPath of the nodes.
  namespaces | A map of namespace prefixes to namespace URIs to be used while finding.

Returns
  The resulting const_NodeSet.

Exceptions

  xmlpp::exception | If the XPath expression cannot be evaluated.
  xmlpp::internal_error | If the result type is not nodeset.

## free_wrappers()

void xmlpp::Node::free_wrappers | ( | _xmlNode * | node | ) |

static

Delete the C++ instance for a given libxml C struct instance, and also recursively destroy the C++ instances for any children.

This is only for use by the libxml++ implementation.

Parameters

  node | A pointer to an xmlNode or a "derived" struct, such as xmlDoc, xmlAttr, etc.

## get_children() [1/2]

NodeList xmlpp::Node::get_children | ( | const ustring & | name = ustring() | ) |

Obtain the list of child nodes.

You may optionally obtain a list of only the child nodes which have a certain name.

Parameters

  name | The names of the child nodes to get. If you do not specify a name, then the list will contain all nodes, regardless of their names.

Returns
  The list of child nodes.

## get_children() [2/2]

const_NodeList xmlpp::Node::get_children | ( | const ustring & | name = ustring() | ) | const

Obtain the list of child nodes.

You may optionally obtain a list of only the child nodes which have a certain name.

Parameters

  name | The names of the child nodes to get. If you do not specify a name, then the list will contain all nodes, regardless of their names.

Returns
  The list of child nodes.

## get_first_child() [1/2]

Node * xmlpp::Node::get_first_child | ( | const ustring & | name = ustring() | ) |

Get the first child of this node.

You may optionally get the first child node which has a certain name.

Parameters

  name | The name of the requested child node, or an empty string.

Returns
  The first child, or nullptr if no child node (with the specified name) exists.

Since libxml++ 2.36

## get_first_child() [2/2]

const Node * xmlpp::Node::get_first_child | ( | const ustring & | name = ustring() | ) | const

Get the first child of this node.

You may optionally get the first child node which has a certain name.

Parameters

  name | The name of the requested child node, or an empty string.

Returns
  The first child, or nullptr if no child node (with the specified name) exists.

Since libxml++ 2.36

## get_line()

int xmlpp::Node::get_line | ( |  | ) | const

Discover at what line number this node occurs in the XML file.

Returns
  The line number.

## get_name()

ustring xmlpp::Node::get_name | ( |  | ) | const

Get the name of this node.

Returns
  The node's name.

Deprecated
  5.6: Use get_name2() instead.

## get_name2()

std::optional< ustring > xmlpp::Node::get_name2 | ( |  | ) | const

Get the name of this node.

Returns
  The node's name, if any, else no value.

Since libxml++ 5.6

## get_namespace_prefix()

ustring xmlpp::Node::get_namespace_prefix | ( |  | ) | const

Get the namespace prefix of this node.

Returns
  The node's namespace prefix. Can be an empty string.

Deprecated
  5.6: Use get_namespace_prefix2() instead.

## get_namespace_prefix2()

std::optional< ustring > xmlpp::Node::get_namespace_prefix2 | ( |  | ) | const

Get the namespace prefix of this node.

Returns
  The node's namespace prefix, or no value if the node has no namespace prefix.

Since libxml++ 5.6

## get_namespace_uri()

ustring xmlpp::Node::get_namespace_uri | ( |  | ) | const

Get the namespace URI of this node.

Returns
  The node's namespace URI. Can be an empty string.

Deprecated
  5.6: Use get_namespace_uri2() instead.

## get_namespace_uri2()

std::optional< ustring > xmlpp::Node::get_namespace_uri2 | ( |  | ) | const

Get the namespace URI of this node.

Returns
  The node's namespace URI, or no value if the node has no namespace URI.

Since libxml++ 5.6

## get_next_sibling() [1/2]

Node * xmlpp::Node::get_next_sibling | ( |  | ) |

Get the next sibling for this node.

Returns
  The next sibling, or nullptr if the node has no next sibling.

## get_next_sibling() [2/2]

const Node * xmlpp::Node::get_next_sibling | ( |  | ) | const

Get the next sibling for this node.

Returns
  The next sibling, or nullptr if the node has no next sibling.

## get_parent() [1/2]

Element * xmlpp::Node::get_parent | ( |  | ) |

Get the parent element for this node.

Returns
  The parent node, or nullptr if the node has no parent element.

## get_parent() [2/2]

const Element * xmlpp::Node::get_parent | ( |  | ) | const

Get the parent element for this node.

Returns
  The parent node, or nullptr if the node has no parent element.

## get_path()

ustring xmlpp::Node::get_path | ( |  | ) | const

Get the XPath of this node.

Returns
  The XPath of the node.

Deprecated
  5.6: Use get_path2() instead.

## get_path2()

std::optional< ustring > xmlpp::Node::get_path2 | ( |  | ) | const

Get the XPath of this node.

Returns
  The XPath of the node, or no value in case of error.

Since libxml++ 5.6

## get_previous_sibling() [1/2]

Node * xmlpp::Node::get_previous_sibling | ( |  | ) |

Get the previous sibling for this node.

Returns
  The previous sibling, or nullptr if the node has no previous sibling.

## get_previous_sibling() [2/2]

const Node * xmlpp::Node::get_previous_sibling | ( |  | ) | const

Get the previous sibling for this node .

Returns
  The previous sibling, or nullptr if the node has no previous sibling.

## import_node()

Node * xmlpp::Node::import_node | ( | const Node * | node,
|  | bool | recursive = true )

Import node(s) from another document under this node, without affecting the source node.

If the imported node is an attribute node, and this node has an attribute with the same name as the imported attribute, the existing attribute is destroyed before the imported attribute is added. Any pointer to a destroyed attribute node becomes invalid.

Parameters

  node | The node to copy and insert under the current node.
  recursive | Whether to import the child nodes also. Defaults to true.

Returns
  Usually the newly created node, but adjacent text nodes are merged, and the old text node with merged contents is returned.

Exceptions

  xmlpp::exception |

## remove_node()

void xmlpp::Node::remove_node | ( | Node * | node | ) |

static

Remove a node and its children.

The node is disconnected from its parent. The underlying libxml xmlNode instances are also removed.

Since libxml++ 3.0
  Replaces remove_child()

Parameters

  node | The node to remove. This Node and all its descendants will be deleted and therefore unusable after calling this method.

## set_name()

void xmlpp::Node::set_name | ( | const ustring & | name | ) |

Set the name of this node.

Parameters

  name | The new name for the node.

## set_namespace()

void xmlpp::Node::set_namespace | ( | const ustring & | ns_prefix | ) |

Set the namespace prefix used by the node.

If no such namespace prefix has been declared then this method will throw an exception.

Parameters

  ns_prefix | The namespace prefix.

Exceptions

  xmlpp::exception |