summaryrefslogtreecommitdiff
path: root/shared/tinyxml/docs/tutorial0.html
blob: f974f859d917019baca501625336e6acb5c9aa45 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TinyXml: TinyXML Tutorial</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="index.html">index</a>
  </div>
</div>
<div class="contents">


<h1><a class="anchor" id="tutorial0">TinyXML Tutorial </a></h1><h1>What is this? </h1>
<p>This tutorial has a few tips and suggestions on how to use TinyXML effectively.</p>
<p>I've also tried to include some C++ tips like how to convert strings to integers and vice versa. This isn't anything to do with TinyXML itself, but it may helpful for your project so I've put it in anyway.</p>
<p>If you don't know basic C++ concepts this tutorial won't be useful. Likewise if you don't know what a DOM is, look elsewhere first.</p>
<h1>Before we start </h1>
<p>Some example XML datasets/files will be used.</p>
<p>example1.xml:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;Hello&gt;World&lt;/Hello&gt;
</pre></div><p>example2.xml:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;poetry&gt;
	&lt;verse&gt;
		Alas
		  Great World
			Alas (again)
	&lt;/verse&gt;
&lt;/poetry&gt;
</pre></div><p>example3.xml:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;shapes&gt;
	&lt;circle name="int-based" x="20" y="30" r="50" /&gt;
	&lt;point name="float-based" x="3.5" y="52.1" /&gt;
&lt;/shapes&gt;
</pre></div><p>example4.xml</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;MyApp&gt;
    &lt;!-- Settings for MyApp --&gt;
    &lt;Messages&gt;
        &lt;Welcome&gt;Welcome to MyApp&lt;/Welcome&gt;
        &lt;Farewell&gt;Thank you for using MyApp&lt;/Farewell&gt;
    &lt;/Messages&gt;
    &lt;Windows&gt;
        &lt;Window name="MainFrame" x="5" y="15" w="400" h="250" /&gt;
    &lt;/Windows&gt;
    &lt;Connection ip="192.168.0.1" timeout="123.456000" /&gt;
&lt;/MyApp&gt;
</pre></div><h1>Getting Started </h1>
<h2>Load XML from a file </h2>
<p>The simplest way to load a file into a TinyXML DOM is:</p>
<div class="fragment"><pre class="fragment">
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
</pre></div><p>A more real-world usage is shown below. This will load the file and display the contents to STDOUT:</p>
<div class="fragment"><pre class="fragment">
// load the named file and dump its structure to STDOUT
void dump_to_stdout(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		printf("\n%s:\n", pFilename);
		dump_to_stdout( &amp;doc ); // defined later in the tutorial
	}
	else
	{
		printf("Failed to load file \"%s\"\n", pFilename);
	}
}
</pre></div><p>A simple demonstration of this function is to use a main like this:</p>
<div class="fragment"><pre class="fragment">
int main(void)
{
	dump_to_stdout("example1.xml");
	return 0;
}
</pre></div><p>Recall that Example 1 XML is:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;Hello&gt;World&lt;/Hello&gt;
</pre></div><p>Running the program with this XML will display this in the console/DOS window:</p>
<div class="fragment"><pre class="fragment">
DOCUMENT
+ DECLARATION
+ ELEMENT Hello
  + TEXT[World]
</pre></div><p>The ``dump_to_stdout`` function is defined later in this tutorial and is useful if you want to understand recursive traversal of a DOM.</p>
<h2>Building Documents Programatically </h2>
<p>This is how to build Example 1 pragmatically:</p>
<div class="fragment"><pre class="fragment">
void build_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 );
	doc.SaveFile( "madeByHand.xml" );
}
</pre></div><p>This can be loaded and displayed on the console with:</p>
<div class="fragment"><pre class="fragment">
dump_to_stdout("madeByHand.xml"); // this func defined later in the tutorial
</pre></div><p>and you'll see it is identical to Example 1:</p>
<div class="fragment"><pre class="fragment">
madeByHand.xml:
Document
+ Declaration
+ Element [Hello]
  + Text: [World]
</pre></div><p>This code produces exactly the same XML DOM but it shows a different ordering to node creation and linking:</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 );
	
	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><h2>Attributes </h2>
<p>Given an existing node, settings attributes is easy:</p>
<div class="fragment"><pre class="fragment">
window = new TiXmlElement( "Demo" );  
window-&gt;SetAttribute("name", "Circle");
window-&gt;SetAttribute("x", 5);
window-&gt;SetAttribute("y", 15);
window-&gt;SetDoubleAttribute("radius", 3.14159);
</pre></div><p>You can it also work with the <a class="el" href="classTiXmlAttribute.html" title="An attribute is a name-value pair.">TiXmlAttribute</a> objects if you want.</p>
<p>The following code shows one way (not the only way) to get all attributes of an element, print the name and string value, and if the value can be converted to an integer or double, print that value too:</p>
<div class="fragment"><pre class="fragment">
// print all attributes of pElement.
// returns the number of attributes printed
int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
	if ( !pElement ) return 0;

	TiXmlAttribute* pAttrib=pElement-&gt;FirstAttribute();
	int i=0;
	int ival;
	double dval;
	const char* pIndent=getIndent(indent);
	printf("\n");
	while (pAttrib)
	{
		printf( "%s%s: value=[%s]", pIndent, pAttrib-&gt;Name(), pAttrib-&gt;Value());

		if (pAttrib-&gt;QueryIntValue(&amp;ival)==TIXML_SUCCESS)    printf( " int=%d", ival);
		if (pAttrib-&gt;QueryDoubleValue(&amp;dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
		printf( "\n" );
		i++;
		pAttrib=pAttrib-&gt;Next();
	}
	return i;
}
</pre></div><h2>Writing a document to a file </h2>
<p>Writing a pre-built DOM to a file is trivial:</p>
<div class="fragment"><pre class="fragment">
doc.SaveFile( saveFilename );  
</pre></div><p>Recall, for example, example 4:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;MyApp&gt;
    &lt;!-- Settings for MyApp --&gt;
    &lt;Messages&gt;
        &lt;Welcome&gt;Welcome to MyApp&lt;/Welcome&gt;
        &lt;Farewell&gt;Thank you for using MyApp&lt;/Farewell&gt;
    &lt;/Messages&gt;
    &lt;Windows&gt;
        &lt;Window name="MainFrame" x="5" y="15" w="400" h="250" /&gt;
    &lt;/Windows&gt;
    &lt;Connection ip="192.168.0.1" timeout="123.456000" /&gt;
&lt;/MyApp&gt;
</pre></div><p>The following function builds this DOM and writes the file "appsettings.xml":</p>
<div class="fragment"><pre class="fragment">
void write_app_settings_doc( )  
{  
	TiXmlDocument doc;  
	TiXmlElement* msg;
 	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	doc.LinkEndChild( decl );  
 
	TiXmlElement * root = new TiXmlElement( "MyApp" );  
	doc.LinkEndChild( root );  

	TiXmlComment * comment = new TiXmlComment();
	comment-&gt;SetValue(" Settings for MyApp " );  
	root-&gt;LinkEndChild( comment );  
 
	TiXmlElement * msgs = new TiXmlElement( "Messages" );  
	root-&gt;LinkEndChild( msgs );  
 
	msg = new TiXmlElement( "Welcome" );  
	msg-&gt;LinkEndChild( new TiXmlText( "Welcome to MyApp" ));  
	msgs-&gt;LinkEndChild( msg );  
 
	msg = new TiXmlElement( "Farewell" );  
	msg-&gt;LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));  
	msgs-&gt;LinkEndChild( msg );  
 
	TiXmlElement * windows = new TiXmlElement( "Windows" );  
	root-&gt;LinkEndChild( windows );  

	TiXmlElement * window;
	window = new TiXmlElement( "Window" );  
	windows-&gt;LinkEndChild( window );  
	window-&gt;SetAttribute("name", "MainFrame");
	window-&gt;SetAttribute("x", 5);
	window-&gt;SetAttribute("y", 15);
	window-&gt;SetAttribute("w", 400);
	window-&gt;SetAttribute("h", 250);

	TiXmlElement * cxn = new TiXmlElement( "Connection" );  
	root-&gt;LinkEndChild( cxn );  
	cxn-&gt;SetAttribute("ip", "192.168.0.1");
	cxn-&gt;SetDoubleAttribute("timeout", 123.456); // floating point attrib
	
	dump_to_stdout( &amp;doc );
	doc.SaveFile( "appsettings.xml" );  
} 
</pre></div><p>The dump_to_stdout function will show this structure:</p>
<div class="fragment"><pre class="fragment">
Document
+ Declaration
+ Element [MyApp]
 (No attributes)
  + Comment: [ Settings for MyApp ]
  + Element [Messages]
 (No attributes)
    + Element [Welcome]
 (No attributes)
      + Text: [Welcome to MyApp]
    + Element [Farewell]
 (No attributes)
      + Text: [Thank you for using MyApp]
  + Element [Windows]
 (No attributes)
    + Element [Window]
      + name: value=[MainFrame]
      + x: value=[5] int=5 d=5.0
      + y: value=[15] int=15 d=15.0
      + w: value=[400] int=400 d=400.0
      + h: value=[250] int=250 d=250.0
      5 attributes
  + Element [Connection]
    + ip: value=[192.168.0.1] int=192 d=192.2
    + timeout: value=[123.456000] int=123 d=123.5
    2 attributes
</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>
<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>
<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>XML to/from C++ objects </h1>
<h2>Intro </h2>
<p>This example assumes you're loading and saving your app settings in an XML file, e.g. something like example4.xml.</p>
<p>There are a number of ways to do this. For example, look into the TinyBind project at <a href="http://sourceforge.net/projects/tinybind">http://sourceforge.net/projects/tinybind</a></p>
<p>This section shows a plain-old approach to loading and saving a basic object structure using XML.</p>
<h2>Set up your object classes </h2>
<p>Start off with some basic classes like these:</p>
<div class="fragment"><pre class="fragment">
#include &lt;string&gt;
#include &lt;map&gt;
using namespace std;

typedef std::map&lt;std::string,std::string&gt; MessageMap;

// a basic window abstraction - demo purposes only
class WindowSettings
{
public:
	int x,y,w,h;
	string name;

	WindowSettings()
		: x(0), y(0), w(100), h(100), name("Untitled")
	{
	}

	WindowSettings(int x, int y, int w, int h, const string&amp; name)
	{
		this-&gt;x=x;
		this-&gt;y=y;
		this-&gt;w=w;
		this-&gt;h=h;
		this-&gt;name=name;
	}
};

class ConnectionSettings
{
public:
	string ip;
	double timeout;
};

class AppSettings
{
public:
	string m_name;
	MessageMap m_messages;
	list&lt;WindowSettings&gt; m_windows;
	ConnectionSettings m_connection;

	AppSettings() {}

	void save(const char* pFilename);
	void load(const char* pFilename);
	
	// just to show how to do it
	void setDemoValues()
	{
		m_name="MyApp";
		m_messages.clear();
		m_messages["Welcome"]="Welcome to "+m_name;
		m_messages["Farewell"]="Thank you for using "+m_name;
		m_windows.clear();
		m_windows.push_back(WindowSettings(15,15,400,250,"Main"));
		m_connection.ip="Unknown";
		m_connection.timeout=123.456;
	}
};
</pre></div><p>This is a basic main() that shows how to create a default settings object tree, save it and load it again:</p>
<div class="fragment"><pre class="fragment">
int main(void)
{
	AppSettings settings;
	
	settings.save("appsettings2.xml");
	settings.load("appsettings2.xml");
	return 0;
}
</pre></div><p>The following main() shows creation, modification, saving and then loading of a settings structure:</p>
<div class="fragment"><pre class="fragment">
int main(void)
{
	// block: customise and save settings
	{
		AppSettings settings;
		settings.m_name="HitchHikerApp";
		settings.m_messages["Welcome"]="Don't Panic";
		settings.m_messages["Farewell"]="Thanks for all the fish";
		settings.m_windows.push_back(WindowSettings(15,25,300,250,"BookFrame"));
		settings.m_connection.ip="192.168.0.77";
		settings.m_connection.timeout=42.0;

		settings.save("appsettings2.xml");
	}
	
	// block: load settings
	{
		AppSettings settings;
		settings.load("appsettings2.xml");
		printf("%s: %s\n", settings.m_name.c_str(), 
			settings.m_messages["Welcome"].c_str());
		WindowSettings &amp; w=settings.m_windows.front();
		printf("%s: Show window '%s' at %d,%d (%d x %d)\n", 
			settings.m_name.c_str(), w.name.c_str(), w.x, w.y, w.w, w.h);
		printf("%s: %s\n", settings.m_name.c_str(), settings.m_messages["Farewell"].c_str());
	}
	return 0;
}
</pre></div><p>When the save() and load() are completed (see below), running this main() displays on the console:</p>
<div class="fragment"><pre class="fragment">
HitchHikerApp: Don't Panic
HitchHikerApp: Show window 'BookFrame' at 15,25 (300 x 100)
HitchHikerApp: Thanks for all the fish
</pre></div><h2>Encode C++ state as XML </h2>
<p>There are lots of different ways to approach saving this to a file. Here's one:</p>
<div class="fragment"><pre class="fragment">
void AppSettings::save(const char* pFilename)
{
	TiXmlDocument doc;  
	TiXmlElement* msg;
	TiXmlComment * comment;
	string s;
 	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	doc.LinkEndChild( decl ); 
 
	TiXmlElement * root = new TiXmlElement(m_name.c_str());  
	doc.LinkEndChild( root );  

	comment = new TiXmlComment();
	s=" Settings for "+m_name+" ";
	comment-&gt;SetValue(s.c_str());  
	root-&gt;LinkEndChild( comment );  

	// block: messages
	{
		MessageMap::iterator iter;

		TiXmlElement * msgs = new TiXmlElement( "Messages" );  
		root-&gt;LinkEndChild( msgs );  
 
		for (iter=m_messages.begin(); iter != m_messages.end(); iter++)
		{
			const string &amp; key=(*iter).first;
			const string &amp; value=(*iter).second;
			msg = new TiXmlElement(key.c_str());  
			msg-&gt;LinkEndChild( new TiXmlText(value.c_str()));  
			msgs-&gt;LinkEndChild( msg );  
		}
	}

	// block: windows
	{
		TiXmlElement * windowsNode = new TiXmlElement( "Windows" );  
		root-&gt;LinkEndChild( windowsNode );  

		list&lt;WindowSettings&gt;::iterator iter;

		for (iter=m_windows.begin(); iter != m_windows.end(); iter++)
		{
			const WindowSettings&amp; w=*iter;

			TiXmlElement * window;
			window = new TiXmlElement( "Window" );  
			windowsNode-&gt;LinkEndChild( window );  
			window-&gt;SetAttribute("name", w.name.c_str());
			window-&gt;SetAttribute("x", w.x);
			window-&gt;SetAttribute("y", w.y);
			window-&gt;SetAttribute("w", w.w);
			window-&gt;SetAttribute("h", w.h);
		}
	}

	// block: connection
	{
		TiXmlElement * cxn = new TiXmlElement( "Connection" );  
		root-&gt;LinkEndChild( cxn );  
		cxn-&gt;SetAttribute("ip", m_connection.ip.c_str());
		cxn-&gt;SetDoubleAttribute("timeout", m_connection.timeout); 
	}

	doc.SaveFile(pFilename);  
}
</pre></div><p>Running this with the modified main produces this file:</p>
<div class="fragment"><pre class="fragment">
&lt;?xml version="1.0" ?&gt;
&lt;HitchHikerApp&gt;
    &lt;!-- Settings for HitchHikerApp --&gt;
    &lt;Messages&gt;
        &lt;Farewell&gt;Thanks for all the fish&lt;/Farewell&gt;
        &lt;Welcome&gt;Don&amp;apos;t Panic&lt;/Welcome&gt;
    &lt;/Messages&gt;
    &lt;Windows&gt;
        &lt;Window name="BookFrame" x="15" y="25" w="300" h="250" /&gt;
    &lt;/Windows&gt;
    &lt;Connection ip="192.168.0.77" timeout="42.000000" /&gt;
&lt;/HitchHikerApp&gt;
</pre></div><h2>Decoding state from XML </h2>
<p>As with encoding objects, there are a number of approaches to decoding XML into your own C++ object structure. The following approach uses TiXmlHandles.</p>
<div class="fragment"><pre class="fragment">
void AppSettings::load(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	if (!doc.LoadFile()) return;

	TiXmlHandle hDoc(&amp;doc);
	TiXmlElement* pElem;
	TiXmlHandle hRoot(0);

	// block: name
	{
		pElem=hDoc.FirstChildElement().Element();
		// should always have a valid root but handle gracefully if it does
		if (!pElem) return;
		m_name=pElem-&gt;Value();

		// save this for later
		hRoot=TiXmlHandle(pElem);
	}

	// block: string table
	{
		m_messages.clear(); // trash existing table

		pElem=hRoot.FirstChild( "Messages" ).FirstChild().Element();
		for( pElem; pElem; pElem=pElem-&gt;NextSiblingElement())
		{
			const char *pKey=pElem-&gt;Value();
			const char *pText=pElem-&gt;GetText();
			if (pKey &amp;&amp; pText) 
			{
				m_messages[pKey]=pText;
			}
		}
	}

	// block: windows
	{
		m_windows.clear(); // trash existing list

		TiXmlElement* pWindowNode=hRoot.FirstChild( "Windows" ).FirstChild().Element();
		for( pWindowNode; pWindowNode; pWindowNode=pWindowNode-&gt;NextSiblingElement())
		{
			WindowSettings w;
			const char *pName=pWindowNode-&gt;Attribute("name");
			if (pName) w.name=pName;
			
			pWindowNode-&gt;QueryIntAttribute("x", &amp;w.x); // If this fails, original value is left as-is
			pWindowNode-&gt;QueryIntAttribute("y", &amp;w.y);
			pWindowNode-&gt;QueryIntAttribute("w", &amp;w.w);
			pWindowNode-&gt;QueryIntAttribute("hh", &amp;w.h);

			m_windows.push_back(w);
		}
	}

	// block: connection
	{
		pElem=hRoot.FirstChild("Connection").Element();
		if (pElem)
		{
			m_connection.ip=pElem-&gt;Attribute("ip");
			pElem-&gt;QueryDoubleAttribute("timeout",&amp;m_connection.timeout);
		}
	}
}
</pre></div><h1>Full listing for dump_to_stdout </h1>
<p>Below is a copy-and-paste demo program for loading arbitrary XML files and dumping the structure to STDOUT using the recursive traversal listed above.</p>
<div class="fragment"><pre class="fragment">
// tutorial demo program
#include "stdafx.h"
#include "tinyxml.h"

// ----------------------------------------------------------------------
// STDOUT dump and indenting utility functions
// ----------------------------------------------------------------------
const unsigned int NUM_INDENTS_PER_SPACE=2;

const char * getIndent( unsigned int numIndents )
{
	static const char * pINDENT="                                      + ";
	static const unsigned int LENGTH=strlen( pINDENT );
	unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
	if ( n &gt; LENGTH ) n = LENGTH;

	return &amp;pINDENT[ LENGTH-n ];
}

// same as getIndent but no "+" at the end
const char * getIndentAlt( unsigned int numIndents )
{
	static const char * pINDENT="                                        ";
	static const unsigned int LENGTH=strlen( pINDENT );
	unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
	if ( n &gt; LENGTH ) n = LENGTH;

	return &amp;pINDENT[ LENGTH-n ];
}

int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
	if ( !pElement ) return 0;

	TiXmlAttribute* pAttrib=pElement-&gt;FirstAttribute();
	int i=0;
	int ival;
	double dval;
	const char* pIndent=getIndent(indent);
	printf("\n");
	while (pAttrib)
	{
		printf( "%s%s: value=[%s]", pIndent, pAttrib-&gt;Name(), pAttrib-&gt;Value());

		if (pAttrib-&gt;QueryIntValue(&amp;ival)==TIXML_SUCCESS)    printf( " int=%d", ival);
		if (pAttrib-&gt;QueryDoubleValue(&amp;dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
		printf( "\n" );
		i++;
		pAttrib=pAttrib-&gt;Next();
	}
	return i;	
}

void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
	if ( !pParent ) return;

	TiXmlNode* pChild;
	TiXmlText* pText;
	int t = pParent-&gt;Type();
	printf( "%s", getIndent(indent));
	int num;

	switch ( t )
	{
	case TiXmlNode::TINYXML_DOCUMENT:
		printf( "Document" );
		break;

	case TiXmlNode::TINYXML_ELEMENT:
		printf( "Element [%s]", pParent-&gt;Value() );
		num=dump_attribs_to_stdout(pParent-&gt;ToElement(), indent+1);
		switch(num)
		{
			case 0:  printf( " (No attributes)"); break;
			case 1:  printf( "%s1 attribute", getIndentAlt(indent)); break;
			default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
		}
		break;

	case TiXmlNode::TINYXML_COMMENT:
		printf( "Comment: [%s]", pParent-&gt;Value());
		break;

	case TiXmlNode::TINYXML_UNKNOWN:
		printf( "Unknown" );
		break;

	case TiXmlNode::TINYXML_TEXT:
		pText = pParent-&gt;ToText();
		printf( "Text: [%s]", pText-&gt;Value() );
		break;

	case TiXmlNode::TINYXML_DECLARATION:
		printf( "Declaration" );
		break;
	default:
		break;
	}
	printf( "\n" );
	for ( pChild = pParent-&gt;FirstChild(); pChild != 0; pChild = pChild-&gt;NextSibling()) 
	{
		dump_to_stdout( pChild, indent+1 );
	}
}

// load the named file and dump its structure to STDOUT
void dump_to_stdout(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		printf("\n%s:\n", pFilename);
		dump_to_stdout( &amp;doc ); // defined later in the tutorial
	}
	else
	{
		printf("Failed to load file \"%s\"\n", pFilename);
	}
}

// ----------------------------------------------------------------------
// main() for printing files named on the command line
// ----------------------------------------------------------------------
int main(int argc, char* argv[])
{
	for (int i=1; i&lt;argc; i++)
	{
		dump_to_stdout(argv[i]);
	}
	return 0;
}
</pre></div><p>Run this from the command line or a DOS window, e.g.:</p>
<div class="fragment"><pre class="fragment">
C:\dev\tinyxml&gt; Debug\tinyxml_1.exe example1.xml

example1.xml:
Document
+ Declaration
+ Element [Hello]
 (No attributes)
  + Text: [World]
</pre></div><p><em> Authors and Changes </p>
<ul>
<li>
Written by Ellers, April, May, June 2005  </li>
<li>
Minor edits and integration into doc system, Lee Thomason September 2005  </li>
<li>
Updated by Ellers, October 2005  </li>
</ul>
<p></em> </p>
</div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>
bgstack15