]>
git.saurik.com Git - wxWidgets.git/blob - interface/xml/xml.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxXmlNode class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @headerfile xml.h wx/xml/xml.h
13 Represents a node in an XML document. See wxXmlDocument.
15 Node has a name and may have content and attributes. Most common node types are
16 @c wxXML_TEXT_NODE (name and attributes are irrelevant) and
17 @c wxXML_ELEMENT_NODE (e.g. in @c titlehi/title there is an element
18 with name="title", irrelevant content and one child (@c wxXML_TEXT_NODE
21 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
22 wxXmlDocument::Load (default is UTF-8).
28 wxXmlDocument, wxXmlAttribute
35 A simplified version of the first constructor form, assuming a @NULL parent.
37 wxXmlNode(wxXmlNode
* parent
, wxXmlNodeType type
,
39 const wxString
& content
= wxEmptyString
,
40 wxXmlAttribute
* attrs
= @NULL
,
41 wxXmlNode
* next
= @NULL
, int lineNo
= -1);
42 wxXmlNode(const wxXmlNode
& node
);
43 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
44 const wxString
& content
= wxEmptyString
,
49 The virtual destructor. Deletes attached children and attributes.
55 Appends given attribute to the list of attributes for this node.
57 void AddAttribute(const wxString
& name
, const wxString
& value
);
58 void AddAttribute(wxXmlAttribute
* attr
);
62 Adds the given node as child of this node. To attach a second children to this
64 SetNext() function of the @e child node.
66 void AddChild(wxXmlNode
* child
);
69 Removes the first attributes which has the given @e name from the list of
70 attributes for this node.
72 bool DeleteAttribute(const wxString
& name
);
76 Returns the value of the attribute named @e attrName if it does exist.
77 If it does not exist, the @e defaultVal is returned.
79 bool GetAttribute(const wxString
& attrName
, wxString
* value
);
80 wxString
GetAttribute(const wxString
& attrName
,
81 const wxString
& defaultVal
);
85 Return a pointer to the first attribute of this node.
87 wxXmlAttribute
* GetAttributes();
90 Returns the first child of this node.
91 To get a pointer to the second child of this node (if it does exist), use the
92 GetNext() function on the returned value.
94 wxXmlNode
* GetChildren();
97 Returns the content of this node. Can be an empty string.
98 Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
100 content is an empty string. See GetNodeContent() for more details.
102 wxString
GetContent();
105 Returns the number of nodes which separe this node from @c grandparent.
107 This function searches only the parents of this node until it finds @c
109 or the @NULL node (which is the parent of non-linked nodes or the parent of a
110 wxXmlDocument's root node).
112 int GetDepth(wxXmlNode
* grandparent
= @NULL
);
115 Returns line number of the node in the input XML file or -1 if it is unknown.
120 Returns the name of this node. Can be an empty string (e.g. for nodes of type
121 @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE).
126 Returns a pointer to the sibling of this node or @NULL if there are no
129 wxXmlNode
* GetNext();
132 Returns the content of the first child node of type @c wxXML_TEXT_NODE or @c
133 wxXML_CDATA_SECTION_NODE.
134 This function is very useful since the XML snippet @c
135 "tagnametagcontent/tagname" is represented by
136 expat with the following tag tree:
138 An empty string is returned if the node has no children of type @c
139 wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content of the first child of such types is empty.
141 wxString
GetNodeContent();
144 Returns a pointer to the parent of this node or @NULL if this node has no
147 wxXmlNode
* GetParent();
150 Returns the type of this node.
152 wxXmlNodeType
GetType();
155 Returns @true if this node has a attribute named @e attrName.
157 bool HasAttribute(const wxString
& attrName
);
160 Inserts the @e child node after @e before_node in the children list.
161 If @e before_node is @NULL, then @e child is prepended to the list of
163 becomes the first child of this node.
164 Returns @true if @e before_node has been found and the @e child node has been
167 bool InsertChild(wxXmlNode
* child
, wxXmlNode
* before_node
);
170 Returns @true if the content of this node is a string containing only
172 tabs, new lines, etc). Note that this function is locale-independent since the
174 documents must always produce the exact same tree regardless of the locale it
177 bool IsWhitespaceOnly();
180 Removes the given node from the children list. Returns @true if the node was
182 or @false if the node could not be found.
184 Note that the caller is reponsible for deleting the removed node in order to
187 bool RemoveChild(wxXmlNode
* child
);
190 Sets as first attribute the given wxXmlAttribute object.
191 The caller is responsible to delete any previously present attributes attached
194 void SetAttributes(wxXmlAttribute
* attr
);
197 Sets as first child the given node. The caller is responsible to delete any
201 void SetChildren(wxXmlNode
* child
);
204 Sets the content of this node.
206 void SetContent(const wxString
& con
);
209 Sets the name of this node.
211 void SetName(const wxString
& name
);
214 Sets as sibling the given node. The caller is responsible to delete any
218 void SetNext(wxXmlNode
* next
);
221 Sets as parent the given node. The caller is responsible to delete any
225 void SetParent(wxXmlNode
* parent
);
228 Sets the type of this node.
230 void SetType(wxXmlNodeType type
);
233 See the copy constructor for more info.
235 wxXmlNode
operator=(const wxXmlNode
& node
);
240 @class wxXmlAttribute
241 @headerfile xml.h wx/xml/xml.h
243 Represents a node attribute.
245 Example: in @c img src="hello.gif" id="3"/, @c "src" is attribute with value
246 @c "hello.gif" and @c "id" is a attribute with value @c "3".
252 wxXmlDocument, wxXmlNode
259 Creates the attribute with given @e name and @e value.
260 If @e next is not @NULL, then sets it as sibling of this attribute.
263 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
264 wxXmlAttribute
* next
= @NULL
);
268 The virtual destructor.
273 Returns the name of this attribute.
278 Returns the sibling of this attribute or @NULL if there are no siblings.
280 wxXmlAttribute
* GetNext();
283 Returns the value of this attribute.
288 Sets the name of this attribute.
290 void SetName(const wxString
& name
);
293 Sets the sibling of this attribute.
295 void SetNext(wxXmlAttribute
* next
);
298 Sets the value of this attribute.
300 void SetValue(const wxString
& value
);
306 @headerfile xml.h wx/xml/xml.h
308 This class holds XML data/document as parsed by XML parser in the root node.
310 wxXmlDocument internally uses the expat library which comes with wxWidgets to
311 parse the given stream.
313 A simple example of using XML classes is:
317 if (!doc.Load(wxT("myfile.xml")))
320 // start processing the XML file
321 if (doc.GetRoot()-GetName() != wxT("myroot-node"))
324 wxXmlNode *child = doc.GetRoot()-GetChildren();
327 if (child-GetName() == wxT("tag1")) {
329 // process text enclosed by tag1/tag1
330 wxString content = child-GetNodeContent();
334 // process attributes of tag1
335 wxString attrvalue1 =
336 child-GetAttribute(wxT("attr1"),
337 wxT("default-value"));
338 wxString attrvalue2 =
339 child-GetAttribute(wxT("attr2"),
340 wxT("default-value"));
344 } else if (child-GetName() == wxT("tag2")) {
349 child = child-GetNext();
353 @b Note: if you want to preserve the original formatting of the loaded file
354 including whitespaces
355 and indentation, you need to turn off whitespace-only textnode removal and
356 automatic indentation:
360 doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES);
362 // myfile2.xml will be indentic to myfile.xml saving it this way:
363 doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION);
366 Using default parameters, you will get a reformatted document which in general
368 the original loaded content:
372 doc.Load(wxT("myfile.xml"));
373 doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml
380 wxXmlNode, wxXmlAttribute
382 class wxXmlDocument
: public wxObject
387 Copy constructor. Deep copies all the XML tree of the given document.
390 wxXmlDocument(const wxString
& filename
);
391 wxXmlDocument(wxInputStream
& stream
);
392 wxXmlDocument(const wxXmlDocument
& doc
);
396 Virtual destructor. Frees the document root node.
401 Detaches the document root node and returns it. The document root node will be
403 and thus IsOk() will return @false after calling this function.
405 Note that the caller is reponsible for deleting the returned node in order to
408 wxXmlNode
* DetachRoot();
411 Returns encoding of in-memory representation of the document
412 (same as passed to Load() or constructor, defaults to UTF-8).
414 NB: this is meaningless in Unicode build where data are stored as @c wchar_t*.
416 wxString
GetEncoding();
419 Returns encoding of document (may be empty).
421 Note: this is the encoding original file was saved in, @b not the
422 encoding of in-memory representation!
424 wxString
GetFileEncoding();
427 Returns the root node of the document.
429 wxXmlNode
* GetRoot();
432 Returns the version of document.
433 This is the value in the @c ?xml version="1.0"? header of the XML document.
434 If the version attribute was not explicitely given in the header, this function
435 returns an empty string.
437 wxString
GetVersion();
440 Returns @true if the document has been loaded successfully.
442 #define bool IsOk() /* implementation is private */
446 , @b int@e flags = wxXMLDOC_NONE)
448 Like above but takes the data from given input stream.
450 bool Load(const wxString
& filename
);
451 int bool Load(wxInputStream
& stream
);
456 Saves XML tree in the given output stream. See other overload for a description
459 bool Save(const wxString
& filename
, int indentstep
= 1);
460 bool Save(wxOutputStream
& stream
, int indentstep
= 1);
464 Sets the enconding of the document.
466 void SetEncoding(const wxString
& enc
);
469 Sets the enconding of the file which will be used to save the document.
471 void SetFileEncoding(const wxString
& encoding
);
474 Sets the root node of this document. Deletes previous root node.
475 Use DetachRoot() and then
476 SetRoot() if you want
477 to replace the root node without deleting the old document tree.
479 void SetRoot(wxXmlNode
* node
);
482 Sets the version of the XML file which will be used to save the document.
484 void SetVersion(const wxString
& version
);
487 Deep copies the given document.
489 wxXmlDocument
& operator operator=(const wxXmlDocument
& doc
);