Revert "Update to version 2.6.2."
Have the fix things for some other branches.
This reverts commit c3bbea3c3cfee4908189a57b3fc54f105b78c59b.
Change-Id: I57e6ff5cecb0b0a73673b52d00b2549c3cd3615e
diff --git a/docs/tutorial0.html b/docs/tutorial0.html
index f974f85..9625d37 100644
--- a/docs/tutorial0.html
+++ b/docs/tutorial0.html
@@ -1,720 +1,323 @@
-<!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"/>
+<!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="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 Page</span></a></li>
- <li class="current"><a href="pages.html"><span>Related 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">
+<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 Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class 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" // <-- you MIGHT need this
+ #include "tinyxml.h" // <-- you definitely need this ;)
+ void write_simple_doc()
+ {
+ ...
+ }
-<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">
-<?xml version="1.0" ?>
-<Hello>World</Hello>
-</pre></div><p>example2.xml:</p>
-<div class="fragment"><pre class="fragment">
-<?xml version="1.0" ?>
-<poetry>
- <verse>
- Alas
- Great World
- Alas (again)
- </verse>
-</poetry>
-</pre></div><p>example3.xml:</p>
-<div class="fragment"><pre class="fragment">
-<?xml version="1.0" ?>
-<shapes>
- <circle name="int-based" x="20" y="30" r="50" />
- <point name="float-based" x="3.5" y="52.1" />
-</shapes>
-</pre></div><p>example4.xml</p>
-<div class="fragment"><pre class="fragment">
-<?xml version="1.0" ?>
-<MyApp>
- <!-- Settings for MyApp -->
- <Messages>
- <Welcome>Welcome to MyApp</Welcome>
- <Farewell>Thank you for using MyApp</Farewell>
- </Messages>
- <Windows>
- <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- </Windows>
- <Connection ip="192.168.0.1" timeout="123.456000" />
-</MyApp>
-</pre></div><h1>Getting Started </h1>
+ 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"> <?xml version="1.0" ?>
+ <Hello>World</Hello>
+ </pre></div><p>
+The other:<p>
+<div class="fragment"><pre class="fragment"> <?xml version="1.0" ?>
+ <poetry>
+ <verse>
+ Alas
+ Great Whatever
+ Alas (again)
+ </verse>
+ </poetry>
+ </pre></div><p>
+<h1>Getting Started </h1>
+<p>
<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)
+<p>
+Loading a file is as simple as:<p>
+<div class="fragment"><pre class="fragment"> void load_file( )
{
- printf("\n%s:\n", pFilename);
- dump_to_stdout( &doc ); // defined later in the tutorial
+ 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( &doc );
+ }
+ else
+ {
+ // load failed
+ }
}
- 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">
-<?xml version="1.0" ?>
-<Hello>World</Hello>
-</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>
+ </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"> <?xml version="1.0" ?>
+ <Hello>World</Hello>
+ </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>This is how to build Example 1 pragmatically:</p>
-<div class="fragment"><pre class="fragment">
-void build_simple_doc( )
-{
- // Make xml: <?xml ..><Hello>World</Hello>
- 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" );
-}
-</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->LinkEndChild( text );
-
- doc.SaveFile( "madeByHand2.xml" );
-}
-</pre></div><p>Both of these produce the same XML, namely:</p>
-<div class="fragment"><pre class="fragment">
-<?xml version="1.0" ?>
-<Hello>World</Hello>
-</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->SetAttribute("name", "Circle");
-window->SetAttribute("x", 5);
-window->SetAttribute("y", 15);
-window->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->FirstAttribute();
- int i=0;
- int ival;
- double dval;
- const char* pIndent=getIndent(indent);
- printf("\n");
- while (pAttrib)
+<p>
+Example:<p>
+<div class="fragment"><pre class="fragment"> void write_simple_doc( )
{
- 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);
+ // Make xml: <?xml ..><Hello>World</Hello>
+ 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 );
+
+ dump_to_stdout( &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->LinkEndChild( text );
+
+ dump_to_stdout( &doc );
+ doc.SaveFile( "madeByHand2.xml" );
+ }
+ </pre></div><p>
+Both of these produce the same XML, namely:<p>
+<div class="fragment"><pre class="fragment"> <?xml version="1.0" ?>
+ <Hello>World</Hello>
+ </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->LinkEndChild( text );
+
+ TiXmlElement * element2 = new TiXmlElement( "Greeting" );
+ element->LinkEndChild( element2 );
+
+ TiXmlText * text2 = new TiXmlText( "How are you?" );
+ element2->LinkEndChild( text2 );
+
+ TiXmlElement * element3 = new TiXmlElement( "Language" );
+ element2->LinkEndChild( element3 );
+
+ TiXmlText * text3 = new TiXmlText( "English" );
+ element3->LinkEndChild( text3 );
+
+ TiXmlElement * element4 = new TiXmlElement( "Exclamation" );
+ element->LinkEndChild( element4 );
+
+ TiXmlText * text4 = new TiXmlText( "You have children!" );
+ element4->LinkEndChild( text4 );
+
+ dump_to_stdout( &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"> <?xml version="1.0" ?>
+ <Hello>Opening a new salutation
+ <Greeting>How are you?
+ <Language>English</Language>
+ </Greeting>
+ <Exclamation>You have children!</Exclamation>
+ </Hello>
+ </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 << 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 > LENGTH ) numIndents = LENGTH;
+
+ return &pINDENT[ LENGTH-numIndents ];
+ }
+
+ void dump_to_stdout( TiXmlNode * pParent, unsigned int indent = 0 )
+ {
+ if ( !pParent ) return;
+
+ TiXmlText *pText;
+ int t = pParent->Type();
+ printf( "%s", getIndent( indent));
+
+ switch ( t )
+ {
+ case TiXmlNode::DOCUMENT:
+ printf( "Document" );
+ break;
+
+ case TiXmlNode::ELEMENT:
+ printf( "Element \"%s\"", pParent->Value() );
+ break;
+
+ case TiXmlNode::COMMENT:
+ printf( "Comment: \"%s\"", pParent->Value());
+ break;
+
+ case TiXmlNode::UNKNOWN:
+ printf( "Unknown" );
+ break;
+
+ case TiXmlNode::TEXT:
+ pText = pParent->ToText();
+ printf( "Text: [%s]", pText->Value() );
+ break;
+
+ case TiXmlNode::DECLARATION:
+ printf( "Declaration" );
+ break;
+ default:
+ break;
+ }
printf( "\n" );
- i++;
- pAttrib=pAttrib->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">
-<?xml version="1.0" ?>
-<MyApp>
- <!-- Settings for MyApp -->
- <Messages>
- <Welcome>Welcome to MyApp</Welcome>
- <Farewell>Thank you for using MyApp</Farewell>
- </Messages>
- <Windows>
- <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- </Windows>
- <Connection ip="192.168.0.1" timeout="123.456000" />
-</MyApp>
-</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->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" );
-}
-</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 << 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 <string>
-#include <map>
-using namespace std;
-
-typedef std::map<std::string,std::string> 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& name)
- {
- this->x=x;
- this->y=y;
- this->w=w;
- this->h=h;
- this->name=name;
- }
-};
-
-class ConnectionSettings
-{
-public:
- string ip;
- double timeout;
-};
-
-class AppSettings
-{
-public:
- string m_name;
- MessageMap m_messages;
- list<WindowSettings> m_windows;
- ConnectionSettings m_connection;
-
- AppSettings() {}
-
- void save(const char* pFilename);
- void load(const char* pFilename);
+ TiXmlNode * pChild;
- // 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;
+ for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
+ {
+ dump_to_stdout( pChild, indent+2 );
+ }
}
-};
-</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;
+ </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 );
- 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");
- }
+ TiXmlDocument doc( "demo.xml" );
+ bool loadOkay = doc.LoadFile();
- // 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 & 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->SetValue(s.c_str());
- root->LinkEndChild( comment );
-
- // block: messages
- {
- MessageMap::iterator iter;
-
- TiXmlElement * msgs = new TiXmlElement( "Messages" );
- root->LinkEndChild( msgs );
-
- for (iter=m_messages.begin(); iter != m_messages.end(); iter++)
+ if ( loadOkay )
{
- const string & key=(*iter).first;
- const string & value=(*iter).second;
- msg = new TiXmlElement(key.c_str());
- msg->LinkEndChild( new TiXmlText(value.c_str()));
- msgs->LinkEndChild( msg );
+ dump_to_stdout( &doc );
+ }
+ else
+ {
+ printf( "Something went wrong\n" );
}
}
-
- // block: windows
- {
- TiXmlElement * windowsNode = new TiXmlElement( "Windows" );
- root->LinkEndChild( windowsNode );
-
- list<WindowSettings>::iterator iter;
-
- for (iter=m_windows.begin(); iter != m_windows.end(); iter++)
- {
- const WindowSettings& w=*iter;
-
- TiXmlElement * window;
- window = new TiXmlElement( "Window" );
- windowsNode->LinkEndChild( window );
- window->SetAttribute("name", w.name.c_str());
- window->SetAttribute("x", w.x);
- window->SetAttribute("y", w.y);
- window->SetAttribute("w", w.w);
- window->SetAttribute("h", w.h);
- }
- }
-
- // block: connection
- {
- TiXmlElement * cxn = new TiXmlElement( "Connection" );
- root->LinkEndChild( cxn );
- cxn->SetAttribute("ip", m_connection.ip.c_str());
- cxn->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">
-<?xml version="1.0" ?>
-<HitchHikerApp>
- <!-- Settings for HitchHikerApp -->
- <Messages>
- <Farewell>Thanks for all the fish</Farewell>
- <Welcome>Don&apos;t Panic</Welcome>
- </Messages>
- <Windows>
- <Window name="BookFrame" x="15" y="25" w="300" h="250" />
- </Windows>
- <Connection ip="192.168.0.77" timeout="42.000000" />
-</HitchHikerApp>
-</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(&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->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->NextSiblingElement())
- {
- const char *pKey=pElem->Value();
- const char *pText=pElem->GetText();
- if (pKey && 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->NextSiblingElement())
- {
- WindowSettings w;
- const char *pName=pWindowNode->Attribute("name");
- if (pName) w.name=pName;
-
- pWindowNode->QueryIntAttribute("x", &w.x); // If this fails, original value is left as-is
- pWindowNode->QueryIntAttribute("y", &w.y);
- pWindowNode->QueryIntAttribute("w", &w.w);
- pWindowNode->QueryIntAttribute("hh", &w.h);
-
- m_windows.push_back(w);
- }
- }
-
- // block: connection
- {
- pElem=hRoot.FirstChild("Connection").Element();
- if (pElem)
- {
- m_connection.ip=pElem->Attribute("ip");
- pElem->QueryDoubleAttribute("timeout",&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 > LENGTH ) n = LENGTH;
-
- return &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 > LENGTH ) n = LENGTH;
-
- return &pINDENT[ LENGTH-n ];
-}
-
-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;
-}
-
-void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
-{
- if ( !pParent ) return;
-
- TiXmlNode* pChild;
- TiXmlText* pText;
- int t = pParent->Type();
- printf( "%s", getIndent(indent));
- int num;
-
- switch ( t )
- {
- case TiXmlNode::TINYXML_DOCUMENT:
- printf( "Document" );
- break;
-
- case TiXmlNode::TINYXML_ELEMENT:
- printf( "Element [%s]", pParent->Value() );
- num=dump_attribs_to_stdout(pParent->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->Value());
- break;
-
- case TiXmlNode::TINYXML_UNKNOWN:
- printf( "Unknown" );
- break;
-
- case TiXmlNode::TINYXML_TEXT:
- pText = pParent->ToText();
- printf( "Text: [%s]", pText->Value() );
- break;
-
- case TiXmlNode::TINYXML_DECLARATION:
- printf( "Declaration" );
- break;
- default:
- break;
- }
- printf( "\n" );
- for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->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( &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<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> 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>
+ </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, May, June 2005 </li>
+Written by Ellers, April 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
+</em> <hr size="1"><address style="align: right;"><small>Generated on Sat Oct 8 14:15:30 2005 for TinyXml by
<a href="http://www.doxygen.org/index.html">
-<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
+<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address>
</body>
</html>