| Version: 3.2.8

Public Member Functions | Static Public Member Functions | List of all members
wxXmlDocument Class Reference
Class List by Category » XML

`#include <wx/xml/xml.h>`

Inheritance diagram for wxXmlDocument:
[legend]

## Detailed Description

This class holds XML data/document as parsed by XML parser in the root node.

wxXmlDocument internally uses the expat library which comes with wxWidgets to parse the given stream.

A wxXmlDocument is in fact a list of wxXmlNode organised into a structure that reflects the XML tree being represented by the document.

Note
  Ownership is passed to the XML tree as each wxXmlNode is added to it, and this has two implications. Firstly, the wxXmlDocument takes responsibility for deleting the node so the user should not `delete` it; and secondly, a wxXmlNode must always be created on the heap and never on the stack.

A simple example of using XML classes is:

wxXmlDocument doc;
if (!doc.Load("myfile.xml"))
return false;
// Start processing the XML file.
if (doc.GetRoot()->GetName() != "myroot-node")
return false;
// Examine prologue.
wxXmlNode *prolog = doc.GetDocumentNode()->GetChildren();
while (prolog)
{
if (prolog->GetType() == wxXML_PI_NODE && prolog->GetName() == "target")
{
// Process Process Instruction (PI) contents.
wxString pi = prolog->GetContent();
...
}
}
wxXmlNode *child = doc.GetRoot()->GetChildren();
while (child)
{
if (child->GetName() == "tag1")
{
// Process text enclosed by tag1/tag1.
wxString content = child->GetNodeContent();
...
// Process attributes of tag1.
wxString attrvalue1 =
child->GetAttribute("attr1", "default-value");
wxString attrvalue2 =
child->GetAttribute("attr2", "default-value");
...
}
else if (child->GetName() == "tag2")
{
// Process tag2 ...
}
child = child->GetNext();
}
wxString
String class for passing textual data to or receiving it from wxWidgets.
Definition: string.h:315
wxXmlDocument
This class holds XML data/document as parsed by XML parser in the root node.
Definition: xml.h:702
wxXmlDocument::GetRoot
wxXmlNode * GetRoot() const
Returns the root element node of the document.
wxXmlDocument::GetDocumentNode
wxXmlNode * GetDocumentNode() const
Returns the document node of the document.
wxXmlDocument::Load
virtual bool Load(const wxString &filename, const wxString &encoding="UTF-8", int flags=wxXMLDOC_NONE)
Parses filename as an xml document and loads its data.
wxXmlNode
Represents a node in an XML document.
Definition: xml.h:74
wxXmlNode::GetAttribute
bool GetAttribute(const wxString &attrName, wxString *value) const
Returns true if a attribute named attrName could be found.
wxXmlNode::GetContent
const wxString & GetContent() const
Returns the content of this node.
wxXmlNode::GetNext
wxXmlNode * GetNext() const
Returns a pointer to the sibling of this node or NULL if there are no siblings.
wxXmlNode::GetName
const wxString & GetName() const
Returns the name of this node.
wxXmlNode::GetType
wxXmlNodeType GetType() const
Returns the type of this node.
wxXmlNode::GetChildren
wxXmlNode * GetChildren() const
Returns the first child of this node.
wxXmlNode::GetNodeContent
wxString GetNodeContent() const
Returns the content of the first child node of type wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE.
wxXML_PI_NODE
@ wxXML_PI_NODE
Definition: xml.h:19

Note that if you want to preserve the original formatting of the loaded file including whitespaces and indentation, you need to turn off whitespace-only textnode removal and automatic indentation. For example:

wxXmlDocument doc;
doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
// myfile2.xml will be identical to myfile.xml saving it this way:
doc.Save("myfile2.xml", wxXML_NO_INDENTATION);
wxXmlDocument::Save
virtual bool Save(const wxString &filename, int indentstep=2) const
Saves XML tree creating a file named with given string.
wxXMLDOC_KEEP_WHITESPACE_NODES
@ wxXMLDOC_KEEP_WHITESPACE_NODES
Definition: xml.h:559
wxXML_NO_INDENTATION
#define wxXML_NO_INDENTATION
Definition: xml.h:553

Using default parameters, you will get a reformatted document which in general is different from the original loaded content:

wxXmlDocument doc;
doc.Load("myfile.xml");
doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml

wxXmlDocument can also be used to create documents. The following code gives an example of creating a simple document with two nested element nodes, the second of which has an attribute, and a text node. It also demonstrates how to write the resulting output to a wxString:

// Create a document and add the root node.
wxXmlDocument xmlDoc;
wxXmlNode* root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "Root");
xmlDoc.SetRoot(root);
// Add some XML.
wxXmlNode* library = new wxXmlNode (root, wxXML_ELEMENT_NODE, "Library");
library->AddAttribute("type", "CrossPlatformList");
wxXmlNode* name = new wxXmlNode(library, wxXML_ELEMENT_NODE, "Name");
name->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "", "wxWidgets"));
// Write the output to a wxString.
wxStringOutputStream stream;
xmlDoc.Save(stream);
wxStringOutputStream
This class implements an output stream which writes data either to a user-provided or internally allo...
Definition: sstream.h:45
wxXmlDocument::SetRoot
void SetRoot(wxXmlNode *node)
Sets the root element node of this document.
wxXmlNode::AddChild
virtual void AddChild(wxXmlNode *child)
Adds node child as the last child of this node.
wxXmlNode::AddAttribute
virtual void AddAttribute(const wxString &name, const wxString &value)
Appends an attribute with given name and value to the list of attributes for this node.
wxXML_TEXT_NODE
@ wxXML_TEXT_NODE
Definition: xml.h:15
wxXML_ELEMENT_NODE
@ wxXML_ELEMENT_NODE
Definition: xml.h:13

This will produce a document that looks something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Library type="CrossPlatformList">
<Name>wxWidgets</Name>
</Library>
</Root>

If the root name value of the DOCTYPE is set, either by loading a file with a DOCTYPE declaration or by setting it directly with the SetDoctype member, then a DOCTYPE declaration will be added immediately after the XML declaration.

##

Library: wxXML
Category: XML

See also
  wxXmlNode, wxXmlAttribute, wxXmlDoctype

## Public Member Functions
| wxXmlDocument ()
| Default constructor. More...
| wxXmlDocument (const wxXmlDocument &doc)
| Copy constructor. More...
| wxXmlDocument (const wxString &filename, const wxString &encoding="UTF-8")
| Loads the given filename using the given encoding. More...
| wxXmlDocument (wxInputStream &stream, const wxString &encoding="UTF-8")
| Loads the XML document from given stream using the given encoding. More...
virtual | ~wxXmlDocument ()
| Virtual destructor. More...
void | AppendToProlog (wxXmlNode *node)
| Appends a Process Instruction or Comment node to the document prologue. More...
wxXmlNode * | DetachDocumentNode ()
| Detaches the document node and returns it. More...
wxXmlNode * | DetachRoot ()
| Detaches the root entity node and returns it. More...
wxString | GetEncoding () const
| Returns encoding of in-memory representation of the document (same as passed to Load() or constructor, defaults to UTF-8). More...
const wxString & | GetFileEncoding () const
| Returns encoding of document (may be empty). More...
const wxXmlDoctype & | GetDoctype () const
| Returns the DOCTYPE declaration data for the document. More...
wxTextFileType | GetFileType () const
| Returns the output line ending format used for documents. More...
wxString | GetEOL () const
| Returns the output line ending string used for documents. More...
wxXmlNode * | GetDocumentNode () const
| Returns the document node of the document. More...
wxXmlNode * | GetRoot () const
| Returns the root element node of the document. More...
const wxString & | GetVersion () const
| Returns the version of document. More...
bool | IsOk () const
| Returns true if the document has been loaded successfully. More...
virtual bool | Load (const wxString &filename, const wxString &encoding="UTF-8", int flags=wxXMLDOC_NONE)
| Parses filename as an xml document and loads its data. More...
virtual bool | Load (wxInputStream &stream, const wxString &encoding="UTF-8", int flags=wxXMLDOC_NONE)
| Like Load(const wxString&, const wxString&, int) but takes the data from given input stream. More...
virtual bool | Save (const wxString &filename, int indentstep=2) const
| Saves XML tree creating a file named with given string. More...
virtual bool | Save (wxOutputStream &stream, int indentstep=2) const
| Saves XML tree in the given output stream. More...
void | SetDocumentNode (wxXmlNode *node)
| Sets the document node of this document. More...
void | SetEncoding (const wxString &enc)
| Sets the encoding of the document. More...
void | SetFileEncoding (const wxString &encoding)
| Sets the encoding of the file which will be used to save the document. More...
void | SetDoctype (const wxXmlDoctype &doctype)
| Sets the data which will appear in the DOCTYPE declaration when the document is saved. More...
void | SetFileType (wxTextFileType fileType)
| Sets the output line ending formats when the document is saved. More...
void | SetRoot (wxXmlNode *node)
| Sets the root element node of this document. More...
void | SetVersion (const wxString &version)
| Sets the version of the XML file which will be used to save the document. More...
wxXmlDocument & | operator= (const wxXmlDocument &doc)
| Deep copies the given document. More...
Public Member Functions inherited from wxObject
| wxObject ()
| Default ctor; initializes to NULL the internal reference data. More...
| wxObject (const wxObject &other)
| Copy ctor. More...
virtual | ~wxObject ()
| Destructor. More...
virtual wxClassInfo * | GetClassInfo () const
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar). More...
wxObjectRefData * | GetRefData () const
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. More...
bool | IsKindOf (const wxClassInfo *info) const
| Determines whether this class is a subclass of (or the same class as) the given class. More...
bool | IsSameAs (const wxObject &obj) const
| Returns true if this object has the same data pointer as obj. More...
void | Ref (const wxObject &clone)
| Makes this object refer to the data in clone. More...
void | SetRefData (wxObjectRefData *data)
| Sets the wxObject::m_refData pointer. More...
void | UnRef ()
| Decrements the reference count in the associated data, and if it is zero, deletes the data. More...
void | UnShare ()
| This is the same of AllocExclusive() but this method is public. More...
void | operator delete (void *buf)
| The delete operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...
void * | operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
| The new operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...

## Static Public Member Functions
static wxVersionInfo | GetLibraryVersionInfo ()
| Get expat library version information. More...

## Additional Inherited Members
Protected Member Functions inherited from wxObject
void | AllocExclusive ()
| Ensure that this object's data is not shared with any other object. More...
virtual wxObjectRefData * | CreateRefData () const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it. More...
virtual wxObjectRefData * | CloneRefData (const wxObjectRefData *data) const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data. More...
Protected Attributes inherited from wxObject
wxObjectRefData * | m_refData
| Pointer to an object which is the object's reference-counted data. More...

## Constructor & Destructor Documentation

## ◆ wxXmlDocument() [1/4]

wxXmlDocument::wxXmlDocument | ( |  | ) |

Default constructor.

## ◆ wxXmlDocument() [2/4]

wxXmlDocument::wxXmlDocument | ( | const wxXmlDocument & | doc | ) |

Copy constructor.

Deep copies all the XML tree of the given document.

## ◆ wxXmlDocument() [3/4]

wxXmlDocument::wxXmlDocument | ( | const wxString & | filename,
|  | const wxString & | encoding = `"UTF-8"`
| ) |  |

Loads the given filename using the given encoding.

See Load().

## ◆ wxXmlDocument() [4/4]

wxXmlDocument::wxXmlDocument | ( | wxInputStream & | stream,
|  | const wxString & | encoding = `"UTF-8"`
| ) |  |

Loads the XML document from given stream using the given encoding.

See Load().

## ◆ ~wxXmlDocument()

virtual wxXmlDocument::~wxXmlDocument | ( |  | ) |
| virtual

Virtual destructor.

Frees the document root node.

## Member Function Documentation

## ◆ AppendToProlog()

void wxXmlDocument::AppendToProlog | ( | wxXmlNode * | node | ) |

Appends a Process Instruction or Comment node to the document prologue.

Calling this function will create a prologue or attach the node to the end of an existing prologue.

Since
  2.9.2

## ◆ DetachDocumentNode()

wxXmlNode* wxXmlDocument::DetachDocumentNode | ( |  | ) |

Detaches the document node and returns it.

The document node will be set to NULL and thus IsOk() will return false after calling this function.

Note that the caller is responsible for deleting the returned node in order to avoid memory leaks.

Since
  2.9.2

## ◆ DetachRoot()

wxXmlNode* wxXmlDocument::DetachRoot | ( |  | ) |

Detaches the root entity node and returns it.

After calling this function, the document node will remain together with any prologue nodes, but IsOk() will return false since the root entity will be missing.

Note that the caller is responsible for deleting the returned node in order to avoid memory leaks.

## ◆ GetDoctype()

const wxXmlDoctype& wxXmlDocument::GetDoctype | ( |  | ) | const

Returns the DOCTYPE declaration data for the document.

Since
  3.1.0

## ◆ GetDocumentNode()

wxXmlNode* wxXmlDocument::GetDocumentNode | ( |  | ) | const

Returns the document node of the document.

Since
  2.9.2

## ◆ GetEncoding()

wxString wxXmlDocument::GetEncoding | ( |  | ) | const

Returns encoding of in-memory representation of the document (same as passed to Load() or constructor, defaults to UTF-8).

Note
  this is meaningless in Unicode build where data are stored as `wchar_t*`.

## ◆ GetEOL()

wxString wxXmlDocument::GetEOL | ( |  | ) | const

Returns the output line ending string used for documents.

This string is determined by the last call to SetFileType().

Since
  3.1.1

## ◆ GetFileEncoding()

const wxString& wxXmlDocument::GetFileEncoding | ( |  | ) | const

Returns encoding of document (may be empty).

Note
  This is the encoding original file was saved in, not the encoding of in-memory representation!

## ◆ GetFileType()

wxTextFileType wxXmlDocument::GetFileType | ( |  | ) | const

Returns the output line ending format used for documents.

Since
  3.1.1

## ◆ GetLibraryVersionInfo()

static wxVersionInfo wxXmlDocument::GetLibraryVersionInfo | ( |  | ) |
| static

Get expat library version information.

Since
  2.9.2

See also
  wxVersionInfo

## ◆ GetRoot()

wxXmlNode* wxXmlDocument::GetRoot | ( |  | ) | const

Returns the root element node of the document.

## ◆ GetVersion()

const wxString& wxXmlDocument::GetVersion | ( |  | ) | const

Returns the version of document.

This is the value in the `<`?xml version="1.0"?> header of the XML document. If the version attribute was not explicitly given in the header, this function returns an empty string.

## ◆ IsOk()

bool wxXmlDocument::IsOk | ( |  | ) | const

Returns true if the document has been loaded successfully.

## ◆ Load() [1/2]

virtual bool wxXmlDocument::Load | ( | const wxString & | filename,
|  | const wxString & | encoding = `"UTF-8"`,
|  | int | flags = `wxXMLDOC_NONE`
| ) |  |
| virtual

Parses filename as an xml document and loads its data.

If flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading, all nodes of type `wxXML_TEXT_NODE` (see wxXmlNode) are automatically skipped if they contain whitespaces only.

The removal of these nodes makes the load process slightly faster and requires less memory however makes impossible to recreate exactly the loaded text with a Save() call later. Read the initial description of this class for more info.

Returns true on success, false otherwise.

## ◆ Load() [2/2]

virtual bool wxXmlDocument::Load | ( | wxInputStream & | stream,
|  | const wxString & | encoding = `"UTF-8"`,
|  | int | flags = `wxXMLDOC_NONE`
| ) |  |
| virtual

Like Load(const wxString&, const wxString&, int) but takes the data from given input stream.

## ◆ operator=()

wxXmlDocument& wxXmlDocument::operator= | ( | const wxXmlDocument & | doc | ) |

Deep copies the given document.

## ◆ Save() [1/2]

virtual bool wxXmlDocument::Save | ( | const wxString & | filename,
|  | int | indentstep = `2`
| ) |  | const
| virtual

Saves XML tree creating a file named with given string.

If indentstep is greater than or equal to zero, then, while saving, an automatic indentation is added with steps composed by indentstep spaces.

If indentstep is `wxXML_NO_INDENTATION`, then, automatic indentation is turned off.

## ◆ Save() [2/2]

virtual bool wxXmlDocument::Save | ( | wxOutputStream & | stream,
|  | int | indentstep = `2`
| ) |  | const
| virtual

Saves XML tree in the given output stream.

See Save(const wxString&, int) for a description of indentstep.

## ◆ SetDoctype()

void wxXmlDocument::SetDoctype | ( | const wxXmlDoctype & | doctype | ) |

Sets the data which will appear in the DOCTYPE declaration when the document is saved.

Since
  3.1.0

## ◆ SetDocumentNode()

void wxXmlDocument::SetDocumentNode | ( | wxXmlNode * | node | ) |

Sets the document node of this document.

Deletes any previous document node. Use DetachDocumentNode() and then SetDocumentNode() if you want to replace the document node without deleting the old document tree.

Since
  2.9.2

## ◆ SetEncoding()

void wxXmlDocument::SetEncoding | ( | const wxString & | enc | ) |

Sets the encoding of the document.

## ◆ SetFileEncoding()

void wxXmlDocument::SetFileEncoding | ( | const wxString & | encoding | ) |

Sets the encoding of the file which will be used to save the document.

## ◆ SetFileType()

void wxXmlDocument::SetFileType | ( | wxTextFileType | fileType | ) |

Sets the output line ending formats when the document is saved.

By default Unix file type is used, i.e. a single ASCII LF (10) character is used at the end of lines.

Since
  3.1.1

## ◆ SetRoot()

void wxXmlDocument::SetRoot | ( | wxXmlNode * | node | ) |

Sets the root element node of this document.

Will create the document node if necessary. Any previous root element node is deleted.

## ◆ SetVersion()

void wxXmlDocument::SetVersion | ( | const wxString & | version | ) |

Sets the version of the XML file which will be used to save the document.