]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: xml/xml.h | |
f41d6c8c | 3 | // Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
f41d6c8c FM |
9 | |
10 | /// Represents XML node type. | |
11 | enum wxXmlNodeType | |
12 | { | |
13 | // note: values are synchronized with xmlElementType from libxml | |
14 | wxXML_ELEMENT_NODE = 1, | |
15 | wxXML_ATTRIBUTE_NODE = 2, | |
16 | wxXML_TEXT_NODE = 3, | |
17 | wxXML_CDATA_SECTION_NODE = 4, | |
18 | wxXML_ENTITY_REF_NODE = 5, | |
19 | wxXML_ENTITY_NODE = 6, | |
20 | wxXML_PI_NODE = 7, | |
21 | wxXML_COMMENT_NODE = 8, | |
22 | wxXML_DOCUMENT_NODE = 9, | |
23 | wxXML_DOCUMENT_TYPE_NODE = 10, | |
24 | wxXML_DOCUMENT_FRAG_NODE = 11, | |
25 | wxXML_NOTATION_NODE = 12, | |
26 | wxXML_HTML_DOCUMENT_NODE = 13 | |
27 | }; | |
28 | ||
23324ae1 FM |
29 | /** |
30 | @class wxXmlNode | |
7c913512 | 31 | |
23324ae1 | 32 | Represents a node in an XML document. See wxXmlDocument. |
7c913512 | 33 | |
f41d6c8c FM |
34 | Node has a name and may have content and attributes. |
35 | ||
36 | Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant) | |
4ce846b1 FM |
37 | and @c wxXML_ELEMENT_NODE. |
38 | ||
39 | Example: in <tt>\<title\>hi\</title\></tt> there is an element with the name | |
40 | @c title and irrelevant content and one child of type @c wxXML_TEXT_NODE | |
41 | with @c hi as content. | |
7c913512 | 42 | |
cee8636e VZ |
43 | The @c wxXML_PI_NODE type sets the name to the PI target and the contents to |
44 | the instructions. Note that whilst the PI instructions are often in the form | |
45 | of pseudo-attributes these do not use the nodes attribute system. It is the users | |
46 | responsibility to code and decode the instruction text. | |
47 | ||
23324ae1 FM |
48 | If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to |
49 | wxXmlDocument::Load (default is UTF-8). | |
7c913512 | 50 | |
23324ae1 FM |
51 | @library{wxxml} |
52 | @category{xml} | |
7c913512 | 53 | |
e54c96f1 | 54 | @see wxXmlDocument, wxXmlAttribute |
23324ae1 | 55 | */ |
7c913512 | 56 | class wxXmlNode |
23324ae1 FM |
57 | { |
58 | public: | |
23324ae1 | 59 | /** |
f41d6c8c FM |
60 | Creates this XML node and eventually insert it into an existing XML tree. |
61 | ||
62 | @param parent | |
63 | The parent node to which append this node instance. | |
64 | If this argument is @NULL this new node will be floating and it can | |
65 | be appended later to another one using the AddChild() or InsertChild() | |
66 | functions. Otherwise the child is already added to the XML tree by | |
67 | this constructor and it shouldn't be done again. | |
68 | @param type | |
69 | One of the ::wxXmlNodeType enumeration value. | |
70 | @param name | |
71 | The name of the node. This is the string which appears between angular brackets. | |
72 | @param content | |
73 | The content of the node. | |
74 | Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE. | |
75 | @param attrs | |
76 | If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node. | |
77 | @param next | |
78 | If not @NULL, this node and its eventual siblings are attached to the node. | |
79 | @param lineNo | |
80 | Number of line this node was present at in input file or -1. | |
23324ae1 FM |
81 | */ |
82 | wxXmlNode(wxXmlNode* parent, wxXmlNodeType type, | |
83 | const wxString& name, | |
84 | const wxString& content = wxEmptyString, | |
4cc4bfaf FM |
85 | wxXmlAttribute* attrs = NULL, |
86 | wxXmlNode* next = NULL, int lineNo = -1); | |
f41d6c8c FM |
87 | |
88 | /** | |
89 | A simplified version of the first constructor form, assuming a @NULL parent. | |
90 | ||
91 | @param type | |
92 | One of the ::wxXmlNodeType enumeration value. | |
93 | @param name | |
94 | The name of the node. This is the string which appears between angular brackets. | |
95 | @param content | |
96 | The content of the node. | |
97 | Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE. | |
98 | @param lineNo | |
99 | Number of line this node was present at in input file or -1. | |
100 | */ | |
7c913512 FM |
101 | wxXmlNode(wxXmlNodeType type, const wxString& name, |
102 | const wxString& content = wxEmptyString, | |
103 | int lineNo = -1); | |
f41d6c8c FM |
104 | |
105 | /** | |
106 | Copy constructor. | |
107 | ||
57ab6f23 | 108 | Note that this does NOT copy siblings and parent pointer, i.e. GetParent() |
f41d6c8c FM |
109 | and GetNext() will return @NULL after using copy ctor and are never unmodified by operator=(). |
110 | On the other hand, it DOES copy children and attributes. | |
111 | */ | |
112 | wxXmlNode(const wxXmlNode& node); | |
23324ae1 FM |
113 | |
114 | /** | |
115 | The virtual destructor. Deletes attached children and attributes. | |
116 | */ | |
adaaa686 | 117 | virtual ~wxXmlNode(); |
23324ae1 | 118 | |
23324ae1 | 119 | /** |
f41d6c8c FM |
120 | Appends a attribute with given @a name and @a value to the list of |
121 | attributes for this node. | |
23324ae1 | 122 | */ |
adaaa686 | 123 | virtual void AddAttribute(const wxString& name, const wxString& value); |
f41d6c8c FM |
124 | |
125 | /** | |
126 | Appends given attribute to the list of attributes for this node. | |
127 | */ | |
adaaa686 | 128 | virtual void AddAttribute(wxXmlAttribute* attr); |
23324ae1 FM |
129 | |
130 | /** | |
43a302f2 VS |
131 | Adds node @a child as the last child of this node. |
132 | ||
133 | @note | |
134 | Note that this function works in O(n) time where @e n is the number | |
135 | of existing children. Consequently, adding large number of child | |
136 | nodes using this method can be expensive, because it has O(n^2) time | |
137 | complexity in number of nodes to be added. Use InsertChildAfter() to | |
138 | populate XML tree in linear time. | |
139 | ||
140 | @see InsertChild(), InsertChildAfter() | |
23324ae1 | 141 | */ |
adaaa686 | 142 | virtual void AddChild(wxXmlNode* child); |
23324ae1 FM |
143 | |
144 | /** | |
4cc4bfaf | 145 | Removes the first attributes which has the given @a name from the list of |
23324ae1 FM |
146 | attributes for this node. |
147 | */ | |
adaaa686 | 148 | virtual bool DeleteAttribute(const wxString& name); |
23324ae1 | 149 | |
f41d6c8c FM |
150 | /** |
151 | Returns true if a attribute named attrName could be found. | |
152 | The value of that attribute is saved in value (which must not be @NULL). | |
153 | */ | |
154 | bool GetAttribute(const wxString& attrName, wxString* value) const; | |
155 | ||
23324ae1 | 156 | /** |
4cc4bfaf FM |
157 | Returns the value of the attribute named @a attrName if it does exist. |
158 | If it does not exist, the @a defaultVal is returned. | |
23324ae1 | 159 | */ |
f41d6c8c FM |
160 | wxString GetAttribute(const wxString& attrName, |
161 | const wxString& defaultVal = wxEmptyString) const; | |
23324ae1 FM |
162 | |
163 | /** | |
164 | Return a pointer to the first attribute of this node. | |
165 | */ | |
328f5751 | 166 | wxXmlAttribute* GetAttributes() const; |
23324ae1 FM |
167 | |
168 | /** | |
169 | Returns the first child of this node. | |
170 | To get a pointer to the second child of this node (if it does exist), use the | |
171 | GetNext() function on the returned value. | |
172 | */ | |
328f5751 | 173 | wxXmlNode* GetChildren() const; |
23324ae1 FM |
174 | |
175 | /** | |
176 | Returns the content of this node. Can be an empty string. | |
177 | Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type) | |
f41d6c8c | 178 | the content is an empty string. See GetNodeContent() for more details. |
23324ae1 | 179 | */ |
95b4a59e | 180 | const wxString& GetContent() const; |
23324ae1 FM |
181 | |
182 | /** | |
57ab6f23 | 183 | Returns the number of nodes which separate this node from @c grandparent. |
f41d6c8c FM |
184 | |
185 | This function searches only the parents of this node until it finds | |
186 | @a grandparent or the @NULL node (which is the parent of non-linked | |
cee8636e | 187 | nodes or the parent of a wxXmlDocument's root element node). |
23324ae1 | 188 | */ |
328f5751 | 189 | int GetDepth(wxXmlNode* grandparent = NULL) const; |
23324ae1 | 190 | |
30f6914b JS |
191 | /** |
192 | Returns a flag indicating whether encoding conversion is necessary when saving. The default is @false. | |
193 | ||
194 | You can improve saving efficiency considerably by setting this value. | |
195 | */ | |
196 | bool GetNoConversion() const; | |
197 | ||
23324ae1 | 198 | /** |
f41d6c8c | 199 | Returns line number of the node in the input XML file or @c -1 if it is unknown. |
23324ae1 | 200 | */ |
328f5751 | 201 | int GetLineNumber() const; |
23324ae1 FM |
202 | |
203 | /** | |
f41d6c8c FM |
204 | Returns the name of this node. |
205 | Can be an empty string (e.g. for nodes of type @c wxXML_TEXT_NODE or | |
206 | @c wxXML_CDATA_SECTION_NODE). | |
23324ae1 | 207 | */ |
95b4a59e | 208 | const wxString& GetName() const; |
23324ae1 FM |
209 | |
210 | /** | |
211 | Returns a pointer to the sibling of this node or @NULL if there are no | |
212 | siblings. | |
213 | */ | |
328f5751 | 214 | wxXmlNode* GetNext() const; |
23324ae1 FM |
215 | |
216 | /** | |
f41d6c8c FM |
217 | Returns the content of the first child node of type @c wxXML_TEXT_NODE |
218 | or @c wxXML_CDATA_SECTION_NODE. | |
219 | This function is very useful since the XML snippet @c "tagnametagcontent/tagname" | |
220 | is represented by expat with the following tag tree: | |
221 | ||
222 | @code | |
cee8636e | 223 | wxXML_ELEMENT_NODE name="tagname", content="" |
f41d6c8c FM |
224 | |-- wxXML_TEXT_NODE name="", content="tagcontent" |
225 | @endcode | |
b5cc5cbd | 226 | |
23324ae1 | 227 | or eventually: |
b5cc5cbd | 228 | |
f41d6c8c | 229 | @code |
cee8636e | 230 | wxXML_ELEMENT_NODE name="tagname", content="" |
f41d6c8c FM |
231 | |-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent" |
232 | @endcode | |
233 | ||
234 | An empty string is returned if the node has no children of type | |
235 | @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content | |
236 | of the first child of such types is empty. | |
23324ae1 | 237 | */ |
328f5751 | 238 | wxString GetNodeContent() const; |
23324ae1 FM |
239 | |
240 | /** | |
241 | Returns a pointer to the parent of this node or @NULL if this node has no | |
242 | parent. | |
243 | */ | |
328f5751 | 244 | wxXmlNode* GetParent() const; |
23324ae1 FM |
245 | |
246 | /** | |
247 | Returns the type of this node. | |
248 | */ | |
328f5751 | 249 | wxXmlNodeType GetType() const; |
23324ae1 FM |
250 | |
251 | /** | |
f41d6c8c | 252 | Returns @true if this node has a attribute named @a attrName. |
23324ae1 | 253 | */ |
328f5751 | 254 | bool HasAttribute(const wxString& attrName) const; |
23324ae1 FM |
255 | |
256 | /** | |
5e05df3c VS |
257 | Inserts the @a child node immediately before @a followingNode in the |
258 | children list. | |
259 | ||
260 | @return @true if @a followingNode has been found and the @a child | |
261 | node has been inserted. | |
262 | ||
263 | @note | |
264 | For historical reasons, @a followingNode may be @NULL. In that case, | |
265 | then @a child is prepended to the list of children and becomes the | |
266 | first child of this node, i.e. it behaves identically to using the | |
267 | first children (as returned by GetChildren()) for @a followingNode). | |
43a302f2 VS |
268 | |
269 | @see AddChild(), InsertChildAfter() | |
5e05df3c | 270 | */ |
adaaa686 | 271 | virtual bool InsertChild(wxXmlNode* child, wxXmlNode* followingNode); |
23324ae1 | 272 | |
43a302f2 VS |
273 | /** |
274 | Inserts the @a child node immediately after @a precedingNode in the | |
275 | children list. | |
276 | ||
277 | @return @true if @a precedingNode has been found and the @a child | |
278 | node has been inserted. | |
279 | ||
77bfb902 FM |
280 | @param child |
281 | The child to insert. | |
43a302f2 VS |
282 | @param precedingNode |
283 | The node to insert @a child after. As a special case, this can be | |
284 | @NULL if this node has no children yet -- in that case, @a child | |
285 | will become this node's only child node. | |
286 | ||
287 | @since 2.8.8 | |
288 | ||
289 | @see InsertChild(), AddChild() | |
290 | */ | |
adaaa686 | 291 | virtual bool InsertChildAfter(wxXmlNode* child, wxXmlNode* precedingNode); |
43a302f2 | 292 | |
23324ae1 FM |
293 | /** |
294 | Returns @true if the content of this node is a string containing only | |
f41d6c8c FM |
295 | whitespaces (spaces, tabs, new lines, etc). |
296 | ||
297 | Note that this function is locale-independent since the parsing of XML | |
298 | documents must always produce the exact same tree regardless of the | |
299 | locale it runs under. | |
23324ae1 | 300 | */ |
328f5751 | 301 | bool IsWhitespaceOnly() const; |
23324ae1 FM |
302 | |
303 | /** | |
f41d6c8c FM |
304 | Removes the given node from the children list. |
305 | ||
306 | Returns @true if the node was found and removed or @false if the node | |
307 | could not be found. | |
57ab6f23 | 308 | Note that the caller is responsible for deleting the removed node in order |
f41d6c8c | 309 | to avoid memory leaks. |
23324ae1 | 310 | */ |
adaaa686 | 311 | virtual bool RemoveChild(wxXmlNode* child); |
23324ae1 FM |
312 | |
313 | /** | |
314 | Sets as first attribute the given wxXmlAttribute object. | |
f41d6c8c | 315 | |
30f6914b | 316 | The caller is responsible for deleting any previously present attributes |
f41d6c8c | 317 | attached to this node. |
23324ae1 FM |
318 | */ |
319 | void SetAttributes(wxXmlAttribute* attr); | |
320 | ||
321 | /** | |
f41d6c8c FM |
322 | Sets as first child the given node. |
323 | ||
30f6914b | 324 | The caller is responsible for deleting any previously present children node. |
23324ae1 FM |
325 | */ |
326 | void SetChildren(wxXmlNode* child); | |
327 | ||
328 | /** | |
329 | Sets the content of this node. | |
330 | */ | |
331 | void SetContent(const wxString& con); | |
332 | ||
333 | /** | |
334 | Sets the name of this node. | |
335 | */ | |
336 | void SetName(const wxString& name); | |
337 | ||
338 | /** | |
f41d6c8c FM |
339 | Sets as sibling the given node. |
340 | ||
30f6914b | 341 | The caller is responsible for deleting any previously present sibling node. |
23324ae1 FM |
342 | */ |
343 | void SetNext(wxXmlNode* next); | |
344 | ||
30f6914b JS |
345 | /** |
346 | Sets a flag to indicate whether encoding conversion is necessary when saving. The default is @false. | |
347 | ||
348 | You can improve saving efficiency considerably by setting this value. | |
349 | */ | |
350 | void SetNoConversion(bool noconversion); | |
351 | ||
23324ae1 | 352 | /** |
f41d6c8c FM |
353 | Sets as parent the given node. |
354 | ||
30f6914b | 355 | The caller is responsible for deleting any previously present parent node. |
23324ae1 FM |
356 | */ |
357 | void SetParent(wxXmlNode* parent); | |
358 | ||
359 | /** | |
360 | Sets the type of this node. | |
361 | */ | |
362 | void SetType(wxXmlNodeType type); | |
363 | ||
364 | /** | |
365 | See the copy constructor for more info. | |
366 | */ | |
f41d6c8c | 367 | wxXmlNode& operator=(const wxXmlNode& node); |
23324ae1 FM |
368 | }; |
369 | ||
370 | ||
e54c96f1 | 371 | |
23324ae1 FM |
372 | /** |
373 | @class wxXmlAttribute | |
7c913512 | 374 | |
23324ae1 | 375 | Represents a node attribute. |
7c913512 | 376 | |
4ce846b1 FM |
377 | Example: in <tt>\<img src="hello.gif" id="3"/\></tt>, @c src is an attribute |
378 | with value @c hello.gif and @c id is an attribute with value @c 3. | |
7c913512 | 379 | |
23324ae1 FM |
380 | @library{wxxml} |
381 | @category{xml} | |
7c913512 | 382 | |
e54c96f1 | 383 | @see wxXmlDocument, wxXmlNode |
23324ae1 | 384 | */ |
7c913512 | 385 | class wxXmlAttribute |
23324ae1 FM |
386 | { |
387 | public: | |
23324ae1 | 388 | /** |
f41d6c8c | 389 | Default constructor. |
23324ae1 FM |
390 | */ |
391 | wxXmlAttribute(); | |
f41d6c8c FM |
392 | |
393 | /** | |
394 | Creates the attribute with given @a name and @a value. | |
395 | If @a next is not @NULL, then sets it as sibling of this attribute. | |
396 | */ | |
7c913512 | 397 | wxXmlAttribute(const wxString& name, const wxString& value, |
4cc4bfaf | 398 | wxXmlAttribute* next = NULL); |
23324ae1 FM |
399 | |
400 | /** | |
401 | The virtual destructor. | |
402 | */ | |
adaaa686 | 403 | virtual ~wxXmlAttribute(); |
23324ae1 FM |
404 | |
405 | /** | |
406 | Returns the name of this attribute. | |
407 | */ | |
328f5751 | 408 | wxString GetName() const; |
23324ae1 FM |
409 | |
410 | /** | |
411 | Returns the sibling of this attribute or @NULL if there are no siblings. | |
412 | */ | |
328f5751 | 413 | wxXmlAttribute* GetNext() const; |
23324ae1 FM |
414 | |
415 | /** | |
416 | Returns the value of this attribute. | |
417 | */ | |
328f5751 | 418 | wxString GetValue() const; |
23324ae1 FM |
419 | |
420 | /** | |
421 | Sets the name of this attribute. | |
422 | */ | |
423 | void SetName(const wxString& name); | |
424 | ||
425 | /** | |
426 | Sets the sibling of this attribute. | |
427 | */ | |
428 | void SetNext(wxXmlAttribute* next); | |
429 | ||
430 | /** | |
431 | Sets the value of this attribute. | |
432 | */ | |
433 | void SetValue(const wxString& value); | |
434 | }; | |
435 | ||
436 | ||
e54c96f1 | 437 | |
23324ae1 FM |
438 | /** |
439 | @class wxXmlDocument | |
7c913512 | 440 | |
23324ae1 | 441 | This class holds XML data/document as parsed by XML parser in the root node. |
7c913512 | 442 | |
23324ae1 FM |
443 | wxXmlDocument internally uses the expat library which comes with wxWidgets to |
444 | parse the given stream. | |
7c913512 | 445 | |
23324ae1 | 446 | A simple example of using XML classes is: |
7c913512 | 447 | |
23324ae1 FM |
448 | @code |
449 | wxXmlDocument doc; | |
1707da0c | 450 | if (!doc.Load("myfile.xml")) |
4ce846b1 | 451 | return false; |
7c913512 | 452 | |
23324ae1 | 453 | // start processing the XML file |
1707da0c | 454 | if (doc.GetRoot()->GetName() != "myroot-node") |
4ce846b1 | 455 | return false; |
7c913512 | 456 | |
cee8636e VZ |
457 | // examine prologue |
458 | wxXmlNode *prolog = doc.GetDocumentNode()->GetChildren(); | |
459 | while (prolog) { | |
460 | ||
461 | if (prolog->GetType() == wxXML_PI_NODE && prolog->GetName() == "target") { | |
462 | ||
463 | // process Process Instruction contents | |
464 | wxString pi = prolog->GetContent(); | |
465 | ||
466 | ... | |
467 | ||
468 | } | |
469 | } | |
470 | ||
1707da0c | 471 | wxXmlNode *child = doc.GetRoot()->GetChildren(); |
23324ae1 | 472 | while (child) { |
7c913512 | 473 | |
4ce846b1 | 474 | if (child->GetName() == "tag1") { |
7c913512 | 475 | |
23324ae1 | 476 | // process text enclosed by tag1/tag1 |
1707da0c | 477 | wxString content = child->GetNodeContent(); |
7c913512 | 478 | |
23324ae1 | 479 | ... |
7c913512 | 480 | |
23324ae1 | 481 | // process attributes of tag1 |
7c913512 | 482 | wxString attrvalue1 = |
1707da0c | 483 | child->GetAttribute("attr1", "default-value"); |
7c913512 | 484 | wxString attrvalue2 = |
1707da0c | 485 | child->GetAttribute("attr2", "default-value"); |
7c913512 | 486 | |
23324ae1 | 487 | ... |
7c913512 | 488 | |
1707da0c | 489 | } else if (child->GetName() == "tag2") { |
7c913512 | 490 | |
23324ae1 FM |
491 | // process tag2 ... |
492 | } | |
7c913512 | 493 | |
1707da0c | 494 | child = child->GetNext(); |
23324ae1 FM |
495 | } |
496 | @endcode | |
7c913512 | 497 | |
f41d6c8c FM |
498 | Note that if you want to preserve the original formatting of the loaded file |
499 | including whitespaces and indentation, you need to turn off whitespace-only | |
500 | textnode removal and automatic indentation: | |
7c913512 | 501 | |
23324ae1 FM |
502 | @code |
503 | wxXmlDocument doc; | |
1707da0c | 504 | doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES); |
7c913512 | 505 | |
57ab6f23 | 506 | // myfile2.xml will be identical to myfile.xml saving it this way: |
1707da0c | 507 | doc.Save("myfile2.xml", wxXML_NO_INDENTATION); |
23324ae1 | 508 | @endcode |
7c913512 | 509 | |
23324ae1 | 510 | Using default parameters, you will get a reformatted document which in general |
f41d6c8c | 511 | is different from the original loaded content: |
7c913512 | 512 | |
23324ae1 FM |
513 | @code |
514 | wxXmlDocument doc; | |
1707da0c FM |
515 | doc.Load("myfile.xml"); |
516 | doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml | |
23324ae1 | 517 | @endcode |
7c913512 | 518 | |
23324ae1 FM |
519 | @library{wxxml} |
520 | @category{xml} | |
7c913512 | 521 | |
e54c96f1 | 522 | @see wxXmlNode, wxXmlAttribute |
23324ae1 FM |
523 | */ |
524 | class wxXmlDocument : public wxObject | |
525 | { | |
526 | public: | |
23324ae1 | 527 | /** |
f41d6c8c | 528 | Default constructor. |
23324ae1 FM |
529 | */ |
530 | wxXmlDocument(); | |
f41d6c8c FM |
531 | |
532 | /** | |
533 | Copy constructor. Deep copies all the XML tree of the given document. | |
534 | */ | |
7c913512 | 535 | wxXmlDocument(const wxXmlDocument& doc); |
f41d6c8c FM |
536 | |
537 | /** | |
538 | Loads the given filename using the given encoding. See Load(). | |
539 | */ | |
540 | wxXmlDocument(const wxString& filename, | |
1707da0c | 541 | const wxString& encoding = "UTF-8")); |
f41d6c8c FM |
542 | |
543 | /** | |
544 | Loads the XML document from given stream using the given encoding. See Load(). | |
545 | */ | |
546 | wxXmlDocument(wxInputStream& stream, | |
f8ebb70d | 547 | const wxString& encoding = "UTF-8"); |
23324ae1 FM |
548 | |
549 | /** | |
550 | Virtual destructor. Frees the document root node. | |
551 | */ | |
adaaa686 | 552 | virtual ~wxXmlDocument(); |
23324ae1 FM |
553 | |
554 | /** | |
cee8636e VZ |
555 | Appends a Process Instruction or Comment node to the document prologue. |
556 | ||
557 | Calling this function will create a prologue or attach the node to the | |
558 | end of an existing prologue. | |
559 | ||
560 | @since 2.9.2 | |
561 | */ | |
562 | void AppendToProlog(wxXmlNode* node); | |
563 | ||
564 | /** | |
565 | Detaches the document node and returns it. | |
f41d6c8c | 566 | |
cee8636e | 567 | The document node will be set to @NULL and thus IsOk() will |
f41d6c8c FM |
568 | return @false after calling this function. |
569 | ||
57ab6f23 | 570 | Note that the caller is responsible for deleting the returned node in order |
f41d6c8c | 571 | to avoid memory leaks. |
cee8636e VZ |
572 | |
573 | @since 2.9.2 | |
574 | */ | |
575 | wxXmlNode* DetachDocumentNode(); | |
576 | ||
577 | /** | |
578 | Detaches the root entity node and returns it. | |
579 | ||
580 | After calling this function, the document node will remain together with | |
581 | any prologue nodes, but IsOk() will return @false since the root entity | |
582 | will be missing. | |
583 | ||
584 | Note that the caller is reponsible for deleting the returned node in order | |
585 | to avoid memory leaks. | |
23324ae1 FM |
586 | */ |
587 | wxXmlNode* DetachRoot(); | |
588 | ||
589 | /** | |
590 | Returns encoding of in-memory representation of the document | |
591 | (same as passed to Load() or constructor, defaults to UTF-8). | |
f41d6c8c | 592 | |
1add55ae | 593 | @note this is meaningless in Unicode build where data are stored as @c wchar_t*. |
23324ae1 | 594 | */ |
328f5751 | 595 | wxString GetEncoding() const; |
23324ae1 FM |
596 | |
597 | /** | |
598 | Returns encoding of document (may be empty). | |
f41d6c8c FM |
599 | |
600 | @note This is the encoding original file was saved in, @b not the | |
601 | encoding of in-memory representation! | |
23324ae1 | 602 | */ |
95b4a59e | 603 | const wxString& GetFileEncoding() const; |
23324ae1 FM |
604 | |
605 | /** | |
cee8636e VZ |
606 | Returns the document node of the document. |
607 | ||
608 | @since 2.9.2 | |
609 | */ | |
610 | wxXmlNode* GetDocumentNode() const; | |
611 | ||
612 | /** | |
613 | Returns the root element node of the document. | |
23324ae1 | 614 | */ |
328f5751 | 615 | wxXmlNode* GetRoot() const; |
23324ae1 FM |
616 | |
617 | /** | |
618 | Returns the version of document. | |
f41d6c8c FM |
619 | |
620 | This is the value in the @c \<?xml version="1.0"?\> header of the XML document. | |
57ab6f23 | 621 | If the version attribute was not explicitly given in the header, this function |
23324ae1 FM |
622 | returns an empty string. |
623 | */ | |
95b4a59e | 624 | const wxString& GetVersion() const; |
23324ae1 FM |
625 | |
626 | /** | |
627 | Returns @true if the document has been loaded successfully. | |
628 | */ | |
328f5751 | 629 | bool IsOk() const; |
23324ae1 | 630 | |
f41d6c8c | 631 | |
23324ae1 | 632 | /** |
f41d6c8c FM |
633 | Parses @a filename as an xml document and loads its data. |
634 | ||
635 | If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading, | |
636 | all nodes of type @c wxXML_TEXT_NODE (see wxXmlNode) are automatically skipped | |
637 | if they contain whitespaces only. | |
638 | ||
639 | The removal of these nodes makes the load process slightly faster and requires | |
640 | less memory however makes impossible to recreate exactly the loaded text with a | |
641 | Save() call later. Read the initial description of this class for more info. | |
642 | ||
643 | Returns true on success, false otherwise. | |
644 | */ | |
645 | virtual bool Load(const wxString& filename, | |
f8ebb70d | 646 | const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE); |
f41d6c8c FM |
647 | |
648 | /** | |
649 | Like Load(const wxString&, const wxString&, int) but takes the data from | |
650 | given input stream. | |
651 | */ | |
652 | virtual bool Load(wxInputStream& stream, | |
f8ebb70d | 653 | const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE); |
f41d6c8c FM |
654 | |
655 | /** | |
656 | Saves XML tree creating a file named with given string. | |
657 | ||
658 | If @a indentstep is greater than or equal to zero, then, while saving, | |
659 | an automatic indentation is added with steps composed by indentstep spaces. | |
660 | ||
661 | If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation | |
662 | is turned off. | |
23324ae1 | 663 | */ |
57cc93eb | 664 | virtual bool Save(const wxString& filename, int indentstep = 2) const; |
23324ae1 | 665 | |
23324ae1 | 666 | /** |
f41d6c8c FM |
667 | Saves XML tree in the given output stream. |
668 | See Save(const wxString&, int) for a description of @a indentstep. | |
23324ae1 | 669 | */ |
57cc93eb | 670 | virtual bool Save(wxOutputStream& stream, int indentstep = 2) const; |
23324ae1 FM |
671 | |
672 | /** | |
cee8636e VZ |
673 | Sets the document node of this document. |
674 | ||
675 | Deletes any previous document node. | |
676 | Use DetachDocumentNode() and then SetDocumentNode() if you want to | |
677 | replace the document node without deleting the old document tree. | |
678 | ||
679 | @since 2.9.2 | |
680 | */ | |
681 | void SetDocumentNode(wxXmlNode* node); | |
682 | ||
683 | /** | |
684 | Sets the encoding of the document. | |
23324ae1 FM |
685 | */ |
686 | void SetEncoding(const wxString& enc); | |
687 | ||
688 | /** | |
689 | Sets the enconding of the file which will be used to save the document. | |
690 | */ | |
691 | void SetFileEncoding(const wxString& encoding); | |
692 | ||
693 | /** | |
cee8636e VZ |
694 | Sets the root element node of this document. |
695 | ||
696 | Will create the document node if necessary. Any previous | |
697 | root element node is deleted. | |
23324ae1 FM |
698 | */ |
699 | void SetRoot(wxXmlNode* node); | |
700 | ||
701 | /** | |
702 | Sets the version of the XML file which will be used to save the document. | |
703 | */ | |
704 | void SetVersion(const wxString& version); | |
705 | ||
706 | /** | |
707 | Deep copies the given document. | |
708 | */ | |
f41d6c8c | 709 | wxXmlDocument& operator=(const wxXmlDocument& doc); |
ccec9093 VZ |
710 | |
711 | /** | |
712 | Get expat library version information. | |
713 | ||
714 | @since 2.9.2 | |
715 | @see wxVersionInfo | |
716 | */ | |
717 | static wxVersionInfo GetLibraryVersionInfo(); | |
23324ae1 | 718 | }; |
e54c96f1 | 719 |