1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
9 /// Represents XML node type.
12 // note: values are synchronized with xmlElementType from libxml
13 wxXML_ELEMENT_NODE
= 1,
14 wxXML_ATTRIBUTE_NODE
= 2,
16 wxXML_CDATA_SECTION_NODE
= 4,
17 wxXML_ENTITY_REF_NODE
= 5,
18 wxXML_ENTITY_NODE
= 6,
20 wxXML_COMMENT_NODE
= 8,
21 wxXML_DOCUMENT_NODE
= 9,
22 wxXML_DOCUMENT_TYPE_NODE
= 10,
23 wxXML_DOCUMENT_FRAG_NODE
= 11,
24 wxXML_NOTATION_NODE
= 12,
25 wxXML_HTML_DOCUMENT_NODE
= 13
31 Represents a node in an XML document. See wxXmlDocument.
33 Node has a name and may have content and attributes.
35 Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant)
36 and @c wxXML_ELEMENT_NODE.
38 Example: in <tt>\<title\>hi\</title\></tt> there is an element with the name
39 @c title and irrelevant content and one child of type @c wxXML_TEXT_NODE
40 with @c hi as content.
42 The @c wxXML_PI_NODE type sets the name to the PI target and the contents to
43 the instructions. Note that whilst the PI instructions are often in the form
44 of pseudo-attributes these do not use the nodes attribute system. It is the users
45 responsibility to code and decode the instruction text.
47 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
48 wxXmlDocument::Load (default is UTF-8).
53 @see wxXmlDocument, wxXmlAttribute
59 Creates this XML node and eventually insert it into an existing XML tree.
62 The parent node to which append this node instance.
63 If this argument is @NULL this new node will be floating and it can
64 be appended later to another one using the AddChild() or InsertChild()
65 functions. Otherwise the child is already added to the XML tree by
66 this constructor and it shouldn't be done again.
68 One of the ::wxXmlNodeType enumeration value.
70 The name of the node. This is the string which appears between angular brackets.
72 The content of the node.
73 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
75 If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
77 If not @NULL, this node and its eventual siblings are attached to the node.
79 Number of line this node was present at in input file or -1.
81 wxXmlNode(wxXmlNode
* parent
, wxXmlNodeType type
,
83 const wxString
& content
= wxEmptyString
,
84 wxXmlAttribute
* attrs
= NULL
,
85 wxXmlNode
* next
= NULL
, int lineNo
= -1);
88 A simplified version of the first constructor form, assuming a @NULL parent.
91 One of the ::wxXmlNodeType enumeration value.
93 The name of the node. This is the string which appears between angular brackets.
95 The content of the node.
96 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
98 Number of line this node was present at in input file or -1.
100 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
101 const wxString
& content
= wxEmptyString
,
107 Note that this does NOT copy siblings and parent pointer, i.e. GetParent()
108 and GetNext() will return @NULL after using copy ctor and are never unmodified by operator=().
109 On the other hand, it DOES copy children and attributes.
111 wxXmlNode(const wxXmlNode
& node
);
114 The virtual destructor. Deletes attached children and attributes.
116 virtual ~wxXmlNode();
119 Appends a attribute with given @a name and @a value to the list of
120 attributes for this node.
122 virtual void AddAttribute(const wxString
& name
, const wxString
& value
);
125 Appends given attribute to the list of attributes for this node.
127 virtual void AddAttribute(wxXmlAttribute
* attr
);
130 Adds node @a child as the last child of this node.
133 Note that this function works in O(n) time where @e n is the number
134 of existing children. Consequently, adding large number of child
135 nodes using this method can be expensive, because it has O(n^2) time
136 complexity in number of nodes to be added. Use InsertChildAfter() to
137 populate XML tree in linear time.
139 @see InsertChild(), InsertChildAfter()
141 virtual void AddChild(wxXmlNode
* child
);
144 Removes the first attributes which has the given @a name from the list of
145 attributes for this node.
147 virtual bool DeleteAttribute(const wxString
& name
);
150 Returns true if a attribute named attrName could be found.
151 The value of that attribute is saved in value (which must not be @NULL).
153 bool GetAttribute(const wxString
& attrName
, wxString
* value
) const;
156 Returns the value of the attribute named @a attrName if it does exist.
157 If it does not exist, the @a defaultVal is returned.
159 wxString
GetAttribute(const wxString
& attrName
,
160 const wxString
& defaultVal
= wxEmptyString
) const;
163 Return a pointer to the first attribute of this node.
165 wxXmlAttribute
* GetAttributes() const;
168 Returns the first child of this node.
169 To get a pointer to the second child of this node (if it does exist), use the
170 GetNext() function on the returned value.
172 wxXmlNode
* GetChildren() const;
175 Returns the content of this node. Can be an empty string.
176 Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
177 the content is an empty string. See GetNodeContent() for more details.
179 const wxString
& GetContent() const;
182 Returns the number of nodes which separate this node from @c grandparent.
184 This function searches only the parents of this node until it finds
185 @a grandparent or the @NULL node (which is the parent of non-linked
186 nodes or the parent of a wxXmlDocument's root element node).
188 int GetDepth(wxXmlNode
* grandparent
= NULL
) const;
191 Returns a flag indicating whether encoding conversion is necessary when saving. The default is @false.
193 You can improve saving efficiency considerably by setting this value.
195 bool GetNoConversion() const;
198 Returns line number of the node in the input XML file or @c -1 if it is unknown.
200 int GetLineNumber() const;
203 Returns the name of this node.
204 Can be an empty string (e.g. for nodes of type @c wxXML_TEXT_NODE or
205 @c wxXML_CDATA_SECTION_NODE).
207 const wxString
& GetName() const;
210 Returns a pointer to the sibling of this node or @NULL if there are no
213 wxXmlNode
* GetNext() const;
216 Returns the content of the first child node of type @c wxXML_TEXT_NODE
217 or @c wxXML_CDATA_SECTION_NODE.
218 This function is very useful since the XML snippet @c "tagnametagcontent/tagname"
219 is represented by expat with the following tag tree:
222 wxXML_ELEMENT_NODE name="tagname", content=""
223 |-- wxXML_TEXT_NODE name="", content="tagcontent"
229 wxXML_ELEMENT_NODE name="tagname", content=""
230 |-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
233 An empty string is returned if the node has no children of type
234 @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content
235 of the first child of such types is empty.
237 wxString
GetNodeContent() const;
240 Returns a pointer to the parent of this node or @NULL if this node has no
243 wxXmlNode
* GetParent() const;
246 Returns the type of this node.
248 wxXmlNodeType
GetType() const;
251 Returns @true if this node has a attribute named @a attrName.
253 bool HasAttribute(const wxString
& attrName
) const;
256 Inserts the @a child node immediately before @a followingNode in the
259 @return @true if @a followingNode has been found and the @a child
260 node has been inserted.
263 For historical reasons, @a followingNode may be @NULL. In that case,
264 then @a child is prepended to the list of children and becomes the
265 first child of this node, i.e. it behaves identically to using the
266 first children (as returned by GetChildren()) for @a followingNode).
268 @see AddChild(), InsertChildAfter()
270 virtual bool InsertChild(wxXmlNode
* child
, wxXmlNode
* followingNode
);
273 Inserts the @a child node immediately after @a precedingNode in the
276 @return @true if @a precedingNode has been found and the @a child
277 node has been inserted.
282 The node to insert @a child after. As a special case, this can be
283 @NULL if this node has no children yet -- in that case, @a child
284 will become this node's only child node.
288 @see InsertChild(), AddChild()
290 virtual bool InsertChildAfter(wxXmlNode
* child
, wxXmlNode
* precedingNode
);
293 Returns @true if the content of this node is a string containing only
294 whitespaces (spaces, tabs, new lines, etc).
296 Note that this function is locale-independent since the parsing of XML
297 documents must always produce the exact same tree regardless of the
298 locale it runs under.
300 bool IsWhitespaceOnly() const;
303 Removes the given node from the children list.
305 Returns @true if the node was found and removed or @false if the node
307 Note that the caller is responsible for deleting the removed node in order
308 to avoid memory leaks.
310 virtual bool RemoveChild(wxXmlNode
* child
);
313 Sets as first attribute the given wxXmlAttribute object.
315 The caller is responsible for deleting any previously present attributes
316 attached to this node.
318 void SetAttributes(wxXmlAttribute
* attr
);
321 Sets as first child the given node.
323 The caller is responsible for deleting any previously present children node.
325 void SetChildren(wxXmlNode
* child
);
328 Sets the content of this node.
330 void SetContent(const wxString
& con
);
333 Sets the name of this node.
335 void SetName(const wxString
& name
);
338 Sets as sibling the given node.
340 The caller is responsible for deleting any previously present sibling node.
342 void SetNext(wxXmlNode
* next
);
345 Sets a flag to indicate whether encoding conversion is necessary when saving. The default is @false.
347 You can improve saving efficiency considerably by setting this value.
349 void SetNoConversion(bool noconversion
);
352 Sets as parent the given node.
354 The caller is responsible for deleting any previously present parent node.
356 void SetParent(wxXmlNode
* parent
);
359 Sets the type of this node.
361 void SetType(wxXmlNodeType type
);
364 See the copy constructor for more info.
366 wxXmlNode
& operator=(const wxXmlNode
& node
);
372 @class wxXmlAttribute
374 Represents a node attribute.
376 Example: in <tt>\<img src="hello.gif" id="3"/\></tt>, @c src is an attribute
377 with value @c hello.gif and @c id is an attribute with value @c 3.
382 @see wxXmlDocument, wxXmlNode
393 Creates the attribute with given @a name and @a value.
394 If @a next is not @NULL, then sets it as sibling of this attribute.
396 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
397 wxXmlAttribute
* next
= NULL
);
400 The virtual destructor.
402 virtual ~wxXmlAttribute();
405 Returns the name of this attribute.
407 wxString
GetName() const;
410 Returns the sibling of this attribute or @NULL if there are no siblings.
412 wxXmlAttribute
* GetNext() const;
415 Returns the value of this attribute.
417 wxString
GetValue() const;
420 Sets the name of this attribute.
422 void SetName(const wxString
& name
);
425 Sets the sibling of this attribute.
427 void SetNext(wxXmlAttribute
* next
);
430 Sets the value of this attribute.
432 void SetValue(const wxString
& value
);
436 //* special indentation value for wxXmlDocument::Save
437 #define wxXML_NO_INDENTATION (-1)
439 //* flags for wxXmlDocument::Load
440 enum wxXmlDocumentLoadFlag
443 wxXMLDOC_KEEP_WHITESPACE_NODES
451 This class holds XML data/document as parsed by XML parser in the root node.
453 wxXmlDocument internally uses the expat library which comes with wxWidgets to
454 parse the given stream.
456 A simple example of using XML classes is:
460 if (!doc.Load("myfile.xml"))
463 // start processing the XML file
464 if (doc.GetRoot()->GetName() != "myroot-node")
468 wxXmlNode *prolog = doc.GetDocumentNode()->GetChildren();
471 if (prolog->GetType() == wxXML_PI_NODE && prolog->GetName() == "target") {
473 // process Process Instruction contents
474 wxString pi = prolog->GetContent();
481 wxXmlNode *child = doc.GetRoot()->GetChildren();
484 if (child->GetName() == "tag1") {
486 // process text enclosed by tag1/tag1
487 wxString content = child->GetNodeContent();
491 // process attributes of tag1
492 wxString attrvalue1 =
493 child->GetAttribute("attr1", "default-value");
494 wxString attrvalue2 =
495 child->GetAttribute("attr2", "default-value");
499 } else if (child->GetName() == "tag2") {
504 child = child->GetNext();
508 Note that if you want to preserve the original formatting of the loaded file
509 including whitespaces and indentation, you need to turn off whitespace-only
510 textnode removal and automatic indentation:
514 doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
516 // myfile2.xml will be identical to myfile.xml saving it this way:
517 doc.Save("myfile2.xml", wxXML_NO_INDENTATION);
520 Using default parameters, you will get a reformatted document which in general
521 is different from the original loaded content:
525 doc.Load("myfile.xml");
526 doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml
532 @see wxXmlNode, wxXmlAttribute
534 class wxXmlDocument
: public wxObject
543 Copy constructor. Deep copies all the XML tree of the given document.
545 wxXmlDocument(const wxXmlDocument
& doc
);
548 Loads the given filename using the given encoding. See Load().
550 wxXmlDocument(const wxString
& filename
,
551 const wxString
& encoding
= "UTF-8"));
554 Loads the XML document from given stream using the given encoding. See Load().
556 wxXmlDocument(wxInputStream
& stream
,
557 const wxString
& encoding
= "UTF-8");
560 Virtual destructor. Frees the document root node.
562 virtual ~wxXmlDocument();
565 Appends a Process Instruction or Comment node to the document prologue.
567 Calling this function will create a prologue or attach the node to the
568 end of an existing prologue.
572 void AppendToProlog(wxXmlNode
* node
);
575 Detaches the document node and returns it.
577 The document node will be set to @NULL and thus IsOk() will
578 return @false after calling this function.
580 Note that the caller is responsible for deleting the returned node in order
581 to avoid memory leaks.
585 wxXmlNode
* DetachDocumentNode();
588 Detaches the root entity node and returns it.
590 After calling this function, the document node will remain together with
591 any prologue nodes, but IsOk() will return @false since the root entity
594 Note that the caller is responsible for deleting the returned node in order
595 to avoid memory leaks.
597 wxXmlNode
* DetachRoot();
600 Returns encoding of in-memory representation of the document
601 (same as passed to Load() or constructor, defaults to UTF-8).
603 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
605 wxString
GetEncoding() const;
608 Returns encoding of document (may be empty).
610 @note This is the encoding original file was saved in, @b not the
611 encoding of in-memory representation!
613 const wxString
& GetFileEncoding() const;
616 Returns the document node of the document.
620 wxXmlNode
* GetDocumentNode() const;
623 Returns the root element node of the document.
625 wxXmlNode
* GetRoot() const;
628 Returns the version of document.
630 This is the value in the @c \<?xml version="1.0"?\> header of the XML document.
631 If the version attribute was not explicitly given in the header, this function
632 returns an empty string.
634 const wxString
& GetVersion() const;
637 Returns @true if the document has been loaded successfully.
643 Parses @a filename as an xml document and loads its data.
645 If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading,
646 all nodes of type @c wxXML_TEXT_NODE (see wxXmlNode) are automatically skipped
647 if they contain whitespaces only.
649 The removal of these nodes makes the load process slightly faster and requires
650 less memory however makes impossible to recreate exactly the loaded text with a
651 Save() call later. Read the initial description of this class for more info.
653 Returns true on success, false otherwise.
655 virtual bool Load(const wxString
& filename
,
656 const wxString
& encoding
= "UTF-8", int flags
= wxXMLDOC_NONE
);
659 Like Load(const wxString&, const wxString&, int) but takes the data from
662 virtual bool Load(wxInputStream
& stream
,
663 const wxString
& encoding
= "UTF-8", int flags
= wxXMLDOC_NONE
);
666 Saves XML tree creating a file named with given string.
668 If @a indentstep is greater than or equal to zero, then, while saving,
669 an automatic indentation is added with steps composed by indentstep spaces.
671 If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation
674 virtual bool Save(const wxString
& filename
, int indentstep
= 2) const;
677 Saves XML tree in the given output stream.
678 See Save(const wxString&, int) for a description of @a indentstep.
680 virtual bool Save(wxOutputStream
& stream
, int indentstep
= 2) const;
683 Sets the document node of this document.
685 Deletes any previous document node.
686 Use DetachDocumentNode() and then SetDocumentNode() if you want to
687 replace the document node without deleting the old document tree.
691 void SetDocumentNode(wxXmlNode
* node
);
694 Sets the encoding of the document.
696 void SetEncoding(const wxString
& enc
);
699 Sets the enconding of the file which will be used to save the document.
701 void SetFileEncoding(const wxString
& encoding
);
704 Sets the root element node of this document.
706 Will create the document node if necessary. Any previous
707 root element node is deleted.
709 void SetRoot(wxXmlNode
* node
);
712 Sets the version of the XML file which will be used to save the document.
714 void SetVersion(const wxString
& version
);
717 Deep copies the given document.
719 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
722 Get expat library version information.
727 static wxVersionInfo
GetLibraryVersionInfo();