]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/xml/xml.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxXmlNode
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 Represents a node in an XML document. See wxXmlDocument.
14 Node has a name and may have content and attributes. Most common node types are
15 @c wxXML_TEXT_NODE (name and attributes are irrelevant) and
16 @c wxXML_ELEMENT_NODE (e.g. in @c titlehi/title there is an element
17 with name="title", irrelevant content and one child (@c wxXML_TEXT_NODE
20 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
21 wxXmlDocument::Load (default is UTF-8).
26 @see wxXmlDocument, wxXmlAttribute
33 A simplified version of the first constructor form, assuming a @NULL parent.
35 wxXmlNode(wxXmlNode
* parent
, wxXmlNodeType type
,
37 const wxString
& content
= wxEmptyString
,
38 wxXmlAttribute
* attrs
= NULL
,
39 wxXmlNode
* next
= NULL
, int lineNo
= -1);
40 wxXmlNode(const wxXmlNode
& node
);
41 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
42 const wxString
& content
= wxEmptyString
,
47 The virtual destructor. Deletes attached children and attributes.
53 Appends given attribute to the list of attributes for this node.
55 void AddAttribute(const wxString
& name
, const wxString
& value
);
56 void AddAttribute(wxXmlAttribute
* attr
);
60 Adds node @a child as the last child of this node.
63 Note that this function works in O(n) time where @e n is the number
64 of existing children. Consequently, adding large number of child
65 nodes using this method can be expensive, because it has O(n^2) time
66 complexity in number of nodes to be added. Use InsertChildAfter() to
67 populate XML tree in linear time.
69 @see InsertChild(), InsertChildAfter()
71 void AddChild(wxXmlNode
* child
);
74 Removes the first attributes which has the given @a name from the list of
75 attributes for this node.
77 bool DeleteAttribute(const wxString
& name
);
81 Returns the value of the attribute named @a attrName if it does exist.
82 If it does not exist, the @a defaultVal is returned.
84 bool GetAttribute(const wxString
& attrName
, wxString
* value
) const;
85 const wxString
GetAttribute(const wxString
& attrName
,
86 const wxString
& defaultVal
= wxEmptyString
) const;
90 Return a pointer to the first attribute of this node.
92 wxXmlAttribute
* GetAttributes() const;
95 Returns the first child of this node.
96 To get a pointer to the second child of this node (if it does exist), use the
97 GetNext() function on the returned value.
99 wxXmlNode
* GetChildren() const;
102 Returns the content of this node. Can be an empty string.
103 Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
105 content is an empty string. See GetNodeContent() for more details.
107 wxString
GetContent() const;
110 Returns the number of nodes which separe this node from @c grandparent.
111 This function searches only the parents of this node until it finds @c
113 or the @NULL node (which is the parent of non-linked nodes or the parent of a
114 wxXmlDocument's root node).
116 int GetDepth(wxXmlNode
* grandparent
= NULL
) const;
119 Returns line number of the node in the input XML file or -1 if it is unknown.
121 int GetLineNumber() const;
124 Returns the name of this node. Can be an empty string (e.g. for nodes of type
125 @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE).
127 wxString
GetName() const;
130 Returns a pointer to the sibling of this node or @NULL if there are no
133 wxXmlNode
* GetNext() const;
136 Returns the content of the first child node of type @c wxXML_TEXT_NODE or @c
137 wxXML_CDATA_SECTION_NODE.
138 This function is very useful since the XML snippet @c
139 "tagnametagcontent/tagname" is represented by
140 expat with the following tag tree:
144 An empty string is returned if the node has no children of type @c
145 wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content of the first child of such types is empty.
147 wxString
GetNodeContent() const;
150 Returns a pointer to the parent of this node or @NULL if this node has no
153 wxXmlNode
* GetParent() const;
156 Returns the type of this node.
158 wxXmlNodeType
GetType() const;
161 Returns @true if this node has a attribute named @e attrName.
163 bool HasAttribute(const wxString
& attrName
) const;
166 Inserts the @a child node immediately before @a followingNode in the
169 @return @true if @a followingNode has been found and the @a child
170 node has been inserted.
173 For historical reasons, @a followingNode may be @NULL. In that case,
174 then @a child is prepended to the list of children and becomes the
175 first child of this node, i.e. it behaves identically to using the
176 first children (as returned by GetChildren()) for @a followingNode).
178 @see AddChild(), InsertChildAfter()
180 bool InsertChild(wxXmlNode
* child
, wxXmlNode
* followingNode
);
183 Inserts the @a child node immediately after @a precedingNode in the
186 @return @true if @a precedingNode has been found and the @a child
187 node has been inserted.
190 The node to insert @a child after. As a special case, this can be
191 @NULL if this node has no children yet -- in that case, @a child
192 will become this node's only child node.
196 @see InsertChild(), AddChild()
198 bool InsertChildAfter(wxXmlNode
* child
, wxXmlNode
* precedingNode
);
201 Returns @true if the content of this node is a string containing only
203 tabs, new lines, etc). Note that this function is locale-independent since the
205 documents must always produce the exact same tree regardless of the locale it
208 bool IsWhitespaceOnly() const;
211 Removes the given node from the children list. Returns @true if the node was
213 or @false if the node could not be found.
214 Note that the caller is reponsible for deleting the removed node in order to
217 bool RemoveChild(wxXmlNode
* child
);
220 Sets as first attribute the given wxXmlAttribute object.
221 The caller is responsible to delete any previously present attributes attached
224 void SetAttributes(wxXmlAttribute
* attr
);
227 Sets as first child the given node. The caller is responsible to delete any
231 void SetChildren(wxXmlNode
* child
);
234 Sets the content of this node.
236 void SetContent(const wxString
& con
);
239 Sets the name of this node.
241 void SetName(const wxString
& name
);
244 Sets as sibling the given node. The caller is responsible to delete any
248 void SetNext(wxXmlNode
* next
);
251 Sets as parent the given node. The caller is responsible to delete any
255 void SetParent(wxXmlNode
* parent
);
258 Sets the type of this node.
260 void SetType(wxXmlNodeType type
);
263 See the copy constructor for more info.
265 wxXmlNode
operator=(const wxXmlNode
& node
);
271 @class wxXmlAttribute
273 Represents a node attribute.
275 Example: in @c img src="hello.gif" id="3"/, @c "src" is attribute with value
276 @c "hello.gif" and @c "id" is a attribute with value @c "3".
281 @see wxXmlDocument, wxXmlNode
288 Creates the attribute with given @a name and @e value.
289 If @a next is not @NULL, then sets it as sibling of this attribute.
292 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
293 wxXmlAttribute
* next
= NULL
);
297 The virtual destructor.
302 Returns the name of this attribute.
304 wxString
GetName() const;
307 Returns the sibling of this attribute or @NULL if there are no siblings.
309 wxXmlAttribute
* GetNext() const;
312 Returns the value of this attribute.
314 wxString
GetValue() const;
317 Sets the name of this attribute.
319 void SetName(const wxString
& name
);
322 Sets the sibling of this attribute.
324 void SetNext(wxXmlAttribute
* next
);
327 Sets the value of this attribute.
329 void SetValue(const wxString
& value
);
337 This class holds XML data/document as parsed by XML parser in the root node.
339 wxXmlDocument internally uses the expat library which comes with wxWidgets to
340 parse the given stream.
342 A simple example of using XML classes is:
346 if (!doc.Load(wxT("myfile.xml")))
349 // start processing the XML file
350 if (doc.GetRoot()-GetName() != wxT("myroot-node"))
353 wxXmlNode *child = doc.GetRoot()-GetChildren();
356 if (child-GetName() == wxT("tag1")) {
358 // process text enclosed by tag1/tag1
359 wxString content = child-GetNodeContent();
363 // process attributes of tag1
364 wxString attrvalue1 =
365 child-GetAttribute(wxT("attr1"),
366 wxT("default-value"));
367 wxString attrvalue2 =
368 child-GetAttribute(wxT("attr2"),
369 wxT("default-value"));
373 } else if (child-GetName() == wxT("tag2")) {
378 child = child-GetNext();
382 @note if you want to preserve the original formatting of the loaded file
383 including whitespaces
384 and indentation, you need to turn off whitespace-only textnode removal and
385 automatic indentation:
389 doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES);
391 // myfile2.xml will be indentic to myfile.xml saving it this way:
392 doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION);
395 Using default parameters, you will get a reformatted document which in general
397 the original loaded content:
401 doc.Load(wxT("myfile.xml"));
402 doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml
408 @see wxXmlNode, wxXmlAttribute
410 class wxXmlDocument
: public wxObject
415 Copy constructor. Deep copies all the XML tree of the given document.
418 wxXmlDocument(const wxString
& filename
);
419 wxXmlDocument(wxInputStream
& stream
);
420 wxXmlDocument(const wxXmlDocument
& doc
);
424 Virtual destructor. Frees the document root node.
429 Detaches the document root node and returns it. The document root node will be
431 and thus IsOk() will return @false after calling this function.
432 Note that the caller is reponsible for deleting the returned node in order to
435 wxXmlNode
* DetachRoot();
438 Returns encoding of in-memory representation of the document
439 (same as passed to Load() or constructor, defaults to UTF-8).
440 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
442 wxString
GetEncoding() const;
445 Returns encoding of document (may be empty).
446 Note: this is the encoding original file was saved in, @b not the
447 encoding of in-memory representation!
449 wxString
GetFileEncoding() const;
452 Returns the root node of the document.
454 wxXmlNode
* GetRoot() const;
457 Returns the version of document.
458 This is the value in the @c ?xml version="1.0"? header of the XML document.
459 If the version attribute was not explicitely given in the header, this function
460 returns an empty string.
462 wxString
GetVersion() const;
465 Returns @true if the document has been loaded successfully.
471 , @b int@e flags = wxXMLDOC_NONE)
472 Like above but takes the data from given input stream.
474 bool Load(const wxString
& filename
);
475 int bool Load(wxInputStream
& stream
);
480 Saves XML tree in the given output stream. See other overload for a description
483 bool Save(const wxString
& filename
, int indentstep
= 1) const;
484 const bool Save(wxOutputStream
& stream
, int indentstep
= 1) const;
488 Sets the enconding of the document.
490 void SetEncoding(const wxString
& enc
);
493 Sets the enconding of the file which will be used to save the document.
495 void SetFileEncoding(const wxString
& encoding
);
498 Sets the root node of this document. Deletes previous root node.
499 Use DetachRoot() and then
500 SetRoot() if you want
501 to replace the root node without deleting the old document tree.
503 void SetRoot(wxXmlNode
* node
);
506 Sets the version of the XML file which will be used to save the document.
508 void SetVersion(const wxString
& version
);
511 Deep copies the given document.
513 wxXmlDocument
& operator operator=(const wxXmlDocument
& doc
);