]>
Commit | Line | Data |
---|---|---|
434cf5a4 RR |
1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2 | %% Name: xmlnode.tex | |
3 | %% Purpose: wxXmlDocument documentation | |
4 | %% Author: Francesco Montorsi | |
5 | %% Created: 2006-04-18 | |
6 | %% RCS-ID: $Id$ | |
7 | %% Copyright: (c) 2006 Francesco Montorsi | |
8 | %% License: wxWindows license | |
9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
10 | ||
4c43dd90 JS |
11 | \section{\class{wxXmlDocument}}\label{wxxmldocument} |
12 | ||
13 | This class holds XML data/document as parsed by XML parser in the root node. | |
14 | ||
15 | wxXmlDocument internally uses the expat library which comes with wxWidgets to parse the given stream. | |
16 | ||
434cf5a4 RR |
17 | A simple example of using XML classes is: |
18 | ||
19 | \begin{verbatim} | |
20 | wxXmlDocument doc; | |
538f3830 | 21 | if (!doc.Load(wxT("myfile.xml"))) |
434cf5a4 RR |
22 | return false; |
23 | ||
24 | // start processing the XML file | |
25 | if (doc.GetRoot()->GetName() != wxT("myroot-node")) | |
26 | return false; | |
27 | ||
28 | wxXmlNode *child = doc.GetRoot()->GetChildren(); | |
29 | while (child) { | |
30 | ||
31 | if (child->GetName() == wxT("tag1")) { | |
32 | ||
33 | // process text enclosed by <tag1></tag1> | |
34 | wxString content = child->GetNodeContent(); | |
35 | ||
36 | ... | |
37 | ||
38 | ||
288b6107 VS |
39 | // process attributes of <tag1> |
40 | wxString attrvalue1 = | |
41 | child->GetAttribute(wxT("attr1"), | |
e8da6b7c | 42 | wxT("default-value")); |
288b6107 VS |
43 | wxString attrvalue2 = |
44 | child->GetAttribute(wxT("attr2"), | |
e8da6b7c | 45 | wxT("default-value")); |
434cf5a4 RR |
46 | |
47 | ... | |
48 | ||
49 | } else if (child->GetName() == wxT("tag2")) { | |
50 | ||
51 | // process tag2 ... | |
52 | } | |
53 | ||
54 | child = child->GetNext(); | |
55 | } | |
56 | \end{verbatim} | |
57 | ||
538f3830 VS |
58 | {\bf Note:} if you want to preserve the original formatting of the loaded file including whitespaces |
59 | and indentation, you need to turn off whitespace-only textnode removal and automatic indentation: | |
60 | ||
61 | \begin{verbatim} | |
62 | wxXmlDocument doc; | |
63 | doc.Load(wxT("myfile.xml"), wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES); | |
e8da6b7c RR |
64 | |
65 | // myfile2.xml will be indentic to myfile.xml saving it this way: | |
66 | doc.Save(wxT("myfile2.xml"), wxXML_NO_INDENTATION); | |
538f3830 VS |
67 | \end{verbatim} |
68 | ||
69 | Using default parameters, you will get a reformatted document which in general is different from | |
70 | the original loaded content: | |
71 | ||
72 | \begin{verbatim} | |
73 | wxXmlDocument doc; | |
74 | doc.Load(wxT("myfile.xml")); | |
e8da6b7c | 75 | doc.Save(wxT("myfile2.xml")); // myfile2.xml != myfile.xml |
538f3830 | 76 | \end{verbatim} |
434cf5a4 RR |
77 | |
78 | ||
4c43dd90 JS |
79 | \wxheading{Derived from} |
80 | ||
81 | \helpref{wxObject}{wxobject} | |
82 | ||
83 | \wxheading{Include files} | |
84 | ||
85 | <wx/xml/xml.h> | |
86 | ||
87 | \wxheading{See also} | |
88 | ||
288b6107 | 89 | \helpref{wxXmlNode}{wxxmlnode}, \helpref{wxXmlAttribute}{wxxmlattribute} |
4c43dd90 JS |
90 | |
91 | ||
92 | \latexignore{\rtfignore{\wxheading{Members}}} | |
93 | ||
94 | ||
74ecac1f | 95 | |
4c43dd90 JS |
96 | \membersection{wxXmlDocument::wxXmlDocument}\label{wxxmldocumentwxxmldocument} |
97 | ||
98 | \func{}{wxXmlDocument}{\void} | |
99 | ||
100 | ||
418ab1e7 | 101 | \func{}{wxXmlDocument}{\param{const wxString\& }{filename}, \param{const wxString\& }{encoding = wxT("UTF-8")}, \param{int }{flags = wxXMLDOC\_NONE}} |
4c43dd90 | 102 | |
e8da6b7c | 103 | Loads the given {\it filename} using the given encoding. See \helpref{Load}{wxxmldocumentload}. |
4c43dd90 | 104 | |
418ab1e7 | 105 | \func{}{wxXmlDocument}{\param{wxInputStream\& }{stream}, \param{const wxString\& }{encoding = wxT("UTF-8")}, \param{int }{flags = wxXMLDOC\_NONE}} |
4c43dd90 | 106 | |
e8da6b7c | 107 | Loads the XML document from given stream using the given encoding. See \helpref{Load}{wxxmldocumentload}. |
4c43dd90 JS |
108 | |
109 | \func{}{wxXmlDocument}{\param{const wxXmlDocument\& }{doc}} | |
110 | ||
e8da6b7c | 111 | Copy constructor. Deep copies all the XML tree of the given document. |
4c43dd90 | 112 | |
74ecac1f | 113 | |
4c43dd90 JS |
114 | \membersection{wxXmlDocument::\destruct{wxXmlDocument}}\label{wxxmldocumentdtor} |
115 | ||
116 | \func{}{\destruct{wxXmlDocument}}{\void} | |
117 | ||
118 | Virtual destructor. Frees the document root node. | |
119 | ||
a124f99a | 120 | |
74ecac1f | 121 | |
a124f99a RR |
122 | \membersection{wxXmlDocument::DetachRoot}\label{wxxmldocumentdetachroot} |
123 | ||
124 | \func{wxXmlNode*}{DetachRoot}{\void} | |
125 | ||
126 | Detaches the document root node and returns it. The document root node will be set to \NULL | |
127 | and thus \helpref{IsOk}{wxxmldocumentisok} will return \false after calling this function. | |
128 | ||
129 | Note that the caller is reponsible for deleting the returned node in order to avoid memory leaks. | |
130 | ||
131 | ||
74ecac1f | 132 | |
4c43dd90 JS |
133 | \membersection{wxXmlDocument::GetEncoding}\label{wxxmldocumentgetencoding} |
134 | ||
135 | \constfunc{wxString}{GetEncoding}{\void} | |
136 | ||
137 | Returns encoding of in-memory representation of the document | |
e8da6b7c | 138 | (same as passed to \helpref{Load}{wxxmldocumentload} or constructor, defaults to UTF-8). |
4c43dd90 | 139 | |
e8da6b7c | 140 | NB: this is meaningless in Unicode build where data are stored as {\tt wchar\_t*}. |
4c43dd90 JS |
141 | |
142 | ||
74ecac1f | 143 | |
4c43dd90 JS |
144 | \membersection{wxXmlDocument::GetFileEncoding}\label{wxxmldocumentgetfileencoding} |
145 | ||
146 | \constfunc{wxString}{GetFileEncoding}{\void} | |
147 | ||
148 | Returns encoding of document (may be empty). | |
149 | ||
e8da6b7c | 150 | Note: this is the encoding original file was saved in, {\bf not} the |
4c43dd90 JS |
151 | encoding of in-memory representation! |
152 | ||
153 | ||
74ecac1f | 154 | |
4c43dd90 JS |
155 | \membersection{wxXmlDocument::GetRoot}\label{wxxmldocumentgetroot} |
156 | ||
157 | \constfunc{wxXmlNode*}{GetRoot}{\void} | |
158 | ||
159 | Returns the root node of the document. | |
160 | ||
161 | ||
74ecac1f | 162 | |
4c43dd90 JS |
163 | \membersection{wxXmlDocument::GetVersion}\label{wxxmldocumentgetversion} |
164 | ||
165 | \constfunc{wxString}{GetVersion}{\void} | |
166 | ||
167 | Returns the version of document. | |
168 | This is the value in the {\tt <?xml version="1.0"?>} header of the XML document. | |
288b6107 | 169 | If the version attribute was not explicitely given in the header, this function |
4c43dd90 JS |
170 | returns an empty string. |
171 | ||
172 | ||
74ecac1f | 173 | |
4c43dd90 JS |
174 | \membersection{wxXmlDocument::IsOk}\label{wxxmldocumentisok} |
175 | ||
176 | \constfunc{bool}{IsOk}{\void} | |
177 | ||
178 | Returns \true if the document has been loaded successfully. | |
179 | ||
180 | ||
74ecac1f | 181 | |
4c43dd90 JS |
182 | \membersection{wxXmlDocument::Load}\label{wxxmldocumentload} |
183 | ||
418ab1e7 | 184 | \func{bool}{Load}{\param{const wxString\& }{filename}, \param{const wxString\& }{encoding = wxT("UTF-8")}, \param{int }{flags = wxXMLDOC\_NONE}} |
4c43dd90 | 185 | |
538f3830 | 186 | Parses {\it filename} as an xml document and loads its data. |
4c43dd90 | 187 | |
418ab1e7 VZ |
188 | If {\tt flags} does not contain {\tt wxXMLDOC\_KEEP\_WHITESPACE\_NODES}, then, while loading, all nodes of |
189 | type {\tt wxXML\_TEXT\_NODE} (see \helpref{wxXmlNode}{wxxmlnode}) are automatically skipped if they | |
538f3830 VS |
190 | contain whitespaces only. |
191 | The removal of these nodes makes the load process slightly faster and requires less memory however | |
192 | makes impossible to recreate exactly the loaded text with a \helpref{Save}{wxxmldocumentsave} call later. | |
193 | Read the initial description of this class for more info. | |
194 | ||
195 | Returns \true on success, \false otherwise. | |
196 | ||
418ab1e7 | 197 | \func{bool}{Load}{\param{wxInputStream\& }{stream}, \param{const wxString\& }{encoding = wxT("UTF-8")}, \param{int }{flags = wxXMLDOC\_NONE}} |
4c43dd90 JS |
198 | |
199 | Like above but takes the data from given input stream. | |
200 | ||
74ecac1f | 201 | |
4c43dd90 JS |
202 | \membersection{wxXmlDocument::Save}\label{wxxmldocumentsave} |
203 | ||
538f3830 | 204 | \constfunc{bool}{Save}{\param{const wxString\& }{filename}, \param{int }{indentstep = 1}} |
4c43dd90 JS |
205 | |
206 | Saves XML tree creating a file named with given string. | |
207 | ||
538f3830 VS |
208 | If {\tt indentstep} is greater than or equal to zero, then, while saving, an automatic indentation |
209 | is added with steps composed by {\tt indentstep} spaces. | |
418ab1e7 | 210 | If {\tt indentstep} is {\tt wxXML\_NO\_INDENTATION}, then, automatic indentation is turned off. |
538f3830 VS |
211 | |
212 | \constfunc{bool}{Save}{\param{wxOutputStream\& }{stream}, \param{int }{indentstep = 1}} | |
4c43dd90 | 213 | |
538f3830 | 214 | Saves XML tree in the given output stream. See other overload for a description of {\tt indentstep}. |
4c43dd90 | 215 | |
74ecac1f | 216 | |
4c43dd90 JS |
217 | \membersection{wxXmlDocument::SetEncoding}\label{wxxmldocumentsetencoding} |
218 | ||
219 | \func{void}{SetEncoding}{\param{const wxString\& }{enc}} | |
220 | ||
221 | Sets the enconding of the document. | |
222 | ||
74ecac1f | 223 | |
4c43dd90 JS |
224 | \membersection{wxXmlDocument::SetFileEncoding}\label{wxxmldocumentsetfileencoding} |
225 | ||
226 | \func{void}{SetFileEncoding}{\param{const wxString\& }{encoding}} | |
227 | ||
228 | Sets the enconding of the file which will be used to save the document. | |
229 | ||
74ecac1f | 230 | |
4c43dd90 JS |
231 | \membersection{wxXmlDocument::SetRoot}\label{wxxmldocumentsetroot} |
232 | ||
233 | \func{void}{SetRoot}{\param{wxXmlNode* }{node}} | |
234 | ||
235 | Sets the root node of this document. Deletes previous root node. | |
e8da6b7c RR |
236 | Use \helpref{DetachRoot}{wxxmldocumentdetachroot} and then |
237 | \helpref{SetRoot}{wxxmldocumentsetroot} if you want | |
74ecac1f VZ |
238 | to replace the root node without deleting the old document tree. |
239 | ||
4c43dd90 JS |
240 | |
241 | \membersection{wxXmlDocument::SetVersion}\label{wxxmldocumentsetversion} | |
242 | ||
243 | \func{void}{SetVersion}{\param{const wxString\& }{version}} | |
244 | ||
245 | Sets the version of the XML file which will be used to save the document. | |
246 | ||
74ecac1f | 247 | |
4c43dd90 JS |
248 | \membersection{wxXmlDocument::operator=}\label{wxxmldocumentoperatorassign} |
249 | ||
250 | \func{wxXmlDocument\& operator}{operator=}{\param{const wxXmlDocument\& }{doc}} | |
251 | ||
e8da6b7c | 252 | Deep copies the given document. |
4c43dd90 | 253 |