libxml++: 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< Glib::ustring, Glib::ustring >
 A map of namespace prefixes to namespace URIs. More...

 

Public Member Functions

 Node (_xmlNode* node)
 
 ~Node () override
 Destructor. More...

 
_xmlNode* cobj () noexcept
 Access the underlying libxml implementation. More...

 
const _xmlNode* cobj () const noexcept
 Access the underlying libxml implementation. More...

 
bool eval_to_boolean (const Glib::ustring& xpath, XPathResultType* result_type=nullptr) const
 Evaluate an XPath expression. More...

 
bool eval_to_boolean (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...

 
double eval_to_number (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...

 
Glib::ustring eval_to_string (const Glib::ustring& xpath, const PrefixNsMap& namespaces, XPathResultType* result_type=nullptr) const
 Evaluate an XPath expression. More...

 
NodeSet find (const Glib::ustring& xpath)
 Find nodes from an XPath expression. More...

 
const_NodeSet find (const Glib::ustring& xpath) const
 Find nodes from an XPath expression. More...

 
NodeSet find (const Glib::ustring& xpath, const PrefixNsMap& namespaces)
 Find nodes from an XPath expression. More...

 
const_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...

 
const Nodeget_first_child (const Glib::ustring& name=Glib::ustring()) const
 Get the first child of this node. More...

 
Nodeget_first_child (const Glib::ustring& name=Glib::ustring())
 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...

 
const Nodeget_next_sibling () const
 Get the next sibling for this node. More...

 
Nodeget_next_sibling ()
 Get the next sibling for this node. More...

 
const Elementget_parent () const
 Get the parent element for this node. More...

 
Elementget_parent ()
 Get the parent element for this node. More...

 
Glib::ustring get_path () const
 Get the XPath of this node. More...

 
const Nodeget_previous_sibling () const
 Get the previous sibling for this node . More...

 
Nodeget_previous_sibling ()
 Get the previous sibling for this node. More...

 
Nodeimport_node (const Node* node, bool recursive=true)
 Import node(s) from another document under this node, without affecting the source 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...

 
static void remove_node (Node* node)
 Remove a node and its children. More...

 

Additional Inherited Members

- Protected Member Functions inherited from xmlpp::NonCopyable
 NonCopyable () noexcept
 
 NonCopyable (const NonCopyable&)=delete
 
 NonCopyable (NonCopyable&&)=delete
 
virtual ~NonCopyable ()
 
NonCopyableoperator= (const NonCopyable&)=delete
 
NonCopyableoperator= (NonCopyable&&)=delete
 

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

A map of namespace prefixes to namespace URIs.

Constructor & Destructor Documentation

xmlpp::Node::Node ( _xmlNode *  node)
explicit
Exceptions
xmlpp::internal_errorIf node is nullptr.
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

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

Access the underlying libxml implementation.

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

Access the underlying libxml implementation.

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
nodeA pointer to an xmlNode or a "derived" struct, such as xmlDoc, xmlAttr, etc.
bool xmlpp::Node::eval_to_boolean ( const Glib::ustring xpath,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
bool xmlpp::Node::eval_to_boolean ( const Glib::ustring xpath,
const PrefixNsMap namespaces,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
namespacesA map of namespace prefixes to namespace URIs to be used while evaluating.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
double xmlpp::Node::eval_to_number ( const Glib::ustring xpath,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
double xmlpp::Node::eval_to_number ( const Glib::ustring xpath,
const PrefixNsMap namespaces,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
namespacesA map of namespace prefixes to namespace URIs to be used while evaluating.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
Glib::ustring xmlpp::Node::eval_to_string ( const Glib::ustring xpath,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
Glib::ustring xmlpp::Node::eval_to_string ( const Glib::ustring xpath,
const PrefixNsMap namespaces,
XPathResultType result_type = nullptr 
) const

Evaluate an XPath expression.

Parameters
xpathThe XPath expression.
namespacesA map of namespace prefixes to namespace URIs to be used while evaluating.
[out]result_typeResult 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::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_error
Since libxml++ 2.36:
NodeSet xmlpp::Node::find ( const Glib::ustring xpath)

Find nodes from an XPath expression.

Parameters
xpathThe XPath of the nodes.
Returns
The resulting NodeSet.
Exceptions
xmlpp::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_errorIf the result type is not nodeset.
const_NodeSet xmlpp::Node::find ( const Glib::ustring xpath) const

Find nodes from an XPath expression.

Parameters
xpathThe XPath of the nodes.
Returns
The resulting const_NodeSet.
Exceptions
xmlpp::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_errorIf the result type is not nodeset.
NodeSet xmlpp::Node::find ( const Glib::ustring xpath,
const PrefixNsMap namespaces 
)

Find nodes from an XPath expression.

Parameters
xpathThe XPath of the nodes.
namespacesA map of namespace prefixes to namespace URIs to be used while finding.
Returns
The resulting NodeSet.
Exceptions
xmlpp::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_errorIf the result type is not nodeset.
const_NodeSet xmlpp::Node::find ( const Glib::ustring xpath,
const PrefixNsMap namespaces 
) const

Find nodes from an XPath expression.

Parameters
xpathThe XPath of the nodes.
namespacesA map of namespace prefixes to namespace URIs to be used while finding.
Returns
The resulting const_NodeSet.
Exceptions
xmlpp::exceptionIf the XPath expression cannot be evaluated.
xmlpp::internal_errorIf the result type is not nodeset.
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
nodeA pointer to an xmlNode or a "derived" struct, such as xmlDoc, xmlAttr, etc.
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
nameThe 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.
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
nameThe 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.
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
nameThe 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:
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
nameThe 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:
int xmlpp::Node::get_line ( ) const

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

Returns
The line number.
Glib::ustring xmlpp::Node::get_name ( ) const

Get the name of this node.

Returns
The node's name.
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.
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.
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.
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.
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.
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.
Glib::ustring xmlpp::Node::get_path ( ) const

Get the XPath of this node.

Returns
The XPath of the node.
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.
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.
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
nodeThe node to copy and insert under the current node.
recursiveWhether 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
static 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
nodeThe node to remove. This Node and all its descendants will be deleted and therefore unusable after calling this method.
void xmlpp::Node::set_name ( const Glib::ustring name)

Set the name of this node.

Parameters
nameThe new name for the node.
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_prefixThe namespace prefix.
Exceptions
xmlpp::exception