]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/xml/xml.h
More wxMutex doc updates
[wxWidgets.git] / interface / wx / xml / xml.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: xml/xml.h
e54c96f1 3// Purpose: interface of wxXmlNode
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxXmlNode
7c913512 11
23324ae1 12 Represents a node in an XML document. See wxXmlDocument.
7c913512
FM
13
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
23324ae1
FM
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
18 with content="hi").
7c913512 19
23324ae1
FM
20 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
21 wxXmlDocument::Load (default is UTF-8).
7c913512 22
23324ae1
FM
23 @library{wxxml}
24 @category{xml}
7c913512 25
e54c96f1 26 @see wxXmlDocument, wxXmlAttribute
23324ae1 27*/
7c913512 28class wxXmlNode
23324ae1
FM
29{
30public:
31 //@{
32 /**
33 A simplified version of the first constructor form, assuming a @NULL parent.
34 */
35 wxXmlNode(wxXmlNode* parent, wxXmlNodeType type,
36 const wxString& name,
37 const wxString& content = wxEmptyString,
4cc4bfaf
FM
38 wxXmlAttribute* attrs = NULL,
39 wxXmlNode* next = NULL, int lineNo = -1);
7c913512
FM
40 wxXmlNode(const wxXmlNode& node);
41 wxXmlNode(wxXmlNodeType type, const wxString& name,
42 const wxString& content = wxEmptyString,
43 int lineNo = -1);
23324ae1
FM
44 //@}
45
46 /**
47 The virtual destructor. Deletes attached children and attributes.
48 */
49 ~wxXmlNode();
50
51 //@{
52 /**
53 Appends given attribute to the list of attributes for this node.
54 */
55 void AddAttribute(const wxString& name, const wxString& value);
7c913512 56 void AddAttribute(wxXmlAttribute* attr);
23324ae1
FM
57 //@}
58
59 /**
43a302f2
VS
60 Adds node @a child as the last child of this node.
61
62 @note
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.
68
69 @see InsertChild(), InsertChildAfter()
23324ae1
FM
70 */
71 void AddChild(wxXmlNode* child);
72
73 /**
4cc4bfaf 74 Removes the first attributes which has the given @a name from the list of
23324ae1
FM
75 attributes for this node.
76 */
77 bool DeleteAttribute(const wxString& name);
78
79 //@{
80 /**
4cc4bfaf
FM
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.
23324ae1 83 */
328f5751
FM
84 bool GetAttribute(const wxString& attrName, wxString* value) const;
85 const wxString GetAttribute(const wxString& attrName,
b5cc5cbd 86 const wxString& defaultVal = wxEmptyString) const;
23324ae1
FM
87 //@}
88
89 /**
90 Return a pointer to the first attribute of this node.
91 */
328f5751 92 wxXmlAttribute* GetAttributes() const;
23324ae1
FM
93
94 /**
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.
98 */
328f5751 99 wxXmlNode* GetChildren() const;
23324ae1
FM
100
101 /**
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)
104 the
105 content is an empty string. See GetNodeContent() for more details.
106 */
328f5751 107 wxString GetContent() const;
23324ae1
FM
108
109 /**
110 Returns the number of nodes which separe this node from @c grandparent.
23324ae1
FM
111 This function searches only the parents of this node until it finds @c
112 grandparent
113 or the @NULL node (which is the parent of non-linked nodes or the parent of a
114 wxXmlDocument's root node).
115 */
328f5751 116 int GetDepth(wxXmlNode* grandparent = NULL) const;
23324ae1
FM
117
118 /**
119 Returns line number of the node in the input XML file or -1 if it is unknown.
120 */
328f5751 121 int GetLineNumber() const;
23324ae1
FM
122
123 /**
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).
126 */
328f5751 127 wxString GetName() const;
23324ae1
FM
128
129 /**
130 Returns a pointer to the sibling of this node or @NULL if there are no
131 siblings.
132 */
328f5751 133 wxXmlNode* GetNext() const;
23324ae1
FM
134
135 /**
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:
b5cc5cbd 141
23324ae1 142 or eventually:
b5cc5cbd 143
23324ae1
FM
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.
146 */
328f5751 147 wxString GetNodeContent() const;
23324ae1
FM
148
149 /**
150 Returns a pointer to the parent of this node or @NULL if this node has no
151 parent.
152 */
328f5751 153 wxXmlNode* GetParent() const;
23324ae1
FM
154
155 /**
156 Returns the type of this node.
157 */
328f5751 158 wxXmlNodeType GetType() const;
23324ae1
FM
159
160 /**
161 Returns @true if this node has a attribute named @e attrName.
162 */
328f5751 163 bool HasAttribute(const wxString& attrName) const;
23324ae1
FM
164
165 /**
5e05df3c
VS
166 Inserts the @a child node immediately before @a followingNode in the
167 children list.
168
169 @return @true if @a followingNode has been found and the @a child
170 node has been inserted.
171
172 @note
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).
43a302f2
VS
177
178 @see AddChild(), InsertChildAfter()
5e05df3c
VS
179 */
180 bool InsertChild(wxXmlNode* child, wxXmlNode* followingNode);
23324ae1 181
43a302f2
VS
182 /**
183 Inserts the @a child node immediately after @a precedingNode in the
184 children list.
185
186 @return @true if @a precedingNode has been found and the @a child
187 node has been inserted.
188
189 @param precedingNode
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.
193
194 @since 2.8.8
195
196 @see InsertChild(), AddChild()
197 */
198 bool InsertChildAfter(wxXmlNode* child, wxXmlNode* precedingNode);
199
23324ae1
FM
200 /**
201 Returns @true if the content of this node is a string containing only
202 whitespaces (spaces,
203 tabs, new lines, etc). Note that this function is locale-independent since the
204 parsing of XML
205 documents must always produce the exact same tree regardless of the locale it
206 runs under.
207 */
328f5751 208 bool IsWhitespaceOnly() const;
23324ae1
FM
209
210 /**
211 Removes the given node from the children list. Returns @true if the node was
212 found and removed
213 or @false if the node could not be found.
23324ae1
FM
214 Note that the caller is reponsible for deleting the removed node in order to
215 avoid memory leaks.
216 */
217 bool RemoveChild(wxXmlNode* child);
218
219 /**
220 Sets as first attribute the given wxXmlAttribute object.
221 The caller is responsible to delete any previously present attributes attached
222 to this node.
223 */
224 void SetAttributes(wxXmlAttribute* attr);
225
226 /**
227 Sets as first child the given node. The caller is responsible to delete any
228 previously present
229 children node.
230 */
231 void SetChildren(wxXmlNode* child);
232
233 /**
234 Sets the content of this node.
235 */
236 void SetContent(const wxString& con);
237
238 /**
239 Sets the name of this node.
240 */
241 void SetName(const wxString& name);
242
243 /**
244 Sets as sibling the given node. The caller is responsible to delete any
245 previously present
246 sibling node.
247 */
248 void SetNext(wxXmlNode* next);
249
250 /**
251 Sets as parent the given node. The caller is responsible to delete any
252 previously present
253 parent node.
254 */
255 void SetParent(wxXmlNode* parent);
256
257 /**
258 Sets the type of this node.
259 */
260 void SetType(wxXmlNodeType type);
261
262 /**
263 See the copy constructor for more info.
264 */
265 wxXmlNode operator=(const wxXmlNode& node);
266};
267
268
e54c96f1 269
23324ae1
FM
270/**
271 @class wxXmlAttribute
7c913512 272
23324ae1 273 Represents a node attribute.
7c913512 274
23324ae1
FM
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".
7c913512 277
23324ae1
FM
278 @library{wxxml}
279 @category{xml}
7c913512 280
e54c96f1 281 @see wxXmlDocument, wxXmlNode
23324ae1 282*/
7c913512 283class wxXmlAttribute
23324ae1
FM
284{
285public:
286 //@{
287 /**
4cc4bfaf
FM
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.
23324ae1
FM
290 */
291 wxXmlAttribute();
7c913512 292 wxXmlAttribute(const wxString& name, const wxString& value,
4cc4bfaf 293 wxXmlAttribute* next = NULL);
23324ae1
FM
294 //@}
295
296 /**
297 The virtual destructor.
298 */
299 ~wxXmlAttribute();
300
301 /**
302 Returns the name of this attribute.
303 */
328f5751 304 wxString GetName() const;
23324ae1
FM
305
306 /**
307 Returns the sibling of this attribute or @NULL if there are no siblings.
308 */
328f5751 309 wxXmlAttribute* GetNext() const;
23324ae1
FM
310
311 /**
312 Returns the value of this attribute.
313 */
328f5751 314 wxString GetValue() const;
23324ae1
FM
315
316 /**
317 Sets the name of this attribute.
318 */
319 void SetName(const wxString& name);
320
321 /**
322 Sets the sibling of this attribute.
323 */
324 void SetNext(wxXmlAttribute* next);
325
326 /**
327 Sets the value of this attribute.
328 */
329 void SetValue(const wxString& value);
330};
331
332
e54c96f1 333
23324ae1
FM
334/**
335 @class wxXmlDocument
7c913512 336
23324ae1 337 This class holds XML data/document as parsed by XML parser in the root node.
7c913512 338
23324ae1
FM
339 wxXmlDocument internally uses the expat library which comes with wxWidgets to
340 parse the given stream.
7c913512 341
23324ae1 342 A simple example of using XML classes is:
7c913512 343
23324ae1
FM
344 @code
345 wxXmlDocument doc;
346 if (!doc.Load(wxT("myfile.xml")))
347 return @false;
7c913512 348
23324ae1
FM
349 // start processing the XML file
350 if (doc.GetRoot()-GetName() != wxT("myroot-node"))
351 return @false;
7c913512 352
23324ae1
FM
353 wxXmlNode *child = doc.GetRoot()-GetChildren();
354 while (child) {
7c913512 355
23324ae1 356 if (child-GetName() == wxT("tag1")) {
7c913512 357
23324ae1
FM
358 // process text enclosed by tag1/tag1
359 wxString content = child-GetNodeContent();
7c913512 360
23324ae1 361 ...
7c913512 362
23324ae1 363 // process attributes of tag1
7c913512
FM
364 wxString attrvalue1 =
365 child-GetAttribute(wxT("attr1"),
23324ae1 366 wxT("default-value"));
7c913512
FM
367 wxString attrvalue2 =
368 child-GetAttribute(wxT("attr2"),
23324ae1 369 wxT("default-value"));
7c913512 370
23324ae1 371 ...
7c913512 372
23324ae1 373 } else if (child-GetName() == wxT("tag2")) {
7c913512 374
23324ae1
FM
375 // process tag2 ...
376 }
7c913512 377
23324ae1
FM
378 child = child-GetNext();
379 }
380 @endcode
7c913512 381
cdbcf4c2 382 @note if you want to preserve the original formatting of the loaded file
23324ae1
FM
383 including whitespaces
384 and indentation, you need to turn off whitespace-only textnode removal and
385 automatic indentation:
7c913512 386
23324ae1
FM
387 @code
388 wxXmlDocument doc;
389 doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES);
7c913512 390
23324ae1
FM
391 // myfile2.xml will be indentic to myfile.xml saving it this way:
392 doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION);
393 @endcode
7c913512 394
23324ae1
FM
395 Using default parameters, you will get a reformatted document which in general
396 is different from
397 the original loaded content:
7c913512 398
23324ae1
FM
399 @code
400 wxXmlDocument doc;
401 doc.Load(wxT("myfile.xml"));
402 doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml
403 @endcode
7c913512 404
23324ae1
FM
405 @library{wxxml}
406 @category{xml}
7c913512 407
e54c96f1 408 @see wxXmlNode, wxXmlAttribute
23324ae1
FM
409*/
410class wxXmlDocument : public wxObject
411{
412public:
413 //@{
414 /**
415 Copy constructor. Deep copies all the XML tree of the given document.
416 */
417 wxXmlDocument();
7c913512
FM
418 wxXmlDocument(const wxString& filename);
419 wxXmlDocument(wxInputStream& stream);
420 wxXmlDocument(const wxXmlDocument& doc);
23324ae1
FM
421 //@}
422
423 /**
424 Virtual destructor. Frees the document root node.
425 */
426 ~wxXmlDocument();
427
428 /**
429 Detaches the document root node and returns it. The document root node will be
430 set to @NULL
431 and thus IsOk() will return @false after calling this function.
23324ae1
FM
432 Note that the caller is reponsible for deleting the returned node in order to
433 avoid memory leaks.
434 */
435 wxXmlNode* DetachRoot();
436
437 /**
438 Returns encoding of in-memory representation of the document
439 (same as passed to Load() or constructor, defaults to UTF-8).
1add55ae 440 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
23324ae1 441 */
328f5751 442 wxString GetEncoding() const;
23324ae1
FM
443
444 /**
445 Returns encoding of document (may be empty).
23324ae1
FM
446 Note: this is the encoding original file was saved in, @b not the
447 encoding of in-memory representation!
448 */
328f5751 449 wxString GetFileEncoding() const;
23324ae1
FM
450
451 /**
452 Returns the root node of the document.
453 */
328f5751 454 wxXmlNode* GetRoot() const;
23324ae1
FM
455
456 /**
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.
461 */
328f5751 462 wxString GetVersion() const;
23324ae1
FM
463
464 /**
465 Returns @true if the document has been loaded successfully.
466 */
328f5751 467 bool IsOk() const;
23324ae1
FM
468
469 //@{
470 /**
471 , @b int@e flags = wxXMLDOC_NONE)
23324ae1
FM
472 Like above but takes the data from given input stream.
473 */
474 bool Load(const wxString& filename);
7c913512 475 int bool Load(wxInputStream& stream);
23324ae1
FM
476 //@}
477
478 //@{
479 /**
480 Saves XML tree in the given output stream. See other overload for a description
481 of @c indentstep.
482 */
328f5751
FM
483 bool Save(const wxString& filename, int indentstep = 1) const;
484 const bool Save(wxOutputStream& stream, int indentstep = 1) const;
23324ae1
FM
485 //@}
486
487 /**
488 Sets the enconding of the document.
489 */
490 void SetEncoding(const wxString& enc);
491
492 /**
493 Sets the enconding of the file which will be used to save the document.
494 */
495 void SetFileEncoding(const wxString& encoding);
496
497 /**
498 Sets the root node of this document. Deletes previous root node.
7c913512 499 Use DetachRoot() and then
23324ae1
FM
500 SetRoot() if you want
501 to replace the root node without deleting the old document tree.
502 */
503 void SetRoot(wxXmlNode* node);
504
505 /**
506 Sets the version of the XML file which will be used to save the document.
507 */
508 void SetVersion(const wxString& version);
509
510 /**
511 Deep copies the given document.
512 */
513 wxXmlDocument& operator operator=(const wxXmlDocument& doc);
514};
e54c96f1 515