2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: xml/xml.h
|
2008-09-27 06:27:44 -04:00
|
|
|
// Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxXmlNode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Represents a node in an XML document. See wxXmlDocument.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
Node has a name and may have content and attributes.
|
|
|
|
|
|
|
|
Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant)
|
2008-12-16 15:42:12 -05:00
|
|
|
and @c wxXML_ELEMENT_NODE.
|
|
|
|
|
|
|
|
Example: in <tt>\<title\>hi\</title\></tt> there is an element with the name
|
|
|
|
@c title and irrelevant content and one child of type @c wxXML_TEXT_NODE
|
|
|
|
with @c hi as content.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
|
|
|
|
wxXmlDocument::Load (default is UTF-8).
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxxml}
|
|
|
|
@category{xml}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxXmlDocument, wxXmlAttribute
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxXmlNode
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Creates this XML node and eventually insert it into an existing XML tree.
|
|
|
|
|
|
|
|
@param parent
|
|
|
|
The parent node to which append this node instance.
|
|
|
|
If this argument is @NULL this new node will be floating and it can
|
|
|
|
be appended later to another one using the AddChild() or InsertChild()
|
|
|
|
functions. Otherwise the child is already added to the XML tree by
|
|
|
|
this constructor and it shouldn't be done again.
|
|
|
|
@param type
|
|
|
|
One of the ::wxXmlNodeType enumeration value.
|
|
|
|
@param name
|
|
|
|
The name of the node. This is the string which appears between angular brackets.
|
|
|
|
@param content
|
|
|
|
The content of the node.
|
|
|
|
Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
|
|
|
|
@param attrs
|
|
|
|
If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
|
|
|
|
@param next
|
|
|
|
If not @NULL, this node and its eventual siblings are attached to the node.
|
|
|
|
@param lineNo
|
|
|
|
Number of line this node was present at in input file or -1.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxXmlNode(wxXmlNode* parent, wxXmlNodeType type,
|
|
|
|
const wxString& name,
|
|
|
|
const wxString& content = wxEmptyString,
|
2008-03-09 08:33:59 -04:00
|
|
|
wxXmlAttribute* attrs = NULL,
|
|
|
|
wxXmlNode* next = NULL, int lineNo = -1);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
A simplified version of the first constructor form, assuming a @NULL parent.
|
|
|
|
|
|
|
|
@param type
|
|
|
|
One of the ::wxXmlNodeType enumeration value.
|
|
|
|
@param name
|
|
|
|
The name of the node. This is the string which appears between angular brackets.
|
|
|
|
@param content
|
|
|
|
The content of the node.
|
|
|
|
Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
|
|
|
|
@param lineNo
|
|
|
|
Number of line this node was present at in input file or -1.
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxXmlNode(wxXmlNodeType type, const wxString& name,
|
|
|
|
const wxString& content = wxEmptyString,
|
|
|
|
int lineNo = -1);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Copy constructor.
|
|
|
|
|
|
|
|
Note that this does NOT copy syblings and parent pointer, i.e. GetParent()
|
|
|
|
and GetNext() will return @NULL after using copy ctor and are never unmodified by operator=().
|
|
|
|
On the other hand, it DOES copy children and attributes.
|
|
|
|
*/
|
|
|
|
wxXmlNode(const wxXmlNode& node);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
The virtual destructor. Deletes attached children and attributes.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual ~wxXmlNode();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Appends a attribute with given @a name and @a value to the list of
|
|
|
|
attributes for this node.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual void AddAttribute(const wxString& name, const wxString& value);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Appends given attribute to the list of attributes for this node.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual void AddAttribute(wxXmlAttribute* attr);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-30 06:27:19 -04:00
|
|
|
Adds node @a child as the last child of this node.
|
|
|
|
|
|
|
|
@note
|
|
|
|
Note that this function works in O(n) time where @e n is the number
|
|
|
|
of existing children. Consequently, adding large number of child
|
|
|
|
nodes using this method can be expensive, because it has O(n^2) time
|
|
|
|
complexity in number of nodes to be added. Use InsertChildAfter() to
|
|
|
|
populate XML tree in linear time.
|
|
|
|
|
|
|
|
@see InsertChild(), InsertChildAfter()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual void AddChild(wxXmlNode* child);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Removes the first attributes which has the given @a name from the list of
|
2008-03-08 08:52:38 -05:00
|
|
|
attributes for this node.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual bool DeleteAttribute(const wxString& name);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
/**
|
|
|
|
Returns true if a attribute named attrName could be found.
|
|
|
|
The value of that attribute is saved in value (which must not be @NULL).
|
|
|
|
*/
|
|
|
|
bool GetAttribute(const wxString& attrName, wxString* value) const;
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Returns the value of the attribute named @a attrName if it does exist.
|
|
|
|
If it does not exist, the @a defaultVal is returned.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 06:27:44 -04:00
|
|
|
wxString GetAttribute(const wxString& attrName,
|
|
|
|
const wxString& defaultVal = wxEmptyString) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Return a pointer to the first attribute of this node.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlAttribute* GetAttributes() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the first child of this node.
|
|
|
|
To get a pointer to the second child of this node (if it does exist), use the
|
|
|
|
GetNext() function on the returned value.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlNode* GetChildren() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the content of this node. Can be an empty string.
|
|
|
|
Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
|
2008-09-27 06:27:44 -04:00
|
|
|
the content is an empty string. See GetNodeContent() for more details.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 16:26:28 -04:00
|
|
|
const wxString& GetContent() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the number of nodes which separe this node from @c grandparent.
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
This function searches only the parents of this node until it finds
|
|
|
|
@a grandparent or the @NULL node (which is the parent of non-linked
|
|
|
|
nodes or the parent of a wxXmlDocument's root node).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int GetDepth(wxXmlNode* grandparent = NULL) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Returns line number of the node in the input XML file or @c -1 if it is unknown.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int GetLineNumber() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Returns the name of this node.
|
|
|
|
Can be an empty string (e.g. for nodes of type @c wxXML_TEXT_NODE or
|
|
|
|
@c wxXML_CDATA_SECTION_NODE).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 16:26:28 -04:00
|
|
|
const wxString& GetName() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a pointer to the sibling of this node or @NULL if there are no
|
|
|
|
siblings.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlNode* GetNext() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Returns the content of the first child node of type @c wxXML_TEXT_NODE
|
|
|
|
or @c wxXML_CDATA_SECTION_NODE.
|
|
|
|
This function is very useful since the XML snippet @c "tagnametagcontent/tagname"
|
|
|
|
is represented by expat with the following tag tree:
|
|
|
|
|
|
|
|
@code
|
|
|
|
wxXML_ENTITY_NODE name="tagname", content=""
|
|
|
|
|-- wxXML_TEXT_NODE name="", content="tagcontent"
|
|
|
|
@endcode
|
2008-03-22 18:54:07 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
or eventually:
|
2008-03-22 18:54:07 -04:00
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
@code
|
|
|
|
wxXML_ENTITY_NODE name="tagname", content=""
|
|
|
|
|-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
An empty string is returned if the node has no children of type
|
|
|
|
@c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content
|
|
|
|
of the first child of such types is empty.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxString GetNodeContent() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a pointer to the parent of this node or @NULL if this node has no
|
|
|
|
parent.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlNode* GetParent() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the type of this node.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlNodeType GetType() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Returns @true if this node has a attribute named @a attrName.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool HasAttribute(const wxString& attrName) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-29 18:55:35 -04:00
|
|
|
Inserts the @a child node immediately before @a followingNode in the
|
|
|
|
children list.
|
|
|
|
|
|
|
|
@return @true if @a followingNode has been found and the @a child
|
|
|
|
node has been inserted.
|
|
|
|
|
|
|
|
@note
|
|
|
|
For historical reasons, @a followingNode may be @NULL. In that case,
|
|
|
|
then @a child is prepended to the list of children and becomes the
|
|
|
|
first child of this node, i.e. it behaves identically to using the
|
|
|
|
first children (as returned by GetChildren()) for @a followingNode).
|
2008-03-30 06:27:19 -04:00
|
|
|
|
|
|
|
@see AddChild(), InsertChildAfter()
|
2008-03-29 18:55:35 -04:00
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual bool InsertChild(wxXmlNode* child, wxXmlNode* followingNode);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2008-03-30 06:27:19 -04:00
|
|
|
/**
|
|
|
|
Inserts the @a child node immediately after @a precedingNode in the
|
|
|
|
children list.
|
|
|
|
|
|
|
|
@return @true if @a precedingNode has been found and the @a child
|
|
|
|
node has been inserted.
|
|
|
|
|
2008-10-04 10:52:38 -04:00
|
|
|
@param child
|
|
|
|
The child to insert.
|
2008-03-30 06:27:19 -04:00
|
|
|
@param precedingNode
|
|
|
|
The node to insert @a child after. As a special case, this can be
|
|
|
|
@NULL if this node has no children yet -- in that case, @a child
|
|
|
|
will become this node's only child node.
|
|
|
|
|
|
|
|
@since 2.8.8
|
|
|
|
|
|
|
|
@see InsertChild(), AddChild()
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual bool InsertChildAfter(wxXmlNode* child, wxXmlNode* precedingNode);
|
2008-03-30 06:27:19 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
Returns @true if the content of this node is a string containing only
|
2008-09-27 06:27:44 -04:00
|
|
|
whitespaces (spaces, tabs, new lines, etc).
|
|
|
|
|
|
|
|
Note that this function is locale-independent since the parsing of XML
|
|
|
|
documents must always produce the exact same tree regardless of the
|
|
|
|
locale it runs under.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsWhitespaceOnly() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Removes the given node from the children list.
|
|
|
|
|
|
|
|
Returns @true if the node was found and removed or @false if the node
|
|
|
|
could not be found.
|
|
|
|
Note that the caller is reponsible for deleting the removed node in order
|
|
|
|
to avoid memory leaks.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual bool RemoveChild(wxXmlNode* child);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Sets as first attribute the given wxXmlAttribute object.
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
The caller is responsible to delete any previously present attributes
|
|
|
|
attached to this node.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetAttributes(wxXmlAttribute* attr);
|
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Sets as first child the given node.
|
|
|
|
|
|
|
|
The caller is responsible to delete any previously present children node.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetChildren(wxXmlNode* child);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sets the content of this node.
|
|
|
|
*/
|
|
|
|
void SetContent(const wxString& con);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sets the name of this node.
|
|
|
|
*/
|
|
|
|
void SetName(const wxString& name);
|
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Sets as sibling the given node.
|
|
|
|
|
|
|
|
The caller is responsible to delete any previously present sibling node.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetNext(wxXmlNode* next);
|
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Sets as parent the given node.
|
|
|
|
|
|
|
|
The caller is responsible to delete any previously present parent node.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetParent(wxXmlNode* parent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sets the type of this node.
|
|
|
|
*/
|
|
|
|
void SetType(wxXmlNodeType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
See the copy constructor for more info.
|
|
|
|
*/
|
2008-09-27 06:27:44 -04:00
|
|
|
wxXmlNode& operator=(const wxXmlNode& node);
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxXmlAttribute
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Represents a node attribute.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-12-16 15:42:12 -05:00
|
|
|
Example: in <tt>\<img src="hello.gif" id="3"/\></tt>, @c src is an attribute
|
|
|
|
with value @c hello.gif and @c id is an attribute with value @c 3.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxxml}
|
|
|
|
@category{xml}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxXmlDocument, wxXmlNode
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxXmlAttribute
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Default constructor.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxXmlAttribute();
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Creates the attribute with given @a name and @a value.
|
|
|
|
If @a next is not @NULL, then sets it as sibling of this attribute.
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxXmlAttribute(const wxString& name, const wxString& value,
|
2008-03-09 08:33:59 -04:00
|
|
|
wxXmlAttribute* next = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
The virtual destructor.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual ~wxXmlAttribute();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the name of this attribute.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxString GetName() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the sibling of this attribute or @NULL if there are no siblings.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlAttribute* GetNext() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the value of this attribute.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxString GetValue() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxXmlDocument
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
This class holds XML data/document as parsed by XML parser in the root node.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
wxXmlDocument internally uses the expat library which comes with wxWidgets to
|
|
|
|
parse the given stream.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
A simple example of using XML classes is:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
wxXmlDocument doc;
|
2008-12-04 16:50:52 -05:00
|
|
|
if (!doc.Load("myfile.xml"))
|
2008-12-16 15:42:12 -05:00
|
|
|
return false;
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
// start processing the XML file
|
2008-12-04 16:50:52 -05:00
|
|
|
if (doc.GetRoot()->GetName() != "myroot-node")
|
2008-12-16 15:42:12 -05:00
|
|
|
return false;
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-12-04 16:50:52 -05:00
|
|
|
wxXmlNode *child = doc.GetRoot()->GetChildren();
|
2008-03-08 08:52:38 -05:00
|
|
|
while (child) {
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-12-16 15:42:12 -05:00
|
|
|
if (child->GetName() == "tag1") {
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
// process text enclosed by tag1/tag1
|
2008-12-04 16:50:52 -05:00
|
|
|
wxString content = child->GetNodeContent();
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
...
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
// process attributes of tag1
|
2008-03-08 09:43:31 -05:00
|
|
|
wxString attrvalue1 =
|
2008-12-04 16:50:52 -05:00
|
|
|
child->GetAttribute("attr1", "default-value");
|
2008-03-08 09:43:31 -05:00
|
|
|
wxString attrvalue2 =
|
2008-12-04 16:50:52 -05:00
|
|
|
child->GetAttribute("attr2", "default-value");
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
...
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-12-04 16:50:52 -05:00
|
|
|
} else if (child->GetName() == "tag2") {
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
// process tag2 ...
|
|
|
|
}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-12-04 16:50:52 -05:00
|
|
|
child = child->GetNext();
|
2008-03-08 08:52:38 -05:00
|
|
|
}
|
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
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:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
wxXmlDocument doc;
|
2008-12-04 16:50:52 -05:00
|
|
|
doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
// myfile2.xml will be indentic to myfile.xml saving it this way:
|
2008-12-04 16:50:52 -05:00
|
|
|
doc.Save("myfile2.xml", wxXML_NO_INDENTATION);
|
2008-03-08 08:52:38 -05:00
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Using default parameters, you will get a reformatted document which in general
|
2008-09-27 06:27:44 -04:00
|
|
|
is different from the original loaded content:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
wxXmlDocument doc;
|
2008-12-04 16:50:52 -05:00
|
|
|
doc.Load("myfile.xml");
|
|
|
|
doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml
|
2008-03-08 08:52:38 -05:00
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxxml}
|
|
|
|
@category{xml}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxXmlNode, wxXmlAttribute
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
class wxXmlDocument : public wxObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Default constructor.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxXmlDocument();
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Copy constructor. Deep copies all the XML tree of the given document.
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxXmlDocument(const wxXmlDocument& doc);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Loads the given filename using the given encoding. See Load().
|
|
|
|
*/
|
|
|
|
wxXmlDocument(const wxString& filename,
|
2008-12-04 16:50:52 -05:00
|
|
|
const wxString& encoding = "UTF-8"));
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Loads the XML document from given stream using the given encoding. See Load().
|
|
|
|
*/
|
|
|
|
wxXmlDocument(wxInputStream& stream,
|
2009-01-18 16:46:46 -05:00
|
|
|
const wxString& encoding = "UTF-8");
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Virtual destructor. Frees the document root node.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual ~wxXmlDocument();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
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.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxXmlNode* DetachRoot();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns encoding of in-memory representation of the document
|
|
|
|
(same as passed to Load() or constructor, defaults to UTF-8).
|
2008-09-27 06:27:44 -04:00
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
@note this is meaningless in Unicode build where data are stored as @c wchar_t*.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxString GetEncoding() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns encoding of document (may be empty).
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
@note This is the encoding original file was saved in, @b not the
|
|
|
|
encoding of in-memory representation!
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 16:26:28 -04:00
|
|
|
const wxString& GetFileEncoding() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the root node of the document.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxXmlNode* GetRoot() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the version of document.
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
This is the value in the @c \<?xml version="1.0"?\> header of the XML document.
|
2008-03-08 08:52:38 -05:00
|
|
|
If the version attribute was not explicitely given in the header, this function
|
|
|
|
returns an empty string.
|
|
|
|
*/
|
2008-09-27 16:26:28 -04:00
|
|
|
const wxString& GetVersion() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the document has been loaded successfully.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOk() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2008-09-27 06:27:44 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
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,
|
2009-01-18 16:46:46 -05:00
|
|
|
const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Like Load(const wxString&, const wxString&, int) but takes the data from
|
|
|
|
given input stream.
|
|
|
|
*/
|
|
|
|
virtual bool Load(wxInputStream& stream,
|
2009-01-18 16:46:46 -05:00
|
|
|
const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE);
|
2008-09-27 06:27:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 06:27:44 -04:00
|
|
|
virtual bool Save(const wxString& filename, int indentstep = 1) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-09-27 06:27:44 -04:00
|
|
|
Saves XML tree in the given output stream.
|
|
|
|
See Save(const wxString&, int) for a description of @a indentstep.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-27 06:27:44 -04:00
|
|
|
virtual bool Save(wxOutputStream& stream, int indentstep = 1) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
2008-09-27 06:27:44 -04:00
|
|
|
Use DetachRoot() and then SetRoot() if you want to replace the root
|
|
|
|
node without deleting the old document tree.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2008-09-27 06:27:44 -04:00
|
|
|
wxXmlDocument& operator=(const wxXmlDocument& doc);
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|