/** @page tutorial0 TinyXML Tutorial
What is this?
This tutorial has a few tips and suggestions on how to use TinyXML
effectively.
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.
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.
Before we start
Some example XML datasets/files will be used.
example1.xml:
@verbatim
World
@endverbatim
example2.xml:
@verbatim
Alas
Great World
Alas (again)
@endverbatim
example3.xml:
@verbatim
@endverbatim
example4.xml
@verbatim
Welcome to MyApp
Thank you for using MyApp
@endverbatim
Getting Started
Load XML from a file
The simplest way to load a file into a TinyXML DOM is:
@verbatim
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
@endverbatim
A more real-world usage is shown below. This will load the file and display
the contents to STDOUT:
@verbatim
// 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( &doc ); // defined later in the tutorial
}
else
{
printf("Failed to load file \"%s\"\n", pFilename);
}
}
@endverbatim
A simple demonstration of this function is to use a main like this:
@verbatim
int main(void)
{
dump_to_stdout("example1.xml");
return 0;
}
@endverbatim
Recall that Example 1 XML is:
@verbatim
World
@endverbatim
Running the program with this XML will display this in the console/DOS window:
@verbatim
DOCUMENT
+ DECLARATION
+ ELEMENT Hello
+ TEXT[World]
@endverbatim
The ``dump_to_stdout`` function is defined later in this tutorial and is
useful if you want to understand recursive traversal of a DOM.
Building Documents Programatically
This is how to build Example 1 pragmatically:
@verbatim
void build_simple_doc( )
{
// Make xml: World
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
doc.SaveFile( "madeByHand.xml" );
}
@endverbatim
This can be loaded and displayed on the console with:
@verbatim
dump_to_stdout("madeByHand.xml"); // this func defined later in the tutorial
@endverbatim
and you'll see it is identical to Example 1:
@verbatim
madeByHand.xml:
Document
+ Declaration
+ Element [Hello]
+ Text: [World]
@endverbatim
This code produces exactly the same XML DOM but it shows a different ordering
to node creation and linking:
@verbatim
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->LinkEndChild( text );
doc.SaveFile( "madeByHand2.xml" );
}
@endverbatim
Both of these produce the same XML, namely:
@verbatim
World
@endverbatim
Or in structure form:
@verbatim
DOCUMENT
+ DECLARATION
+ ELEMENT Hello
+ TEXT[World]
@endverbatim
Attributes
Given an existing node, settings attributes is easy:
@verbatim
window = new TiXmlElement( "Demo" );
window->SetAttribute("name", "Circle");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetDoubleAttribute("radius", 3.14159);
@endverbatim
You can it also work with the TiXmlAttribute objects if you want.
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:
@verbatim
// 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->FirstAttribute();
int i=0;
int ival;
double dval;
const char* pIndent=getIndent(indent);
printf("\n");
while (pAttrib)
{
printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) printf( " int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
printf( "\n" );
i++;
pAttrib=pAttrib->Next();
}
return i;
}
@endverbatim
Writing a document to a file
Writing a pre-built DOM to a file is trivial:
@verbatim
doc.SaveFile( saveFilename );
@endverbatim
Recall, for example, example 4:
@verbatim
Welcome to MyApp
Thank you for using MyApp
@endverbatim
The following function builds this DOM and writes the file "appsettings.xml":
@verbatim
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->SetValue(" Settings for MyApp " );
root->LinkEndChild( comment );
TiXmlElement * msgs = new TiXmlElement( "Messages" );
root->LinkEndChild( msgs );
msg = new TiXmlElement( "Welcome" );
msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));
msgs->LinkEndChild( msg );
msg = new TiXmlElement( "Farewell" );
msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));
msgs->LinkEndChild( msg );
TiXmlElement * windows = new TiXmlElement( "Windows" );
root->LinkEndChild( windows );
TiXmlElement * window;
window = new TiXmlElement( "Window" );
windows->LinkEndChild( window );
window->SetAttribute("name", "MainFrame");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetAttribute("w", 400);
window->SetAttribute("h", 250);
TiXmlElement * cxn = new TiXmlElement( "Connection" );
root->LinkEndChild( cxn );
cxn->SetAttribute("ip", "192.168.0.1");
cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib
dump_to_stdout( &doc );
doc.SaveFile( "appsettings.xml" );
}
@endverbatim
The dump_to_stdout function will show this structure:
@verbatim
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
@endverbatim
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.
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.
[Lee: It's easy in STL mode, just use cout << myDoc. Non-STL mode is
always in "pretty" format. Adding a switch would be a nice feature and
has been requested.]
XML to/from C++ objects
Intro
This example assumes you're loading and saving your app settings in an
XML file, e.g. something like example4.xml.
There are a number of ways to do this. For example, look into the TinyBind
project at http://sourceforge.net/projects/tinybind
This section shows a plain-old approach to loading and saving a basic object
structure using XML.
Set up your object classes
Start off with some basic classes like these:
@verbatim
#include
#include