1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 /// Represents XML node type.
13 // note: values are synchronized with xmlElementType from libxml
14 wxXML_ELEMENT_NODE
= 1,
15 wxXML_ATTRIBUTE_NODE
= 2,
17 wxXML_CDATA_SECTION_NODE
= 4,
18 wxXML_ENTITY_REF_NODE
= 5,
19 wxXML_ENTITY_NODE
= 6,
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
32 Represents a node in an XML document. See wxXmlDocument.
34 Node has a name and may have content and attributes.
36 Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant)
37 and @c wxXML_ELEMENT_NODE.
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.
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.
48 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
49 wxXmlDocument::Load (default is UTF-8).
54 @see wxXmlDocument, wxXmlAttribute
60 Creates this XML node and eventually insert it into an existing XML tree.
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.
69 One of the ::wxXmlNodeType enumeration value.
71 The name of the node. This is the string which appears between angular brackets.
73 The content of the node.
74 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
76 If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
78 If not @NULL, this node and its eventual siblings are attached to the node.
80 Number of line this node was present at in input file or -1.
82 wxXmlNode(wxXmlNode
* parent
, wxXmlNodeType type
,
84 const wxString
& content
= wxEmptyString
,
85 wxXmlAttribute
* attrs
= NULL
,
86 wxXmlNode
* next
= NULL
, int lineNo
= -1);
89 A simplified version of the first constructor form, assuming a @NULL parent.
92 One of the ::wxXmlNodeType enumeration value.
94 The name of the node. This is the string which appears between angular brackets.
96 The content of the node.
97 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
99 Number of line this node was present at in input file or -1.
101 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
102 const wxString
& content
= wxEmptyString
,
108 Note that this does NOT copy siblings and parent pointer, i.e. GetParent()
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.
112 wxXmlNode(const wxXmlNode
& node
);
115 The virtual destructor. Deletes attached children and attributes.
117 virtual ~wxXmlNode();
120 Appends a attribute with given @a name and @a value to the list of
121 attributes for this node.
123 virtual void AddAttribute(const wxString
& name
, const wxString
& value
);
126 Appends given attribute to the list of attributes for this node.
128 virtual void AddAttribute(wxXmlAttribute
* attr
);
131 Adds node @a child as the last child of this node.
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.
140 @see InsertChild(), InsertChildAfter()
142 virtual void AddChild(wxXmlNode
* child
);
145 Removes the first attributes which has the given @a name from the list of
146 attributes for this node.
148 virtual bool DeleteAttribute(const wxString
& name
);
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).
154 bool GetAttribute(const wxString
& attrName
, wxString
* value
) const;
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.
160 wxString
GetAttribute(const wxString
& attrName
,
161 const wxString
& defaultVal
= wxEmptyString
) const;
164 Return a pointer to the first attribute of this node.
166 wxXmlAttribute
* GetAttributes() const;
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.
173 wxXmlNode
* GetChildren() const;
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)
178 the content is an empty string. See GetNodeContent() for more details.
180 const wxString
& GetContent() const;
183 Returns the number of nodes which separate this node from @c grandparent.
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
187 nodes or the parent of a wxXmlDocument's root element node).
189 int GetDepth(wxXmlNode
* grandparent
= NULL
) const;
192 Returns a flag indicating whether encoding conversion is necessary when saving. The default is @false.
194 You can improve saving efficiency considerably by setting this value.
196 bool GetNoConversion() const;
199 Returns line number of the node in the input XML file or @c -1 if it is unknown.
201 int GetLineNumber() const;
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).
208 const wxString
& GetName() const;
211 Returns a pointer to the sibling of this node or @NULL if there are no
214 wxXmlNode
* GetNext() const;
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:
223 wxXML_ELEMENT_NODE name="tagname", content=""
224 |-- wxXML_TEXT_NODE name="", content="tagcontent"
230 wxXML_ELEMENT_NODE name="tagname", content=""
231 |-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
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.
238 wxString
GetNodeContent() const;
241 Returns a pointer to the parent of this node or @NULL if this node has no
244 wxXmlNode
* GetParent() const;
247 Returns the type of this node.
249 wxXmlNodeType
GetType() const;
252 Returns @true if this node has a attribute named @a attrName.
254 bool HasAttribute(const wxString
& attrName
) const;
257 Inserts the @a child node immediately before @a followingNode in the
260 @return @true if @a followingNode has been found and the @a child
261 node has been inserted.
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).
269 @see AddChild(), InsertChildAfter()
271 virtual bool InsertChild(wxXmlNode
* child
, wxXmlNode
* followingNode
);
274 Inserts the @a child node immediately after @a precedingNode in the
277 @return @true if @a precedingNode has been found and the @a child
278 node has been inserted.
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.
289 @see InsertChild(), AddChild()
291 virtual bool InsertChildAfter(wxXmlNode
* child
, wxXmlNode
* precedingNode
);
294 Returns @true if the content of this node is a string containing only
295 whitespaces (spaces, tabs, new lines, etc).
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.
301 bool IsWhitespaceOnly() const;
304 Removes the given node from the children list.
306 Returns @true if the node was found and removed or @false if the node
308 Note that the caller is responsible for deleting the removed node in order
309 to avoid memory leaks.
311 virtual bool RemoveChild(wxXmlNode
* child
);
314 Sets as first attribute the given wxXmlAttribute object.
316 The caller is responsible for deleting any previously present attributes
317 attached to this node.
319 void SetAttributes(wxXmlAttribute
* attr
);
322 Sets as first child the given node.
324 The caller is responsible for deleting any previously present children node.
326 void SetChildren(wxXmlNode
* child
);
329 Sets the content of this node.
331 void SetContent(const wxString
& con
);
334 Sets the name of this node.
336 void SetName(const wxString
& name
);
339 Sets as sibling the given node.
341 The caller is responsible for deleting any previously present sibling node.
343 void SetNext(wxXmlNode
* next
);
346 Sets a flag to indicate whether encoding conversion is necessary when saving. The default is @false.
348 You can improve saving efficiency considerably by setting this value.
350 void SetNoConversion(bool noconversion
);
353 Sets as parent the given node.
355 The caller is responsible for deleting any previously present parent node.
357 void SetParent(wxXmlNode
* parent
);
360 Sets the type of this node.
362 void SetType(wxXmlNodeType type
);
365 See the copy constructor for more info.
367 wxXmlNode
& operator=(const wxXmlNode
& node
);
373 @class wxXmlAttribute
375 Represents a node attribute.
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.
383 @see wxXmlDocument, wxXmlNode
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.
397 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
398 wxXmlAttribute
* next
= NULL
);
401 The virtual destructor.
403 virtual ~wxXmlAttribute();
406 Returns the name of this attribute.
408 wxString
GetName() const;
411 Returns the sibling of this attribute or @NULL if there are no siblings.
413 wxXmlAttribute
* GetNext() const;
416 Returns the value of this attribute.
418 wxString
GetValue() const;
421 Sets the name of this attribute.
423 void SetName(const wxString
& name
);
426 Sets the sibling of this attribute.
428 void SetNext(wxXmlAttribute
* next
);
431 Sets the value of this attribute.
433 void SetValue(const wxString
& value
);
437 //* special indentation value for wxXmlDocument::Save
438 #define wxXML_NO_INDENTATION (-1)
440 //* flags for wxXmlDocument::Load
441 enum wxXmlDocumentLoadFlag
444 wxXMLDOC_KEEP_WHITESPACE_NODES
452 This class holds XML data/document as parsed by XML parser in the root node.
454 wxXmlDocument internally uses the expat library which comes with wxWidgets to
455 parse the given stream.
457 A simple example of using XML classes is:
461 if (!doc.Load("myfile.xml"))
464 // start processing the XML file
465 if (doc.GetRoot()->GetName() != "myroot-node")
469 wxXmlNode *prolog = doc.GetDocumentNode()->GetChildren();
472 if (prolog->GetType() == wxXML_PI_NODE && prolog->GetName() == "target") {
474 // process Process Instruction contents
475 wxString pi = prolog->GetContent();
482 wxXmlNode *child = doc.GetRoot()->GetChildren();
485 if (child->GetName() == "tag1") {
487 // process text enclosed by tag1/tag1
488 wxString content = child->GetNodeContent();
492 // process attributes of tag1
493 wxString attrvalue1 =
494 child->GetAttribute("attr1", "default-value");
495 wxString attrvalue2 =
496 child->GetAttribute("attr2", "default-value");
500 } else if (child->GetName() == "tag2") {
505 child = child->GetNext();
509 Note that if you want to preserve the original formatting of the loaded file
510 including whitespaces and indentation, you need to turn off whitespace-only
511 textnode removal and automatic indentation:
515 doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
517 // myfile2.xml will be identical to myfile.xml saving it this way:
518 doc.Save("myfile2.xml", wxXML_NO_INDENTATION);
521 Using default parameters, you will get a reformatted document which in general
522 is different from the original loaded content:
526 doc.Load("myfile.xml");
527 doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml
533 @see wxXmlNode, wxXmlAttribute
535 class wxXmlDocument
: public wxObject
544 Copy constructor. Deep copies all the XML tree of the given document.
546 wxXmlDocument(const wxXmlDocument
& doc
);
549 Loads the given filename using the given encoding. See Load().
551 wxXmlDocument(const wxString
& filename
,
552 const wxString
& encoding
= "UTF-8"));
555 Loads the XML document from given stream using the given encoding. See Load().
557 wxXmlDocument(wxInputStream
& stream
,
558 const wxString
& encoding
= "UTF-8");
561 Virtual destructor. Frees the document root node.
563 virtual ~wxXmlDocument();
566 Appends a Process Instruction or Comment node to the document prologue.
568 Calling this function will create a prologue or attach the node to the
569 end of an existing prologue.
573 void AppendToProlog(wxXmlNode
* node
);
576 Detaches the document node and returns it.
578 The document node will be set to @NULL and thus IsOk() will
579 return @false after calling this function.
581 Note that the caller is responsible for deleting the returned node in order
582 to avoid memory leaks.
586 wxXmlNode
* DetachDocumentNode();
589 Detaches the root entity node and returns it.
591 After calling this function, the document node will remain together with
592 any prologue nodes, but IsOk() will return @false since the root entity
595 Note that the caller is responsible for deleting the returned node in order
596 to avoid memory leaks.
598 wxXmlNode
* DetachRoot();
601 Returns encoding of in-memory representation of the document
602 (same as passed to Load() or constructor, defaults to UTF-8).
604 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
606 wxString
GetEncoding() const;
609 Returns encoding of document (may be empty).
611 @note This is the encoding original file was saved in, @b not the
612 encoding of in-memory representation!
614 const wxString
& GetFileEncoding() const;
617 Returns the document node of the document.
621 wxXmlNode
* GetDocumentNode() const;
624 Returns the root element node of the document.
626 wxXmlNode
* GetRoot() const;
629 Returns the version of document.
631 This is the value in the @c \<?xml version="1.0"?\> header of the XML document.
632 If the version attribute was not explicitly given in the header, this function
633 returns an empty string.
635 const wxString
& GetVersion() const;
638 Returns @true if the document has been loaded successfully.
644 Parses @a filename as an xml document and loads its data.
646 If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading,
647 all nodes of type @c wxXML_TEXT_NODE (see wxXmlNode) are automatically skipped
648 if they contain whitespaces only.
650 The removal of these nodes makes the load process slightly faster and requires
651 less memory however makes impossible to recreate exactly the loaded text with a
652 Save() call later. Read the initial description of this class for more info.
654 Returns true on success, false otherwise.
656 virtual bool Load(const wxString
& filename
,
657 const wxString
& encoding
= "UTF-8", int flags
= wxXMLDOC_NONE
);
660 Like Load(const wxString&, const wxString&, int) but takes the data from
663 virtual bool Load(wxInputStream
& stream
,
664 const wxString
& encoding
= "UTF-8", int flags
= wxXMLDOC_NONE
);
667 Saves XML tree creating a file named with given string.
669 If @a indentstep is greater than or equal to zero, then, while saving,
670 an automatic indentation is added with steps composed by indentstep spaces.
672 If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation
675 virtual bool Save(const wxString
& filename
, int indentstep
= 2) const;
678 Saves XML tree in the given output stream.
679 See Save(const wxString&, int) for a description of @a indentstep.
681 virtual bool Save(wxOutputStream
& stream
, int indentstep
= 2) const;
684 Sets the document node of this document.
686 Deletes any previous document node.
687 Use DetachDocumentNode() and then SetDocumentNode() if you want to
688 replace the document node without deleting the old document tree.
692 void SetDocumentNode(wxXmlNode
* node
);
695 Sets the encoding of the document.
697 void SetEncoding(const wxString
& enc
);
700 Sets the enconding of the file which will be used to save the document.
702 void SetFileEncoding(const wxString
& encoding
);
705 Sets the root element node of this document.
707 Will create the document node if necessary. Any previous
708 root element node is deleted.
710 void SetRoot(wxXmlNode
* node
);
713 Sets the version of the XML file which will be used to save the document.
715 void SetVersion(const wxString
& version
);
718 Deep copies the given document.
720 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
723 Get expat library version information.
728 static wxVersionInfo
GetLibraryVersionInfo();