From 3b17184379fcaaeb7f1fbe08018b7fedf2640b3b Mon Sep 17 00:00:00 2001
From: Arseny Kapoulkine pugixml 1.14 manual
-
-
XPath for XPath reference. As discussed in C++ interface, there are two types of handles to tree data - xml_node and xml_attribute. The handles have special null (empty) values which propagate through various functions and thus are useful for writing more concise code; see this description for details. The documentation in this section will explicitly state the results of all function in case of null inputs.
The internal representation of the document is a tree, where each node has a list of child nodes (the order of children corresponds to their order in the XML representation), and additionally element nodes have a list of attributes, which is also ordered. Several functions are provided in order to let you get from one node in the tree to the other. These functions roughly correspond to the internal representation, and thus are usually building blocks for other methods of traversing (i.e. XPath traversals are based on these functions).
@@ -2055,7 +2055,7 @@Apart from structural information (parent, child nodes, attributes), nodes can have name and value, both of which are strings. Depending on node type, name or value may be absent. node_document nodes do not have a name or value, node_element and node_declaration nodes always have a name but never have a value, node_pcdata, node_cdata, node_comment and node_doctype nodes never have a name but always have a value (it may be empty though), node_pi nodes always have a name and a value (again, value may be empty). In order to get node’s name or value, you can use the following functions:
@@ -2090,7 +2090,7 @@
All attributes have name and value, both of which are strings (value may be empty). There are two corresponding accessors, like for xml_node
:
Since a lot of document traversal consists of finding the node/attribute with the correct name, there are special functions for that purpose:
@@ -2258,7 +2258,7 @@If your C++ compiler supports range-based for-loop (this is a C++11 feature, at the time of writing it’s supported by Microsoft Visual Studio 2012+, GCC 4.6+ and Clang 3.0+), you can use it to enumerate nodes/attributes. Additional helpers are provided to support this; note that they are also compatible with Boost Foreach, and possibly other pre-C++11 foreach facilities.
@@ -2319,7 +2319,7 @@
Child node lists and attribute lists are simply double-linked lists; while you can use previous_sibling
/next_sibling
and other such functions for iteration, pugixml additionally provides node and attribute iterators, so that you can treat nodes as containers of other nodes or attributes:
The methods described above allow traversal of immediate children of some node; if you want to do a deep tree traversal, you’ll have to do it via a recursive function or some equivalent method. However, pugixml provides a helper for depth-first traversal of a subtree. In order to use it, you have to implement xml_tree_walker
interface and to call traverse
function:
While there are existing functions for getting a node/attribute with known contents, they are often not sufficient for simple queries. As an alternative for manual iteration through nodes/attributes until the needed one is found, you can make a predicate and call one of find_
functions:
It is common to store data as text contents of some node - i.e. <node><description>This is a node</description></node>
. In this case, <description>
node does not have a value, but instead has a child of type node_pcdata with value "This is a node"
. pugixml provides a special class, xml_text
, to work with such data. Working with text objects to modify data is described in the documentation for modifying document data; this section describes the access interface of xml_text
.
If you need to get the document root of some node, you can use the following function:
As discussed before, nodes can have name and value, both of which are strings. Depending on node type, name or value may be absent. node_document nodes do not have a name or value, node_element and node_declaration nodes always have a name but never have a value, node_pcdata, node_cdata, node_comment and node_doctype nodes never have a name but always have a value (it may be empty though), node_pi nodes always have a name and a value (again, value may be empty). In order to set node’s name or value, you can use the following functions:
@@ -2691,7 +2691,7 @@All attributes have name and value, both of which are strings (value may be empty). You can set them with the following functions:
@@ -2794,7 +2794,7 @@Nodes and attributes do not exist without a document tree, so you can’t create them without adding them to some document. A node or attribute can be created at the end of node/attribute list or before/after some other node:
@@ -2889,7 +2889,7 @@If you do not want your document to contain some node or attribute, you can remove it with one of the following functions:
@@ -2953,7 +2953,7 @@pugixml provides a special class, xml_text
, to work with text contents stored as a value of some node, i.e. <node><description>This is a node</description></node>
. Working with text objects to retrieve data is described in the documentation for accessing document data; this section describes the modification interface of xml_text
.
With the help of previously described functions, it is possible to create trees with any contents and structure, including cloning the existing data. However since this is an often needed operation, pugixml provides built-in node/attribute cloning facilities. Since nodes and attributes do not exist without a document tree, you can’t create a standalone copy - you have to immediately insert it somewhere in the tree. For this, you can use one of the following functions:
@@ -3121,7 +3121,7 @@Sometimes instead of cloning a node you need to move an existing node to a different position in a tree. This can be accomplished by copying the node and removing the original; however, this is expensive since it results in a lot of extra operations. For moving nodes within the same document tree, you can use of the following functions instead:
@@ -3158,7 +3158,7 @@pugixml provides several ways to assemble an XML document from other XML documents. Assuming there is a set of document fragments, represented as in-memory buffers, the implementation choices are as follows: