///////////////////////////////////////////////////////////////////////////// // Name: xml/xml.h // Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument // Author: wxWidgets team // RCS-ID: $Id$ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// /// Represents XML node type. enum wxXmlNodeType { // note: values are synchronized with xmlElementType from libxml wxXML_ELEMENT_NODE = 1, wxXML_ATTRIBUTE_NODE = 2, wxXML_TEXT_NODE = 3, wxXML_CDATA_SECTION_NODE = 4, wxXML_ENTITY_REF_NODE = 5, wxXML_ENTITY_NODE = 6, wxXML_PI_NODE = 7, wxXML_COMMENT_NODE = 8, wxXML_DOCUMENT_NODE = 9, wxXML_DOCUMENT_TYPE_NODE = 10, wxXML_DOCUMENT_FRAG_NODE = 11, wxXML_NOTATION_NODE = 12, wxXML_HTML_DOCUMENT_NODE = 13 }; /** @class wxXmlNode Represents a node in an XML document. See wxXmlDocument. Node has a name and may have content and attributes. Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant) and @c wxXML_ELEMENT_NODE (e.g. in @c \
", @c "src" is attribute with value
@c "hello.gif" and @c "id" is a attribute with value @c "3".
@library{wxxml}
@category{xml}
@see wxXmlDocument, wxXmlNode
*/
class wxXmlAttribute
{
public:
/**
Default constructor.
*/
wxXmlAttribute();
/**
Creates the attribute with given @a name and @a value.
If @a next is not @NULL, then sets it as sibling of this attribute.
*/
wxXmlAttribute(const wxString& name, const wxString& value,
wxXmlAttribute* next = NULL);
/**
The virtual destructor.
*/
virtual ~wxXmlAttribute();
/**
Returns the name of this attribute.
*/
wxString GetName() const;
/**
Returns the sibling of this attribute or @NULL if there are no siblings.
*/
wxXmlAttribute* GetNext() const;
/**
Returns the value of this attribute.
*/
wxString GetValue() const;
/**
Sets the name of this attribute.
*/
void SetName(const wxString& name);
/**
Sets the sibling of this attribute.
*/
void SetNext(wxXmlAttribute* next);
/**
Sets the value of this attribute.
*/
void SetValue(const wxString& value);
};
/**
@class wxXmlDocument
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 simple example of using XML classes is:
@code
wxXmlDocument doc;
if (!doc.Load(wxT("myfile.xml")))
return @false;
// start processing the XML file
if (doc.GetRoot()-GetName() != wxT("myroot-node"))
return @false;
wxXmlNode *child = doc.GetRoot()-GetChildren();
while (child) {
if (child-GetName() == wxT("tag1")) {
// process text enclosed by tag1/tag1
wxString content = child-GetNodeContent();
...
// process attributes of tag1
wxString attrvalue1 =
child-GetAttribute(wxT("attr1"),
wxT("default-value"));
wxString attrvalue2 =
child-GetAttribute(wxT("attr2"),
wxT("default-value"));
...
} else if (child-GetName() == wxT("tag2")) {
// process tag2 ...
}
child = child-GetNext();
}
@endcode
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:
@code
wxXmlDocument doc;
doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES);
// myfile2.xml will be indentic to myfile.xml saving it this way:
doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION);
@endcode
Using default parameters, you will get a reformatted document which in general
is different from the original loaded content:
@code
wxXmlDocument doc;
doc.Load(wxT("myfile.xml"));
doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml
@endcode
@library{wxxml}
@category{xml}
@see wxXmlNode, wxXmlAttribute
*/
class wxXmlDocument : public wxObject
{
public:
/**
Default constructor.
*/
wxXmlDocument();
/**
Copy constructor. Deep copies all the XML tree of the given document.
*/
wxXmlDocument(const wxXmlDocument& doc);
/**
Loads the given filename using the given encoding. See Load().
*/
wxXmlDocument(const wxString& filename,
const wxString& encoding = wxT("UTF-8"));
/**
Loads the XML document from given stream using the given encoding. See Load().
*/
wxXmlDocument(wxInputStream& stream,
const wxString& encoding = wxT("UTF-8"));
/**
Virtual destructor. Frees the document root node.
*/
virtual ~wxXmlDocument();
/**
Detaches the document root node and returns it.
The document root node will be set to @NULL and thus IsOk() will
return @false after calling this function.
Note that the caller is reponsible for deleting the returned node in order
to avoid memory leaks.
*/
wxXmlNode* DetachRoot();
/**
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 @c wchar_t*.
*/
wxString GetEncoding() const;
/**
Returns encoding of document (may be empty).
@note This is the encoding original file was saved in, @b not the
encoding of in-memory representation!
*/
wxString GetFileEncoding() const;
/**
Returns the root node of the document.
*/
wxXmlNode* GetRoot() const;
/**
Returns the version of document.
This is the value in the @c \ header of the XML document.
If the version attribute was not explicitely given in the header, this function
returns an empty string.
*/
wxString GetVersion() const;
/**
Returns @true if the document has been loaded successfully.
*/
bool IsOk() const;
/**
Parses @a filename as an xml document and loads its data.
If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading,
all nodes of type @c 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.
*/
virtual bool Load(const wxString& filename,
const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
/**
Like Load(const wxString&, const wxString&, int) but takes the data from
given input stream.
*/
virtual bool Load(wxInputStream& stream,
const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
/**
Saves XML tree creating a file named with given string.
If @a indentstep is greater than or equal to zero, then, while saving,
an automatic indentation is added with steps composed by indentstep spaces.
If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation
is turned off.
*/
virtual bool Save(const wxString& filename, int indentstep = 1) const;
/**
Saves XML tree in the given output stream.
See Save(const wxString&, int) for a description of @a indentstep.
*/
virtual bool Save(wxOutputStream& stream, int indentstep = 1) const;
/**
Sets the enconding of the document.
*/
void SetEncoding(const wxString& enc);
/**
Sets the enconding of the file which will be used to save the document.
*/
void SetFileEncoding(const wxString& encoding);
/**
Sets the root node of this document. Deletes previous root node.
Use DetachRoot() and then SetRoot() if you want to replace the root
node without deleting the old document tree.
*/
void SetRoot(wxXmlNode* node);
/**
Sets the version of the XML file which will be used to save the document.
*/
void SetVersion(const wxString& version);
/**
Deep copies the given document.
*/
wxXmlDocument& operator=(const wxXmlDocument& doc);
};