xmlwrapp
Lightweight C++ XML parsing library
|
The xml::node class is used to hold information about one XML node. More...
#include <node.h>
Classes | |
struct | cdata |
Helper struct for creating a xml::node of type_cdata. More... | |
struct | comment |
Helper struct for creating a xml::node of type_comment. More... | |
class | const_iterator |
The xml::node::const_iterator provides a way to access children nodes similar to a standard C++ container. More... | |
class | iterator |
The xml::node::iterator provides a way to access children nodes similar to a standard C++ container. More... | |
struct | pi |
Helper struct for creating a xml::node of type_pi. More... | |
struct | text |
Helper struct for creating a xml::node of type_text. More... | |
Public Types | |
enum | node_type { type_element, type_text, type_cdata, type_pi, type_comment, type_entity, type_entity_ref, type_xinclude, type_document, type_document_type, type_document_frag, type_notation, type_dtd, type_dtd_element, type_dtd_attribute, type_dtd_entity, type_dtd_namespace } |
enum for the different types of XML nodes More... | |
typedef std::size_t | size_type |
size type More... | |
Public Member Functions | |
node () | |
Construct a new blank xml::node. More... | |
node (const char *name) | |
Construct a new xml::node and set the name of the node. More... | |
node (const char *name, const char *content) | |
Construct a new xml::node given a name and content. More... | |
node (cdata cdata_info) | |
Construct a new xml::node that is of type_cdata. More... | |
node (comment comment_info) | |
Construct a new xml::node that is of type_comment. More... | |
node (pi pi_info) | |
Construct a new xml::node that is of type_pi. More... | |
node (text text_info) | |
Construct a new xml::node that is of type_text. More... | |
node (const node &other) | |
Construct a new xml::node by copying another xml::node. More... | |
node & | operator= (const node &other) |
Make this node equal to some other node via assignment. More... | |
~node () | |
Class destructor. More... | |
void | set_name (const char *name) |
Set the name of this xml::node. More... | |
const char * | get_name () const |
Get the name of this xml::node. More... | |
void | set_content (const char *content) |
Set the content of a node. More... | |
void | set_text_content (const char *content) |
Set the content of a node to given text. More... | |
const char * | get_content () const |
Get the content for this text node. More... | |
node_type | get_type () const |
Get this node's "type". More... | |
xml::attributes & | get_attributes () |
Get the list of attributes. More... | |
const xml::attributes & | get_attributes () const |
Get the list of attributes. More... | |
const char * | get_namespace () const |
Get the namespace of this xml::node. More... | |
void | set_namespace (const std::string &href) |
Set the default namespace of this xml::node. More... | |
bool | is_text () const |
Find out if this node is a text node or something like a text node, CDATA for example. More... | |
void | push_back (const node &child) |
Add a child xml::node to this node. More... | |
void | swap (node &other) |
Swap this node with another one. More... | |
void | move_under (node &new_parent) |
Move this node under another parent. More... | |
size_type | size () const |
Returns the number of children this nodes has. More... | |
bool | empty () const |
Find out if this node has any children. More... | |
iterator | begin () |
Get an iterator that points to the beginning of this node's children. More... | |
const_iterator | begin () const |
Get a const_iterator that points to the beginning of this node's children. More... | |
iterator | end () |
Get an iterator that points one past the last child for this node. More... | |
const_iterator | end () const |
Get a const_iterator that points one past the last child for this node. More... | |
iterator | self () |
Get an iterator that points back at this node. More... | |
const_iterator | self () const |
Get a const_iterator that points back at this node. More... | |
iterator | parent () |
Get an iterator that points at the parent of this node. More... | |
const_iterator | parent () const |
Get a const_iterator that points at the parent of this node. More... | |
iterator | find (const char *name) |
Find the first child node that has the given name. More... | |
const_iterator | find (const char *name) const |
Find the first child node that has the given name. More... | |
iterator | find (const char *name, const iterator &start) |
Find the first child node, starting with the given iterator, that has the given name. More... | |
const_iterator | find (const char *name, const const_iterator &start) const |
Find the first child node, starting with the given const_iterator, that has the given name. More... | |
nodes_view | elements () |
Returns view of child nodes of type type_element. More... | |
const_nodes_view | elements () const |
Returns view of child nodes of type type_element. More... | |
nodes_view | elements (const char *name) |
Returns view of child nodes of type type_element with name name. More... | |
const_nodes_view | elements (const char *name) const |
Returns view of child nodes of type type_element with name name. More... | |
iterator | insert (const node &n) |
Insert a new child node. More... | |
iterator | insert (const iterator &position, const node &n) |
Insert a new child node. More... | |
iterator | replace (const iterator &old_node, const node &new_node) |
Replace the node pointed to by the given iterator with another node. More... | |
iterator | erase (const iterator &to_erase) |
Erase the node that is pointed to by the given iterator. More... | |
iterator | erase (iterator first, const iterator &last) |
Erase all nodes in the given range, from first to last. More... | |
size_type | erase (const char *name) |
Erase all children nodes with the given name. More... | |
void | clear () |
Erases all children nodes. More... | |
void | sort (const char *node_name, const char *attr_name) |
Sort all the children nodes of this node using one of their attributes. More... | |
template<typename T > | |
void | sort (T compare) |
Sort all the children nodes of this node using the given comparison function object. More... | |
std::string | node_to_string () const |
Convert the node and all its children into XML text and return the string containing them. More... | |
void | node_to_string (std::string &xml) const |
Convert the node and all its children into XML text and set the given string to that text. More... | |
Friends | |
std::ostream & | operator<< (std::ostream &stream, const node &n) |
Write a node and all of its children to the given stream. More... | |
The xml::node class is used to hold information about one XML node.
This includes the name of the node, the namespace of the node and attributes for the node. It also has an iterator whereby you can get to the children nodes.
It should be noted that any member function that returns a const char* returns a temporary value. The pointer that is returned will change with ANY operation to the xml::node. If you need the data to stick around a little longer you should put it inside a std::string.
typedef std::size_t xml::node::size_type |
size type
enum xml::node::node_type |
enum for the different types of XML nodes
xml::node::node | ( | ) |
Construct a new blank xml::node.
|
explicit |
Construct a new xml::node and set the name of the node.
name | The name of the new node. |
xml::node::node | ( | const char * | name, |
const char * | content | ||
) |
Construct a new xml::node given a name and content.
The content, if it's not an empty string, will be used to create a new child text node.
name | The name of the new element. |
content | The text that will be used to create a child node. |
|
explicit |
|
explicit |
|
explicit |
|
explicit |
xml::node::node | ( | const node & | other | ) |
xml::node::~node | ( | ) |
Class destructor.
iterator xml::node::begin | ( | ) |
Get an iterator that points to the beginning of this node's children.
const_iterator xml::node::begin | ( | ) | const |
Get a const_iterator that points to the beginning of this node's children.
void xml::node::clear | ( | ) |
Erases all children nodes.
nodes_view xml::node::elements | ( | ) |
Returns view of child nodes of type type_element.
If no such node can be found, returns empty view.
Example:
const_nodes_view xml::node::elements | ( | ) | const |
Returns view of child nodes of type type_element.
If no such node can be found, returns empty view.
Example:
nodes_view xml::node::elements | ( | const char * | name | ) |
Returns view of child nodes of type type_element with name name.
If no such node can be found, returns empty view.
Example:
name | Name of the elements to return. |
const_nodes_view xml::node::elements | ( | const char * | name | ) | const |
Returns view of child nodes of type type_element with name name.
If no such node can be found, returns empty view.
Example:
name | Name of the elements to return. |
bool xml::node::empty | ( | ) | const |
Find out if this node has any children.
This is the same as xml::node::size() == 0 except it is much faster.
|
inline |
Get an iterator that points one past the last child for this node.
|
inline |
Get a const_iterator that points one past the last child for this node.
Erase the node that is pointed to by the given iterator.
The node and all its children will be removed from this node. This will invalidate any iterators that point to the node to be erased, or any pointers or references to that node.
to_erase | An iterator that points to the node to be erased. |
Erase all nodes in the given range, from first to last.
This will invalidate any iterators that point to the nodes to be erased, or any pointers or references to those nodes.
first | The first node in the range to be removed. |
last | An iterator that points one past the last node to erase. Think xml::node::end(). |
size_type xml::node::erase | ( | const char * | name | ) |
Erase all children nodes with the given name.
This will find all nodes that have the given node name and remove them from this node. This will invalidate any iterators that point to the nodes to be erased, or any pointers or references to those nodes.
name | The name of nodes to remove. |
iterator xml::node::find | ( | const char * | name | ) |
Find the first child node that has the given name.
If no such node can be found, this function will return the same iterator that end() would return.
This function is not recursive. That is, it will not search down the tree for the requested node. Instead, it will only search one level deep, only checking the children of this node.
name | The name of the node you want to find. |
const_iterator xml::node::find | ( | const char * | name | ) | const |
Find the first child node that has the given name.
If no such node can be found, this function will return the same const_iterator that end() would return.
This function is not recursive. That is, it will not search down the tree for the requested node. Instead, it will only search one level deep, only checking the children of this node.
name | The name of the node you want to find. |
Find the first child node, starting with the given iterator, that has the given name.
If no such node can be found, this function will return the same iterator that end() would return.
This function should be given an iterator to one of this node's children. The search will begin with that node and continue with all its sibliings. This function will not recurse down the tree, it only searches in one level.
name | The name of the node you want to find. |
start | Where to begin the search. |
const_iterator xml::node::find | ( | const char * | name, |
const const_iterator & | start | ||
) | const |
Find the first child node, starting with the given const_iterator, that has the given name.
If no such node can be found, this function will return the same const_iterator that end() would return.
This function should be given a const_iterator to one of this node's children. The search will begin with that node and continue with all its siblings. This function will not recurse down the tree, it only searches in one level.
name | The name of the node you want to find. |
start | Where to begin the search. |
xml::attributes& xml::node::get_attributes | ( | ) |
Get the list of attributes.
You can use the returned object to get and set the attributes for this node. Make sure you use a reference to this returned object, to prevent a copy.
const xml::attributes& xml::node::get_attributes | ( | ) | const |
Get the list of attributes.
You can use the returned object to get the attributes for this node. Make sure you use a reference to this returned object, to prevent a copy.
const char* xml::node::get_content | ( | ) | const |
Get the content for this text node.
If this node is not a text node but it has children nodes that are text nodes, the contents of those child nodes will be returned. If there is no content or these conditions do not apply, zero will be returned.
This function may change in the future to return std::string. Feedback is welcome.
const char* xml::node::get_name | ( | ) | const |
Get the name of this xml::node.
This function may change in the future to return std::string. Feedback is welcome.
const char* xml::node::get_namespace | ( | ) | const |
Get the namespace of this xml::node.
node_type xml::node::get_type | ( | ) | const |
Get this node's "type".
You can use that information to know what you can and cannot do with it.
Insert a new child node.
The new node will be inserted at the end of the child list. This is similar to the xml::node::push_back member function except that an iterator to the inserted node is returned.
n | The node to insert as a child of this node. |
Insert a new child node.
The new node will be inserted before the node pointed to by the given iterator.
position | An iterator that points to the location where the new node should be inserted (before it). |
n | The node to insert as a child of this node. |
bool xml::node::is_text | ( | ) | const |
Find out if this node is a text node or something like a text node, CDATA for example.
void xml::node::move_under | ( | node & | new_parent | ) |
Move this node under another parent.
This node will become the last child of new_parent. Notice that this node must not be an ancestor of new_parent, in particular it shouldn't be the document root.
Currently this method can only be used to move nodes inside the same document.
All iterators pointing to this node are invalidated after the move.
new_parent | The new parent for the node. |
std::string xml::node::node_to_string | ( | ) | const |
Convert the node and all its children into XML text and return the string containing them.
|
inline |
Convert the node and all its children into XML text and set the given string to that text.
xml | The string to set the node's XML data to. |
Make this node equal to some other node via assignment.
other | The other node to copy. |
iterator xml::node::parent | ( | ) |
Get an iterator that points at the parent of this node.
If this node does not have a parent, this member function will return an "end" iterator.
const_iterator xml::node::parent | ( | ) | const |
Get a const_iterator that points at the parent of this node.
If this node does not have a parent, this member function will return an "end" const_iterator.
void xml::node::push_back | ( | const node & | child | ) |
Replace the node pointed to by the given iterator with another node.
The old node will be removed, including all its children, and replaced with the new node. This will invalidate any iterators that point to the node to be replaced, or any pointers or references to that node.
old_node | An iterator that points to the node that should be removed. |
new_node | The node to put in old_node's place. |
iterator xml::node::self | ( | ) |
Get an iterator that points back at this node.
const_iterator xml::node::self | ( | ) | const |
Get a const_iterator that points back at this node.
void xml::node::set_content | ( | const char * | content | ) |
Set the content of a node.
If this node is an element node, this function will remove all of its children nodes and replace them with one text node set to the given string.
content | The content of the text node. |
void xml::node::set_name | ( | const char * | name | ) |
void xml::node::set_namespace | ( | const std::string & | href | ) |
Set the default namespace of this xml::node.
If the default namespace is already set on this node, it is changed.
Example:
void xml::node::set_text_content | ( | const char * | content | ) |
Set the content of a node to given text.
In contrast to set_content(), content is raw text, so unescaped XML special chars are allowed and entity references are not supported.
If this node is an element node, this function will remove all of its children nodes and replace them with one text node set to the given string.
content | The content text. |
size_type xml::node::size | ( | ) | const |
Returns the number of children this nodes has.
If you just want to know how if this node has children or not, you should use xml::node::empty() instead.
void xml::node::sort | ( | const char * | node_name, |
const char * | attr_name | ||
) |
Sort all the children nodes of this node using one of their attributes.
Only nodes that are of xml::node::type_element will be sorted, and they must have the given node_name.
The sorting is done by calling std::strcmp on the value of the given attribute.
node_name | The name of the nodes to sort. |
attr_name | The attribute to sort on. |
|
inline |
Sort all the children nodes of this node using the given comparison function object.
All element type nodes will be considered for sorting.
compare | The binary function object to call in order to sort all child nodes. |
void xml::node::swap | ( | node & | other | ) |
Swap this node with another one.
other | The other node to swap with. |
|
friend |
Write a node and all of its children to the given stream.
stream | The stream to write the node as XML. |
n | The node to write to the stream. |