xmlwrapp
Lightweight C++ XML parsing library
Public Types | Public Member Functions | Friends | List of all members
xml::document Class Reference

The xml::document class is used to hold the XML tree and various bits of information about it. More...

#include <document.h>

Public Types

typedef std::size_t size_type
 size type More...
 

Public Member Functions

 document ()
 Create a new XML document with the default settings. More...
 
 document (const char *root_name)
 Create a new XML document and set the name of the root element to the given text. More...
 
 document (const node &n)
 Create a new XML document and set the root node. More...
 
 document (const char *filename, error_handler &on_error)
 Load XML document from given file. More...
 
 document (const char *data, size_type size, error_handler &on_error=throw_on_error)
 Load XML document from given data. More...
 
 document (const document &other)
 Copy construct a new XML document. More...
 
documentoperator= (const document &other)
 Copy another document object into this one using the assignment operator. More...
 
void swap (document &other)
 Swap one xml::document object for another. More...
 
 ~document ()
 Clean up after an XML document object. More...
 
const nodeget_root_node () const
 Get a reference to the root node of this document. More...
 
nodeget_root_node ()
 Get a reference to the root node of this document. More...
 
void set_root_node (const node &n)
 Set the root node to the given node. More...
 
const std::string & get_version () const
 Get the XML version for this document. More...
 
void set_version (const char *version)
 Set the XML version number for this document. More...
 
std::string get_encoding () const
 Get the XML encoding for this document. More...
 
void set_encoding (const char *encoding)
 Set the XML encoding string. More...
 
bool get_is_standalone () const
 Find out if the current document is a standalone document. More...
 
void set_is_standalone (bool sa)
 Set the standalone flag. More...
 
bool process_xinclude ()
 Walk through the document and expand <xi:include> elements. More...
 
bool has_internal_subset () const
 Test to see if this document has an internal subset. More...
 
bool has_external_subset () const
 Test to see if this document has an external subset. More...
 
bool validate ()
 Validate this document against the DTD that has been attached to it. More...
 
bool validate (const char *dtdname)
 Parse the given DTD and try to validate this document against it. More...
 
size_type size () const
 Returns the number of child nodes of this document. More...
 
node::iterator begin ()
 Get an iterator to the first child node of this document. More...
 
node::const_iterator begin () const
 Get a const_iterator to the first child node of this document. More...
 
node::iterator end ()
 Get an iterator that points one past the last child node for this document. More...
 
node::const_iterator end () const
 Get a const_iterator that points one past the last child node for this document. More...
 
void push_back (const node &child)
 Add a child xml::node to this document. More...
 
node::iterator insert (const node &n)
 Insert a new child node. More...
 
node::iterator insert (node::iterator position, const node &n)
 Insert a new child node. More...
 
node::iterator replace (node::iterator old_node, const node &new_node)
 Replace the node pointed to by the given iterator with another node. More...
 
node::iterator erase (node::iterator to_erase)
 Erase the node that is pointed to by the given iterator. More...
 
node::iterator erase (node::iterator first, node::iterator last)
 Erase all nodes in the given range, from first to last. More...
 
void save_to_string (std::string &s, error_handler &on_error=throw_on_error) const
 Convert the XML document tree into XML text data and place it into the given string. More...
 
bool save_to_file (const char *filename, int compression_level=0, error_handler &on_error=throw_on_error) const
 Convert the XML document tree into XML text data and place it into the given filename. More...
 

Friends

std::ostream & operator<< (std::ostream &stream, const document &doc)
 Convert the XML document tree into XML text data and then insert it into the given stream. More...
 

Detailed Description

The xml::document class is used to hold the XML tree and various bits of information about it.

Member Typedef Documentation

◆ size_type

typedef std::size_t xml::document::size_type

size type

Constructor & Destructor Documentation

◆ document() [1/6]

xml::document::document ( )

Create a new XML document with the default settings.

The new document will contain a root node with a name of "blank".

◆ document() [2/6]

xml::document::document ( const char *  root_name)
explicit

Create a new XML document and set the name of the root element to the given text.

Parameters
root_nameWhat to set the name of the root element to.
Deprecated:
Use xml::document(xml::node(root_name)) constructor instead.

◆ document() [3/6]

xml::document::document ( const node n)
explicit

Create a new XML document and set the root node.

Parameters
nThe node to use as the root node. n will be copied.

◆ document() [4/6]

xml::document::document ( const char *  filename,
error_handler on_error 
)
explicit

Load XML document from given file.

Errors are handled by on_error handler; if you pass xml::throw_on_error, xml::exception is thrown on errors. If there's a fatal error that prevents the document from being loaded and the error handler doesn't throw an exception, the constructor will throw xml::exception anyway.

Parameters
filenameThe name of the file to parse.
on_errorHandler called to process errors and warnings.
Since
0.7.0

◆ document() [5/6]

xml::document::document ( const char *  data,
size_type  size,
error_handler on_error = throw_on_error 
)
explicit

Load XML document from given data.

Errors are handled by on_error handler; by default, xml::exception is thrown on errors. If there's a fatal error that prevents the document from being loaded and the error handler doesn't throw an exception, the constructor will throw xml::exception anyway.

Parameters
dataThe XML data to parse.
sizeThe size of the XML data to parse.
on_errorHandler called to process errors and warnings.
Since
0.7.0

◆ document() [6/6]

xml::document::document ( const document other)

Copy construct a new XML document.

The new document will be an exact copy of the original.

Parameters
otherThe other document object to copy from.

◆ ~document()

xml::document::~document ( )

Clean up after an XML document object.

Member Function Documentation

◆ begin() [1/2]

node::iterator xml::document::begin ( )

Get an iterator to the first child node of this document.

If what you really wanted was the root node (the first element) you should use the get_root_node() member function instead.

Returns
A xml::node::iterator that points to the first child node.
An end iterator if there are no children in this document

◆ begin() [2/2]

node::const_iterator xml::document::begin ( ) const

Get a const_iterator to the first child node of this document.

If what you really wanted was the root node (the first element) you should use the get_root_node() member function instead.

Returns
A xml::node::const_iterator that points to the first child node.
An end const_iterator if there are no children in this document.

◆ end() [1/2]

node::iterator xml::document::end ( )

Get an iterator that points one past the last child node for this document.

Returns
An end xml::node::iterator.

◆ end() [2/2]

node::const_iterator xml::document::end ( ) const

Get a const_iterator that points one past the last child node for this document.

Returns
An end xml::node::const_iterator.

◆ erase() [1/2]

node::iterator xml::document::erase ( node::iterator  to_erase)

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.

Do not remove the root node using this member function. The same rules that apply to push_back apply here. If you try to erase the root node, an exception will be thrown.

Parameters
to_eraseAn iterator that points to the node to be erased.
Returns
An iterator that points to the node after the one being erased.
See also
xml::document::push_back

◆ erase() [2/2]

node::iterator xml::document::erase ( node::iterator  first,
node::iterator  last 
)

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.

Do not remove the root node using this member function. The same rules that apply to push_back apply here. If you try to erase the root node, an exception will be thrown.

Parameters
firstThe first node in the range to be removed.
lastAn iterator that points one past the last node to erase. Think xml::node::end().
Returns
An iterator that points to the node after the last one being erased.
See also
xml::document::push_back

◆ get_encoding()

std::string xml::document::get_encoding ( ) const

Get the XML encoding for this document.

The default encoding is UTF-8.

Returns
The encoding string.

◆ get_is_standalone()

bool xml::document::get_is_standalone ( ) const

Find out if the current document is a standalone document.

For generated documents, this will be the default. For parsed documents this will be set based on the XML processing instruction.

Returns
True if this document is standalone.
False if this document is not standalone.

◆ get_root_node() [1/2]

const node& xml::document::get_root_node ( ) const

Get a reference to the root node of this document.

If no root node has been set, the returned node will be a blank node. You should take caution to use a reference so that you don't copy the whole node tree!

Returns
A const reference to the root node.

◆ get_root_node() [2/2]

node& xml::document::get_root_node ( )

Get a reference to the root node of this document.

If no root node has been set, the returned node will be a blank node. You should take caution to use a reference so that you don't copy the whole node tree!

Returns
A reference to the root node.

◆ get_version()

const std::string& xml::document::get_version ( ) const

Get the XML version for this document.

For generated documents, the version will be the default. For parsed documents, this will be the version from the XML processing instruction.

Returns
The XML version string for this document.

◆ has_external_subset()

bool xml::document::has_external_subset ( ) const

Test to see if this document has an external subset.

That is, it references a DTD from an external source, such as a file or URL.

Returns
True if this document has an external subset.
False otherwise.

◆ has_internal_subset()

bool xml::document::has_internal_subset ( ) const

Test to see if this document has an internal subset.

That is, DTD data that is declared within the XML document itself.

Returns
True if this document has an internal subset.
False otherwise.

◆ insert() [1/2]

node::iterator xml::document::insert ( const node n)

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.

The rules from the push_back member function apply here. Don't add a node of type element.

Parameters
nThe node to insert as a child of this document.
Returns
An iterator that points to the newly inserted node.
See also
xml::document::push_back

◆ insert() [2/2]

node::iterator xml::document::insert ( node::iterator  position,
const node n 
)

Insert a new child node.

The new node will be inserted before the node pointed to by the given iterator.

The rules from the push_back member function apply here. Don't add a node of type element.

Parameters
positionAn iterator that points to the location where the new node should be inserted (before it).
nThe node to insert as a child of this document.
Returns
An iterator that points to the newly inserted node.
See also
xml::document::push_back

◆ operator=()

document& xml::document::operator= ( const document other)

Copy another document object into this one using the assignment operator.

This document object will be an exact copy of the other document after the assignment.

Parameters
otherThe document to copy from.
Returns
*this.

◆ process_xinclude()

bool xml::document::process_xinclude ( )

Walk through the document and expand <xi:include> elements.

For more information, please see the w3c recommendation for XInclude. http://www.w3.org/2001/XInclude.

The return value of this function may change to int after a bug has been fixed in libxml2 (xmlXIncludeDoProcess).

Returns
False if there was an error with substitutions.
True if there were no errors (with or without substitutions).

◆ push_back()

void xml::document::push_back ( const node child)

Add a child xml::node to this document.

You should not add a element type node, since there can only be one root node. This member function is only useful for adding processing instructions, comments, etc.. If you do try to add a node of type element, an exception will be thrown.

Parameters
childThe child xml::node to add.

◆ replace()

node::iterator xml::document::replace ( node::iterator  old_node,
const node new_node 
)

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.

Do not replace this root node with this member function. The same rules that apply to push_back apply here. If you try to replace a node of type element, an exception will be thrown.

Parameters
old_nodeAn iterator that points to the node that should be removed.
new_nodeThe node to put in old_node's place.
Returns
An iterator that points to the new node.
See also
xml::document::push_back

◆ save_to_file()

bool xml::document::save_to_file ( const char *  filename,
int  compression_level = 0,
error_handler on_error = throw_on_error 
) const

Convert the XML document tree into XML text data and place it into the given filename.

This function throws an exception if saving fails for any reason by default and allows to customize this behaviour by passing a non-default on_error handler.

Parameters
filenameThe name of the file to place the XML text data into.
on_errorHandler called to process errors and warnings (new since 0.8.0).
compression_level0 is no compression, 1-9 allowed, where 1 is for better speed, and 9 is for smaller size
Returns
True if the data was saved successfully.
False otherwise (notice that this is only possible if a custom error handler not throwing on error is specified).

◆ save_to_string()

void xml::document::save_to_string ( std::string &  s,
error_handler on_error = throw_on_error 
) const

Convert the XML document tree into XML text data and place it into the given string.

Any errors occurring while converting the document to string are passed to on_error handler. By default, an exception will be thrown if anything goes wrong.

Parameters
sThe string to place the XML text data.
on_errorHandler called to process errors and warnings (new since 0.8.0).

◆ set_encoding()

void xml::document::set_encoding ( const char *  encoding)

Set the XML encoding string.

If you don't set this, it will default to UTF-8.

Note that all strings in the XML document must be encoded in the document encoding.

Parameters
encodingThe XML encoding to use.

◆ set_is_standalone()

void xml::document::set_is_standalone ( bool  sa)

Set the standalone flag.

This will show up in the XML output in the correct processing instruction.

Parameters
saWhat to set the standalone flag to.

◆ set_root_node()

void xml::document::set_root_node ( const node n)

Set the root node to the given node.

A full copy is made and stored in the document object.

Parameters
nThe new root node to use.

◆ set_version()

void xml::document::set_version ( const char *  version)

Set the XML version number for this document.

This version string will be used when generating the XML output.

Parameters
versionThe version string to use, like "1.0".

◆ size()

size_type xml::document::size ( ) const

Returns the number of child nodes of this document.

This will always be at least one, since all xmlwrapp documents must have a root node. This member function is useful to find out how many document children there are, including processing instructions, comments, etc.

Returns
The number of children nodes that this document has.

◆ swap()

void xml::document::swap ( document other)

Swap one xml::document object for another.

Parameters
otherThe other document to swap

◆ validate() [1/2]

bool xml::document::validate ( )

Validate this document against the DTD that has been attached to it.

This would happen at parse time if there was a !DOCTYPE definition. If the DTD is valid, and the document is valid, this member function will return true.

If it returns false, you may want to send the document through xmllint to get the actual error messages.

Returns
True if the document is valid.
False if there was a problem with the DTD or XML doc.

◆ validate() [2/2]

bool xml::document::validate ( const char *  dtdname)

Parse the given DTD and try to validate this document against it.

If the DTD is valid, and the document is valid, this member function will return true.

If it returns false, you may want to send the document through xmllint to get the actual error messages.

This member function will add the parsed DTD to this document as the external subset after the validation. If there is already an external DTD attached to this document it will be removed and deleted.

Parameters
dtdnameA filename or URL for the DTD to use.
Returns
True if the document is valid.
False if there was a problem with the DTD or XML doc.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  stream,
const document doc 
)
friend

Convert the XML document tree into XML text data and then insert it into the given stream.

Parameters
streamThe stream to insert the XML into.
docThe document to insert.
Returns
The stream from the first parameter.

The documentation for this class was generated from the following file: