huangcm
2025-04-26 2868c607307b8de19383692485d1cbe1b64eb94d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>TinyXml: TinyXML Tutorial</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.4 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a></div>
<div class="nav">
<a class="el" href="index.html">index</a></div>
<h1><a class="anchor" name="tutorial0">TinyXML Tutorial</a></h1><h1>Tutorial Preliminary </h1>
<p>
These pages contains a bunch of examples of using TinyXml.<p>
Each demo is written in a standalone function. If you want to try the code, all you need to do is copy/paste the code into a file, then have a main to call it.<p>
So if the example has a function:<p>
<div class="fragment"><pre class="fragment">    void write_simple_doc()
   {
       ...
   }
   </pre></div><p>
then the *complete* program you need to try it is:<p>
<div class="fragment"><pre class="fragment">    #include "stdafx.h" // &lt;-- you MIGHT need this
   #include "tinyxml.h" // &lt;-- you definitely need this ;)
 
   void write_simple_doc()
   {
       ...
   }
 
   void main( void )
   {
       write_simple_doc();
   }
   </pre></div><p>
Two example XML datasets/files will be used. The first looks like this:<p>
<div class="fragment"><pre class="fragment">    &lt;?xml version="1.0" ?&gt;
   &lt;Hello&gt;World&lt;/Hello&gt;
   </pre></div><p>
The other:<p>
<div class="fragment"><pre class="fragment">    &lt;?xml version="1.0" ?&gt;
   &lt;poetry&gt;
       &lt;verse&gt;
           Alas
             Great Whatever
               Alas (again)
       &lt;/verse&gt;
   &lt;/poetry&gt;
   </pre></div><p>
<h1>Getting Started </h1>
<p>
<h2>Load XML from a file </h2>
<p>
Loading a file is as simple as:<p>
<div class="fragment"><pre class="fragment">    void load_file( )
   {
       TiXmlDocument doc( "demo.xml" );
       bool loadOkay = doc.LoadFile();
 
       if ( loadOkay )
       {
           // Your document is loaded - do what you like
           // with it.
           //
           // Here we'll dump the structure to STDOUT,
           // just for example
           dump_to_stdout( &amp;doc );
       }
       else
       {
           // load failed
       }
   }
   </pre></div><p>
The ``dump_to_stdout`` function is defined in the section `Dump structure of a Document to STDOUT` below. If you run this program with this XML:<p>
<div class="fragment"><pre class="fragment">    &lt;?xml version="1.0" ?&gt;
   &lt;Hello&gt;World&lt;/Hello&gt;
   </pre></div><p>
You'll see this:<p>
<div class="fragment"><pre class="fragment">    DOCUMENT
   + DECLARATION
   + ELEMENT Hello
     + TEXT[World]
   </pre></div><p>
<h2>Building Documents Programatically </h2>
<p>
Example:<p>
<div class="fragment"><pre class="fragment">    void write_simple_doc( )
   {
       // Make xml: &lt;?xml ..&gt;&lt;Hello&gt;World&lt;/Hello&gt;
       TiXmlDocument doc;
       TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
       TiXmlElement * element = new TiXmlElement( "Hello" );
       TiXmlText * text = new TiXmlText( "World" );
       element-&gt;LinkEndChild( text );
       doc.LinkEndChild( decl );
       doc.LinkEndChild( element );
       
       dump_to_stdout( &amp;doc );
       doc.SaveFile( "madeByHand.xml" );
   }
   </pre></div><p>
Alternatively:<p>
<div class="fragment"><pre class="fragment">    void write_simple_doc2( )
   {
       // same as write_simple_doc1 but add each node
       // as early as possible into the tree.
   
       TiXmlDocument doc;
       TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
       doc.LinkEndChild( decl );
       
       TiXmlElement * element = new TiXmlElement( "Hello" );
       doc.LinkEndChild( element );
       
       TiXmlText * text = new TiXmlText( "World" );
       element-&gt;LinkEndChild( text );
       
       dump_to_stdout( &amp;doc );
       doc.SaveFile( "madeByHand2.xml" );
   }
   </pre></div><p>
Both of these produce the same XML, namely:<p>
<div class="fragment"><pre class="fragment">    &lt;?xml version="1.0" ?&gt;
   &lt;Hello&gt;World&lt;/Hello&gt;
   </pre></div><p>
Or in structure form:<p>
<div class="fragment"><pre class="fragment">    DOCUMENT
   + DECLARATION
   + ELEMENT Hello
     + TEXT[World]
   </pre></div><p>
<h2>Saving Documents to File </h2>
<p>
This function:<p>
<div class="fragment"><pre class="fragment">    void write_simple_doc3( )  
   {  
       // This example courtesy of polocolege
    
       TiXmlDocument doc;  
       TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );  
       doc.LinkEndChild( decl );  
    
       TiXmlElement * element = new TiXmlElement( "Hello" );  
       doc.LinkEndChild( element );  
    
       TiXmlText * text = new TiXmlText( "Opening a new salutation" );  
       element-&gt;LinkEndChild( text );  
    
       TiXmlElement * element2 = new TiXmlElement( "Greeting" );  
       element-&gt;LinkEndChild( element2 );  
    
       TiXmlText * text2 = new TiXmlText( "How are you?" );  
       element2-&gt;LinkEndChild( text2 );  
    
       TiXmlElement * element3 = new TiXmlElement( "Language" );  
       element2-&gt;LinkEndChild( element3 );  
    
       TiXmlText * text3 = new TiXmlText( "English" );  
       element3-&gt;LinkEndChild( text3 );  
    
       TiXmlElement * element4 = new TiXmlElement( "Exclamation" );  
       element-&gt;LinkEndChild( element4 );  
    
       TiXmlText * text4 = new TiXmlText( "You have children!" );  
       element4-&gt;LinkEndChild( text4 );  
    
       dump_to_stdout( &amp;doc );
       doc.SaveFile( "madeByHand3.xml" );  
   }  
   </pre></div><p>
Produces this structure:<p>
<div class="fragment"><pre class="fragment">    Document
   + Declaration
   + Element "Hello"
     + Text: [Opening a new salutation]
     + Element "Greeting"
       + Text: [How are you?]
       + Element "Language"
         + Text: [English]
     + Element "Exclamation"
       + Text: [You have children!]
   </pre></div><p>
The file ``madeByHand3.xml`` looks exactly like this (including indents):<p>
<div class="fragment"><pre class="fragment">    &lt;?xml version="1.0" ?&gt;
   &lt;Hello&gt;Opening a new salutation
       &lt;Greeting&gt;How are you?
           &lt;Language&gt;English&lt;/Language&gt;
       &lt;/Greeting&gt;
       &lt;Exclamation&gt;You have children!&lt;/Exclamation&gt;
   &lt;/Hello&gt;
   </pre></div><p>
I was surprised that TinyXml, by default, writes the XML in what other APIs call a "pretty" format - it modifies the whitespace of text of elements that contain other nodes so that writing the tree includes an indication of nesting level.<p>
I haven't looked yet to see if there is a way to turn off indenting when writing a file - its bound to be easy.<p>
[Lee: It's easy in STL mode, just use cout &lt;&lt; myDoc. Non-STL mode is always in "pretty" format. Adding a switch would be a nice feature and has been requested.]<p>
<h1>Iterating Over Documents </h1>
<p>
<h2>Dump structure of a Document to STDOUT </h2>
<p>
Often when you're starting its helpful and reassuring to know that you're document got loaded as you expect it to.<p>
Below I've defined a function to walk a document and write contents to STDOUT:<p>
<div class="fragment"><pre class="fragment">    // a utility function defining a very simple method to indent a line of text
   const char * getIndent( unsigned int numIndents )
   {
       static const char * pINDENT = "                                      + ";
       static const unsigned int LENGTH = strlen( pINDENT );
   
       if ( numIndents &gt; LENGTH ) numIndents = LENGTH;
   
       return &amp;pINDENT[ LENGTH-numIndents ];
   }
   
   void dump_to_stdout( TiXmlNode * pParent, unsigned int indent = 0 )
   {
       if ( !pParent ) return;
   
       TiXmlText *pText;
       int t = pParent-&gt;Type();
       printf( "%s", getIndent( indent));
   
       switch ( t )
       {
       case TiXmlNode::DOCUMENT:
           printf( "Document" );
           break;
   
       case TiXmlNode::ELEMENT:
           printf( "Element \"%s\"", pParent-&gt;Value() );
           break;
   
       case TiXmlNode::COMMENT:
           printf( "Comment: \"%s\"", pParent-&gt;Value());
           break;
   
       case TiXmlNode::UNKNOWN:
           printf( "Unknown" );
           break;
   
       case TiXmlNode::TEXT:
           pText = pParent-&gt;ToText();
           printf( "Text: [%s]", pText-&gt;Value() );
           break;
   
       case TiXmlNode::DECLARATION:
           printf( "Declaration" );
           break;
       default:
           break;
       }
       printf( "\n" );
   
       TiXmlNode * pChild;
   
       for ( pChild = pParent-&gt;FirstChild(); pChild != 0; pChild = pChild-&gt;NextSibling()) 
       {
           dump_to_stdout( pChild, indent+2 );
       }
   }
   </pre></div><p>
To load a file and dump its structure:<p>
<div class="fragment"><pre class="fragment">    void load_and_display( )
   {
       // important for the poetry demo, but you may not need this 
       // in your own projects
       TiXmlBase::SetCondenseWhiteSpace( false );
   
       TiXmlDocument doc( "demo.xml" );
       bool loadOkay = doc.LoadFile();
   
       if ( loadOkay )
       {
           dump_to_stdout( &amp;doc );
       }
       else
       {
           printf( "Something went wrong\n" );
       }
   }
   </pre></div><p>
If you run this with the first XML file you'll see this on STDOUT:<p>
<div class="fragment"><pre class="fragment">    DOCUMENT
   + DECLARATION
   + ELEMENT Hello
     + TEXT[World]
   </pre></div><p>
and on the second XML file:<p>
<div class="fragment"><pre class="fragment">    DOCUMENT
   + DECLARATION
   + ELEMENT poetry
     + COMMENT:  my great work of art
     + ELEMENT verse
       + TEXT[
   Alas
     Great Whatever
       Alas (again)
   ]
   </pre></div><p>
Note that if you call dump_to_stdout like this:<p>
<div class="fragment"><pre class="fragment">    dump_to_stdout( doc.RootElement());
   </pre></div><p>
You'll see this instead:<p>
<div class="fragment"><pre class="fragment">    ELEMENT Hello
   + TEXT[World]
   </pre></div><p>
and:<p>
<div class="fragment"><pre class="fragment">    ELEMENT poetry
   + COMMENT:  my great work of art
   + ELEMENT verse
      + TEXT[
   Alas
     Great Whatever
       Alas (again)
   ] 
   </pre></div><p>
<em> Authors and Changes <ul>
<li>
Written by Ellers, April 2005  </li>
<li>
Minor edits and integration into doc system, Lee Thomason September 2005  </li>
</ul>
</em> <hr size="1"><address style="align: right;"><small>Generated on Sat Oct 8 14:15:30 2005 for TinyXml by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address>
</body>
</html>