]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/xml/xml.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument
4 // Author: wxWidgets team
6 // Licence: wxWindows license
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 (e.g. in @c \<title\>hi\</title\> there is an element
38 with name="title", irrelevant content and one child @c wxXML_TEXT_NODE
41 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
42 wxXmlDocument::Load (default is UTF-8).
47 @see wxXmlDocument, wxXmlAttribute
53 Creates this XML node and eventually insert it into an existing XML tree.
56 The parent node to which append this node instance.
57 If this argument is @NULL this new node will be floating and it can
58 be appended later to another one using the AddChild() or InsertChild()
59 functions. Otherwise the child is already added to the XML tree by
60 this constructor and it shouldn't be done again.
62 One of the ::wxXmlNodeType enumeration value.
64 The name of the node. This is the string which appears between angular brackets.
66 The content of the node.
67 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
69 If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
71 If not @NULL, this node and its eventual siblings are attached to the node.
73 Number of line this node was present at in input file or -1.
75 wxXmlNode(wxXmlNode
* parent
, wxXmlNodeType type
,
77 const wxString
& content
= wxEmptyString
,
78 wxXmlAttribute
* attrs
= NULL
,
79 wxXmlNode
* next
= NULL
, int lineNo
= -1);
82 A simplified version of the first constructor form, assuming a @NULL parent.
85 One of the ::wxXmlNodeType enumeration value.
87 The name of the node. This is the string which appears between angular brackets.
89 The content of the node.
90 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
92 Number of line this node was present at in input file or -1.
94 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
95 const wxString
& content
= wxEmptyString
,
101 Note that this does NOT copy syblings and parent pointer, i.e. GetParent()
102 and GetNext() will return @NULL after using copy ctor and are never unmodified by operator=().
103 On the other hand, it DOES copy children and attributes.
105 wxXmlNode(const wxXmlNode
& node
);
108 The virtual destructor. Deletes attached children and attributes.
110 virtual ~wxXmlNode();
113 Appends a attribute with given @a name and @a value to the list of
114 attributes for this node.
116 virtual void AddAttribute(const wxString
& name
, const wxString
& value
);
119 Appends given attribute to the list of attributes for this node.
121 virtual void AddAttribute(wxXmlAttribute
* attr
);
124 Adds node @a child as the last child of this node.
127 Note that this function works in O(n) time where @e n is the number
128 of existing children. Consequently, adding large number of child
129 nodes using this method can be expensive, because it has O(n^2) time
130 complexity in number of nodes to be added. Use InsertChildAfter() to
131 populate XML tree in linear time.
133 @see InsertChild(), InsertChildAfter()
135 virtual void AddChild(wxXmlNode
* child
);
138 Removes the first attributes which has the given @a name from the list of
139 attributes for this node.
141 virtual bool DeleteAttribute(const wxString
& name
);
144 Returns true if a attribute named attrName could be found.
145 The value of that attribute is saved in value (which must not be @NULL).
147 bool GetAttribute(const wxString
& attrName
, wxString
* value
) const;
150 Returns the value of the attribute named @a attrName if it does exist.
151 If it does not exist, the @a defaultVal is returned.
153 wxString
GetAttribute(const wxString
& attrName
,
154 const wxString
& defaultVal
= wxEmptyString
) const;
157 Return a pointer to the first attribute of this node.
159 wxXmlAttribute
* GetAttributes() const;
162 Returns the first child of this node.
163 To get a pointer to the second child of this node (if it does exist), use the
164 GetNext() function on the returned value.
166 wxXmlNode
* GetChildren() const;
169 Returns the content of this node. Can be an empty string.
170 Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
171 the content is an empty string. See GetNodeContent() for more details.
173 const wxString
& GetContent() const;
176 Returns the number of nodes which separe this node from @c grandparent.
178 This function searches only the parents of this node until it finds
179 @a grandparent or the @NULL node (which is the parent of non-linked
180 nodes or the parent of a wxXmlDocument's root node).
182 int GetDepth(wxXmlNode
* grandparent
= NULL
) const;
185 Returns line number of the node in the input XML file or @c -1 if it is unknown.
187 int GetLineNumber() const;
190 Returns the name of this node.
191 Can be an empty string (e.g. for nodes of type @c wxXML_TEXT_NODE or
192 @c wxXML_CDATA_SECTION_NODE).
194 const wxString
& GetName() const;
197 Returns a pointer to the sibling of this node or @NULL if there are no
200 wxXmlNode
* GetNext() const;
203 Returns the content of the first child node of type @c wxXML_TEXT_NODE
204 or @c wxXML_CDATA_SECTION_NODE.
205 This function is very useful since the XML snippet @c "tagnametagcontent/tagname"
206 is represented by expat with the following tag tree:
209 wxXML_ENTITY_NODE name="tagname", content=""
210 |-- wxXML_TEXT_NODE name="", content="tagcontent"
216 wxXML_ENTITY_NODE name="tagname", content=""
217 |-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
220 An empty string is returned if the node has no children of type
221 @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content
222 of the first child of such types is empty.
224 wxString
GetNodeContent() const;
227 Returns a pointer to the parent of this node or @NULL if this node has no
230 wxXmlNode
* GetParent() const;
233 Returns the type of this node.
235 wxXmlNodeType
GetType() const;
238 Returns @true if this node has a attribute named @a attrName.
240 bool HasAttribute(const wxString
& attrName
) const;
243 Inserts the @a child node immediately before @a followingNode in the
246 @return @true if @a followingNode has been found and the @a child
247 node has been inserted.
250 For historical reasons, @a followingNode may be @NULL. In that case,
251 then @a child is prepended to the list of children and becomes the
252 first child of this node, i.e. it behaves identically to using the
253 first children (as returned by GetChildren()) for @a followingNode).
255 @see AddChild(), InsertChildAfter()
257 virtual bool InsertChild(wxXmlNode
* child
, wxXmlNode
* followingNode
);
260 Inserts the @a child node immediately after @a precedingNode in the
263 @return @true if @a precedingNode has been found and the @a child
264 node has been inserted.
269 The node to insert @a child after. As a special case, this can be
270 @NULL if this node has no children yet -- in that case, @a child
271 will become this node's only child node.
275 @see InsertChild(), AddChild()
277 virtual bool InsertChildAfter(wxXmlNode
* child
, wxXmlNode
* precedingNode
);
280 Returns @true if the content of this node is a string containing only
281 whitespaces (spaces, tabs, new lines, etc).
283 Note that this function is locale-independent since the parsing of XML
284 documents must always produce the exact same tree regardless of the
285 locale it runs under.
287 bool IsWhitespaceOnly() const;
290 Removes the given node from the children list.
292 Returns @true if the node was found and removed or @false if the node
294 Note that the caller is reponsible for deleting the removed node in order
295 to avoid memory leaks.
297 virtual bool RemoveChild(wxXmlNode
* child
);
300 Sets as first attribute the given wxXmlAttribute object.
302 The caller is responsible to delete any previously present attributes
303 attached to this node.
305 void SetAttributes(wxXmlAttribute
* attr
);
308 Sets as first child the given node.
310 The caller is responsible to delete any previously present children node.
312 void SetChildren(wxXmlNode
* child
);
315 Sets the content of this node.
317 void SetContent(const wxString
& con
);
320 Sets the name of this node.
322 void SetName(const wxString
& name
);
325 Sets as sibling the given node.
327 The caller is responsible to delete any previously present sibling node.
329 void SetNext(wxXmlNode
* next
);
332 Sets as parent the given node.
334 The caller is responsible to delete any previously present parent node.
336 void SetParent(wxXmlNode
* parent
);
339 Sets the type of this node.
341 void SetType(wxXmlNodeType type
);
344 See the copy constructor for more info.
346 wxXmlNode
& operator=(const wxXmlNode
& node
);
352 @class wxXmlAttribute
354 Represents a node attribute.
356 Example: in @c "\<img src="hello.gif" id="3"/\>", @c "src" is attribute with value
357 @c "hello.gif" and @c "id" is a attribute with value @c "3".
362 @see wxXmlDocument, wxXmlNode
373 Creates the attribute with given @a name and @a value.
374 If @a next is not @NULL, then sets it as sibling of this attribute.
376 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
377 wxXmlAttribute
* next
= NULL
);
380 The virtual destructor.
382 virtual ~wxXmlAttribute();
385 Returns the name of this attribute.
387 wxString
GetName() const;
390 Returns the sibling of this attribute or @NULL if there are no siblings.
392 wxXmlAttribute
* GetNext() const;
395 Returns the value of this attribute.
397 wxString
GetValue() const;
400 Sets the name of this attribute.
402 void SetName(const wxString
& name
);
405 Sets the sibling of this attribute.
407 void SetNext(wxXmlAttribute
* next
);
410 Sets the value of this attribute.
412 void SetValue(const wxString
& value
);
420 This class holds XML data/document as parsed by XML parser in the root node.
422 wxXmlDocument internally uses the expat library which comes with wxWidgets to
423 parse the given stream.
425 A simple example of using XML classes is:
429 if (!doc.Load(wxT("myfile.xml")))
432 // start processing the XML file
433 if (doc.GetRoot()-GetName() != wxT("myroot-node"))
436 wxXmlNode *child = doc.GetRoot()-GetChildren();
439 if (child-GetName() == wxT("tag1")) {
441 // process text enclosed by tag1/tag1
442 wxString content = child-GetNodeContent();
446 // process attributes of tag1
447 wxString attrvalue1 =
448 child-GetAttribute(wxT("attr1"),
449 wxT("default-value"));
450 wxString attrvalue2 =
451 child-GetAttribute(wxT("attr2"),
452 wxT("default-value"));
456 } else if (child-GetName() == wxT("tag2")) {
461 child = child-GetNext();
465 Note that if you want to preserve the original formatting of the loaded file
466 including whitespaces and indentation, you need to turn off whitespace-only
467 textnode removal and automatic indentation:
471 doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES);
473 // myfile2.xml will be indentic to myfile.xml saving it this way:
474 doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION);
477 Using default parameters, you will get a reformatted document which in general
478 is different from the original loaded content:
482 doc.Load(wxT("myfile.xml"));
483 doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml
489 @see wxXmlNode, wxXmlAttribute
491 class wxXmlDocument
: public wxObject
500 Copy constructor. Deep copies all the XML tree of the given document.
502 wxXmlDocument(const wxXmlDocument
& doc
);
505 Loads the given filename using the given encoding. See Load().
507 wxXmlDocument(const wxString
& filename
,
508 const wxString
& encoding
= wxT("UTF-8"));
511 Loads the XML document from given stream using the given encoding. See Load().
513 wxXmlDocument(wxInputStream
& stream
,
514 const wxString
& encoding
= wxT("UTF-8"));
517 Virtual destructor. Frees the document root node.
519 virtual ~wxXmlDocument();
522 Detaches the document root node and returns it.
524 The document root node will be set to @NULL and thus IsOk() will
525 return @false after calling this function.
527 Note that the caller is reponsible for deleting the returned node in order
528 to avoid memory leaks.
530 wxXmlNode
* DetachRoot();
533 Returns encoding of in-memory representation of the document
534 (same as passed to Load() or constructor, defaults to UTF-8).
536 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
538 wxString
GetEncoding() const;
541 Returns encoding of document (may be empty).
543 @note This is the encoding original file was saved in, @b not the
544 encoding of in-memory representation!
546 const wxString
& GetFileEncoding() const;
549 Returns the root node of the document.
551 wxXmlNode
* GetRoot() const;
554 Returns the version of document.
556 This is the value in the @c \<?xml version="1.0"?\> header of the XML document.
557 If the version attribute was not explicitely given in the header, this function
558 returns an empty string.
560 const wxString
& GetVersion() const;
563 Returns @true if the document has been loaded successfully.
569 Parses @a filename as an xml document and loads its data.
571 If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading,
572 all nodes of type @c wxXML_TEXT_NODE (see wxXmlNode) are automatically skipped
573 if they contain whitespaces only.
575 The removal of these nodes makes the load process slightly faster and requires
576 less memory however makes impossible to recreate exactly the loaded text with a
577 Save() call later. Read the initial description of this class for more info.
579 Returns true on success, false otherwise.
581 virtual bool Load(const wxString
& filename
,
582 const wxString
& encoding
= wxT("UTF-8"), int flags
= wxXMLDOC_NONE
);
585 Like Load(const wxString&, const wxString&, int) but takes the data from
588 virtual bool Load(wxInputStream
& stream
,
589 const wxString
& encoding
= wxT("UTF-8"), int flags
= wxXMLDOC_NONE
);
592 Saves XML tree creating a file named with given string.
594 If @a indentstep is greater than or equal to zero, then, while saving,
595 an automatic indentation is added with steps composed by indentstep spaces.
597 If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation
600 virtual bool Save(const wxString
& filename
, int indentstep
= 1) const;
603 Saves XML tree in the given output stream.
604 See Save(const wxString&, int) for a description of @a indentstep.
606 virtual bool Save(wxOutputStream
& stream
, int indentstep
= 1) const;
609 Sets the enconding of the document.
611 void SetEncoding(const wxString
& enc
);
614 Sets the enconding of the file which will be used to save the document.
616 void SetFileEncoding(const wxString
& encoding
);
619 Sets the root node of this document. Deletes previous root node.
620 Use DetachRoot() and then SetRoot() if you want to replace the root
621 node without deleting the old document tree.
623 void SetRoot(wxXmlNode
* node
);
626 Sets the version of the XML file which will be used to save the document.
628 void SetVersion(const wxString
& version
);
631 Deep copies the given document.
633 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);