libxml++: xmlpp::Node Class Reference

libxml++ 2.42.0

Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
xmlpp::Node Class Reference

Represents XML Nodes. More...

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

Inheritance diagram for xmlpp::Node:
[legend]

## Public Types
typedef std::list< Node* > | NodeList
typedef std::map< Glib::ustring, Glib::ustring > | PrefixNsMap
| A map of namespace prefixes to namespace URIs. More...

## Public Member Functions
| Node (_xmlNode* node)
| ~Node () override
Element* | add_child (const Glib::ustring& name, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node. More...
Element* | add_child (xmlpp::Node* previous_sibling, const Glib::ustring& name, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node after the specified existing child node. More...
Element* | add_child_before (xmlpp::Node* next_sibling, const Glib::ustring& name, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node before the specified existing child node. More...
Element* | add_child_before_with_new_ns (xmlpp::Node* next_sibling, const Glib::ustring& name, const Glib::ustring& ns_uri, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node before the specified existing child node. More...
Element* | add_child_with_new_ns (const Glib::ustring& name, const Glib::ustring& ns_uri, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node. More...
Element* | add_child_with_new_ns (xmlpp::Node* previous_sibling, const Glib::ustring& name, const Glib::ustring& ns_uri, const Glib::ustring& ns_prefix=Glib::ustring())
| Add a child element to this node after the specified existing child node. More...
_xmlNode* | cobj ()
| Access the underlying libxml implementation. More...
const _xmlNode* | cobj () const
| Access the underlying libxml implementation. More...
bool | eval_to_boolean (const Glib::ustring& xpath, const PrefixNsMap& namespaces, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
bool | eval_to_boolean (const Glib::ustring& xpath, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
double | eval_to_number (const Glib::ustring& xpath, const PrefixNsMap& namespaces, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
double | eval_to_number (const Glib::ustring& xpath, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
Glib::ustring | eval_to_string (const Glib::ustring& xpath, const PrefixNsMap& namespaces, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
Glib::ustring | eval_to_string (const Glib::ustring& xpath, XPathResultType* result_type=nullptr) const
| Evaluate an XPath expression. More...
NodeSet | find (const Glib::ustring& xpath) const
| Find nodes from an XPath expression. More...
NodeSet | find (const Glib::ustring& xpath, const PrefixNsMap& namespaces) const
| Find nodes from an XPath expression. More...
NodeList | get_children (const Glib::ustring& name=Glib::ustring())
| Obtain the list of child nodes. More...
const NodeList | get_children (const Glib::ustring& name=Glib::ustring()) const
| Obtain the list of child nodes. More...
Node* | get_first_child (const Glib::ustring& name=Glib::ustring())
| Get the first child of this node. More...
const Node* | get_first_child (const Glib::ustring& name=Glib::ustring()) const
| Get the first child of this node. More...
int | get_line () const
| Discover at what line number this node occurs in the XML file. More...
Glib::ustring | get_name () const
| Get the name of this node. More...
Glib::ustring | get_namespace_prefix () const
| Get the namespace prefix of this node. More...
Glib::ustring | get_namespace_uri () const
| Get the namespace URI of this node. More...
Node* | get_next_sibling ()
| Get the next sibling for this node. More...
const Node* | get_next_sibling () const
| Get the next sibling for this node. More...
Element* | get_parent ()
| Get the parent element for this node. More...
const Element* | get_parent () const
| Get the parent element for this node. More...
Glib::ustring | get_path () const
| Get the XPath of this node. More...
Node* | get_previous_sibling ()
| Get the previous sibling for this node. More...
const Node* | get_previous_sibling () const
| Get the previous sibling for this node . More...
Node* | import_node (const Node* node, bool recursive=true)
| Import node(s) from another document under this node, without affecting the source node. More...
void | remove_child (Node* node)
| Remove the child node. More...
void | set_name (const Glib::ustring& name)
| Set the name of this node. More...
void | set_namespace (const Glib::ustring& ns_prefix)
| Set the namespace prefix used by the node. More...

## Static Public Member Functions
static void | create_wrapper (_xmlNode* node)
| Construct the correct C++ instance for a given libxml C struct instance. More...
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. More...

## Protected Member Functions
_xmlNode* | create_new_child_node (const Glib::ustring& name, const Glib::ustring& ns_prefix)
| Create the C instance ready to be added to the parent node. More...
_xmlNode* | create_new_child_node_with_new_ns (const Glib::ustring& name, const Glib::ustring& ns_uri, const Glib::ustring& ns_prefix)
| Create the C instance ready to be added to the parent node. More...
Protected Member Functions inherited from xmlpp::NonCopyable
| NonCopyable ()
| NonCopyable (const NonCopyable&)=delete
virtual | ~NonCopyable ()
NonCopyable& | operator= (const NonCopyable&)=delete

## Detailed Description

Represents XML Nodes.

You should never new or delete Nodes. The Parser will create and manage them for you.

## Member Typedef Documentation

## ◆ NodeList

typedef std::list<Node*> xmlpp::Node::NodeList

## ◆ PrefixNsMap

typedef std::map<Glib::ustring, Glib::ustring> xmlpp::Node::PrefixNsMap

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

## Member Function Documentation

## ◆ add_child() [1/2]

Element* xmlpp::Node::add_child | ( | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node.

This node must be an element node.

Parameters

  name | The new node name
  ns_prefix | The namespace prefix. If the prefix has not been declared then this method will throw an exception.

Returns
  The newly-created element

Exceptions

  xmlpp::exception | If a namespace prefix is specified, but has not been declared.
  xmlpp::internal_error | If this node is not an element node, or the child node cannot be created.

## ◆ add_child() [2/2]

Element* xmlpp::Node::add_child | ( | xmlpp::Node* | previous_sibling,
|  | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node after the specified existing child node.

This node must be an element node.

Since libxml++ 2.24:

Parameters

  previous_sibling | An existing child node.
  name | The new node name
  ns_prefix | The namespace prefix. If the prefix has not been declared then this method will throw an exception.

Returns
  The newly-created element

Exceptions

  xmlpp::exception | If a namespace prefix is specified, but has not been declared.
  xmlpp::internal_error | If this node is not an element node, or the child node cannot be created.

## ◆ add_child_before()

Element* xmlpp::Node::add_child_before | ( | xmlpp::Node* | next_sibling,
|  | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node before the specified existing child node.

This node must be an element node.

Since libxml++ 2.24:

Parameters

  next_sibling | An existing child node.
  name | The new node name
  ns_prefix | The namespace prefix. If the prefix has not been declared then this method will throw an exception.

Returns
  The newly-created element

Exceptions

  xmlpp::exception | If a namespace prefix is specified, but has not been declared.
  xmlpp::internal_error | If this node is not an element node, or the child node cannot be created.

## ◆ add_child_before_with_new_ns()

Element* xmlpp::Node::add_child_before_with_new_ns | ( | xmlpp::Node* | next_sibling,
|  | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_uri,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node before the specified existing child node.

This node must be an element node.

Since libxml++ 2.38:

Parameters

  next_sibling | An existing child node.
  name | The new node name.
  ns_uri | The namespace to associate with the prefix, or to use as the default namespace if no prefix is specified.
  ns_prefix | The prefix of the node's namespace. If no prefix is specified then the namespace URI will be the default namespace.

Returns
  The newly-created element.

Exceptions

  xmlpp::internal_error | If this node is not an element node, or the child node or the namespace node cannot be created.

## ◆ add_child_with_new_ns() [1/2]

Element* xmlpp::Node::add_child_with_new_ns | ( | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_uri,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node.

This node must be an element node.

Since libxml++ 2.38:

Parameters

  name | The new node name.
  ns_uri | The namespace to associate with the prefix, or to use as the default namespace if no prefix is specified.
  ns_prefix | The prefix of the node's namespace. If no prefix is specified then the namespace URI will be the default namespace.

Returns
  The newly-created element.

Exceptions

  xmlpp::internal_error | If this node is not an element node, or the child node or the namespace node cannot be created.

## ◆ add_child_with_new_ns() [2/2]

Element* xmlpp::Node::add_child_with_new_ns | ( | xmlpp::Node* | previous_sibling,
|  | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_uri,
|  | const Glib::ustring & | ns_prefix = `Glib::ustring()`
| ) |  |

Add a child element to this node after the specified existing child node.

This node must be an element node.

Since libxml++ 2.38:

Parameters

  previous_sibling | An existing child node.
  name | The new node name.
  ns_uri | The namespace to associate with the prefix, or to use as the default namespace if no prefix is specified.
  ns_prefix | The prefix of the node's namespace. If no prefix is specified then the namespace URI will be the default namespace.

Returns
  The newly-created element.

Exceptions

  xmlpp::internal_error | If this node is not an element node, or the child node or the namespace node cannot be created.

## ◆ cobj() [1/2]

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

Access the underlying libxml implementation.

## ◆ cobj() [2/2]

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

Access the underlying libxml implementation.

## ◆ create_new_child_node()

_xmlNode* xmlpp::Node::create_new_child_node | ( | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_prefix
| ) |  |
| protected

Create the C instance ready to be added to the parent node.

## ◆ create_new_child_node_with_new_ns()

_xmlNode* xmlpp::Node::create_new_child_node_with_new_ns | ( | const Glib::ustring & | name,
|  | const Glib::ustring & | ns_uri,
|  | const Glib::ustring & | ns_prefix
| ) |  |
| protected

Create the C instance ready to be added to the parent node.

## ◆ create_wrapper()

static 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 Glib::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 Glib::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 Glib::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 Glib::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]

Glib::ustring xmlpp::Node::eval_to_string | ( | const Glib::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]

Glib::ustring xmlpp::Node::eval_to_string | ( | const Glib::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:

## ◆ find() [1/2]

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

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/2]

NodeSet xmlpp::Node::find | ( | const Glib::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 NodeSet.

Exceptions

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

## ◆ free_wrappers()

static 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 Glib::ustring & | name = `Glib::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 Glib::ustring & | name = `Glib::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 Glib::ustring & | name = `Glib::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 Glib::ustring & | name = `Glib::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()

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

Get the name of this node.

Returns
  The node's name.

## ◆ get_namespace_prefix()

Glib::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.

## ◆ get_namespace_uri()

Glib::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.

## ◆ 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()

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

Get the XPath of this node.

Returns
  The XPath of the node.

## ◆ 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_child()

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

Remove the child node.

Parameters

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

## ◆ set_name()

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

Set the name of this node.

Parameters

  name | The new name for the node.

## ◆ set_namespace()

void xmlpp::Node::set_namespace | ( | const Glib::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 |