• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<meta http-equiv="X-UA-Compatible" content="IE=9"/>
6<meta name="generator" content="Doxygen 1.8.13"/>
7<meta name="viewport" content="width=device-width, initial-scale=1"/>
8<title>TinyXML-2: TinyXML-2</title>
9<link href="tabs.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="dynsections.js"></script>
12<link href="search/search.css" rel="stylesheet" type="text/css"/>
13<script type="text/javascript" src="search/searchdata.js"></script>
14<script type="text/javascript" src="search/search.js"></script>
15<link href="doxygen.css" rel="stylesheet" type="text/css" />
16</head>
17<body>
18<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
19<div id="titlearea">
20<table cellspacing="0" cellpadding="0">
21 <tbody>
22 <tr style="height: 56px;">
23  <td id="projectalign" style="padding-left: 0.5em;">
24   <div id="projectname">TinyXML-2
25   &#160;<span id="projectnumber">7.0.0</span>
26   </div>
27  </td>
28 </tr>
29 </tbody>
30</table>
31</div>
32<!-- end header part -->
33<!-- Generated by Doxygen 1.8.13 -->
34<script type="text/javascript">
35var searchBox = new SearchBox("searchBox", "search",false,'Search');
36</script>
37<script type="text/javascript" src="menudata.js"></script>
38<script type="text/javascript" src="menu.js"></script>
39<script type="text/javascript">
40$(function() {
41  initMenu('',true,false,'search.php','Search');
42  $(document).ready(function() { init_search(); });
43});
44</script>
45<div id="main-nav"></div>
46</div><!-- top -->
47<!-- window showing the filter options -->
48<div id="MSearchSelectWindow"
49     onmouseover="return searchBox.OnSearchSelectShow()"
50     onmouseout="return searchBox.OnSearchSelectHide()"
51     onkeydown="return searchBox.OnSearchSelectKey(event)">
52</div>
53
54<!-- iframe showing the search results (closed by default) -->
55<div id="MSearchResultsWindow">
56<iframe src="javascript:void(0)" frameborder="0"
57        name="MSearchResults" id="MSearchResults">
58</iframe>
59</div>
60
61<div class="header">
62  <div class="headertitle">
63<div class="title">TinyXML-2 </div>  </div>
64</div><!--header-->
65<div class="contents">
66<div class="textblock"><p><a href="https://travis-ci.org/leethomason/tinyxml2"></a> <a href="https://ci.appveyor.com/project/leethomason/tinyxml2"></a></p>
67<div class="image">
68<img src="http://www.grinninglizard.com/tinyxml2/TinyXML2_small.png" alt="TinyXML-2 Logo"/>
69</div>
70<p>TinyXML-2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.</p>
71<p>The master is hosted on github: <a href="https://github.com/leethomason/tinyxml2">https://github.com/leethomason/tinyxml2</a></p>
72<p>The online HTML version of these docs: <a href="http://leethomason.github.io/tinyxml2/">http://leethomason.github.io/tinyxml2/</a></p>
73<p>Examples are in the "related pages" tab of the HTML docs.</p>
74<h2>What it does. </h2>
75<p>In brief, TinyXML-2 parses an XML document, and builds from that a Document Object Model (DOM) that can be read, modified, and saved.</p>
76<p>XML stands for "eXtensible Markup Language." It is a general purpose human and machine readable markup language to describe arbitrary data. All those random file formats created to store application data can all be replaced with XML. One parser for everything.</p>
77<p><a href="http://en.wikipedia.org/wiki/XML">http://en.wikipedia.org/wiki/XML</a></p>
78<p>There are different ways to access and interact with XML data. TinyXML-2 uses a Document Object Model (DOM), meaning the XML data is parsed into a C++ objects that can be browsed and manipulated, and then written to disk or another output stream. You can also construct an XML document from scratch with C++ objects and write this to disk or another output stream. You can even use TinyXML-2 to stream XML programmatically from code without creating a document first.</p>
79<p>TinyXML-2 is designed to be easy and fast to learn. It is one header and one cpp file. Simply add these to your project and off you go. There is an example file - xmltest.cpp - to get you started.</p>
80<p>TinyXML-2 is released under the ZLib license, so you can use it in open source or commercial code. The details of the license are at the top of every source file.</p>
81<p>TinyXML-2 attempts to be a flexible parser, but with truly correct and compliant XML output. TinyXML-2 should compile on any reasonably C++ compliant system. It does not rely on exceptions, RTTI, or the STL.</p>
82<h2>What it doesn't do. </h2>
83<p>TinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs (eXtensible Stylesheet Language.) There are other parsers out there that are much more fully featured. But they are also much bigger, take longer to set up in your project, have a higher learning curve, and often have a more restrictive license. If you are working with browsers or have more complete XML needs, TinyXML-2 is not the parser for you.</p>
84<h2>TinyXML-1 vs. TinyXML-2 </h2>
85<p>TinyXML-2 is now the focus of all development, well tested, and your best choice between the two APIs. At this point, unless you are maintaining legacy code, you should choose TinyXML-2.</p>
86<p>TinyXML-2 uses a similar API to TinyXML-1 and the same rich test cases. But the implementation of the parser is completely re-written to make it more appropriate for use in a game. It uses less memory, is faster, and uses far fewer memory allocations.</p>
87<p>TinyXML-2 has no requirement or support for STL. By returning <code>const char*</code> TinyXML-2 can be much more efficient with memory usage. (TinyXML-1 did support and use STL, but consumed much more memory for the DOM representation.)</p>
88<h2>Features </h2>
89<h3>Code Page</h3>
90<p>TinyXML-2 uses UTF-8 exclusively when interpreting XML. All XML is assumed to be UTF-8.</p>
91<p>Filenames for loading / saving are passed unchanged to the underlying OS.</p>
92<h3>Memory Model</h3>
93<p>An XMLDocument is a C++ object like any other, that can be on the stack, or new'd and deleted on the heap.</p>
94<p>However, any sub-node of the Document, XMLElement, XMLText, etc, can only be created by calling the appropriate XMLDocument::NewElement, NewText, etc. method. Although you have pointers to these objects, they are still owned by the Document. When the Document is deleted, so are all the nodes it contains.</p>
95<h3>White Space</h3>
96<h4>Whitespace Preservation (default)</h4>
97<p>Microsoft has an excellent article on white space: <a href="http://msdn.microsoft.com/en-us/library/ms256097.aspx">http://msdn.microsoft.com/en-us/library/ms256097.aspx</a></p>
98<p>By default, TinyXML-2 preserves white space in a (hopefully) sane way that is almost compliant with the spec. (TinyXML-1 used a completely different model, much more similar to 'collapse', below.)</p>
99<p>As a first step, all newlines / carriage-returns / line-feeds are normalized to a line-feed character, as required by the XML spec.</p>
100<p>White space in text is preserved. For example: </p><pre class="fragment">&lt;element&gt; Hello,  World&lt;/element&gt;
101</pre><p>The leading space before the "Hello" and the double space after the comma are preserved. Line-feeds are preserved, as in this example: </p><pre class="fragment">&lt;element&gt; Hello again,
102          World&lt;/element&gt;
103</pre><p>However, white space between elements is <b>not</b> preserved. Although not strictly compliant, tracking and reporting inter-element space is awkward, and not normally valuable. TinyXML-2 sees these as the same XML: </p><pre class="fragment">&lt;document&gt;
104    &lt;data&gt;1&lt;/data&gt;
105    &lt;data&gt;2&lt;/data&gt;
106    &lt;data&gt;3&lt;/data&gt;
107&lt;/document&gt;
108
109&lt;document&gt;&lt;data&gt;1&lt;/data&gt;&lt;data&gt;2&lt;/data&gt;&lt;data&gt;3&lt;/data&gt;&lt;/document&gt;
110</pre><h4>Whitespace Collapse</h4>
111<p>For some applications, it is preferable to collapse whitespace. Collapsing whitespace gives you "HTML-like" behavior, which is sometimes more suitable for hand typed documents.</p>
112<p>TinyXML-2 supports this with the 'whitespace' parameter to the XMLDocument constructor. (The default is to preserve whitespace, as described above.)</p>
113<p>However, you may also use COLLAPSE_WHITESPACE, which will:</p>
114<ul>
115<li>Remove leading and trailing whitespace</li>
116<li>Convert newlines and line-feeds into a space character</li>
117<li>Collapse a run of any number of space characters into a single space character</li>
118</ul>
119<p>Note that (currently) there is a performance impact for using COLLAPSE_WHITESPACE. It essentially causes the XML to be parsed twice.</p>
120<h4>Error Reporting</h4>
121<p>TinyXML-2 reports the line number of any errors in an XML document that cannot be parsed correctly. In addition, all nodes (elements, declarations, text, comments etc.) and attributes have a line number recorded as they are parsed. This allows an application that performs additional validation of the parsed XML document (e.g. application-implemented DTD validation) to report line number information for error messages.</p>
122<h3>Entities</h3>
123<p>TinyXML-2 recognizes the pre-defined "character entities", meaning special characters. Namely: </p><pre class="fragment">&amp;amp;   &amp;
124&amp;lt;    &lt;
125&amp;gt;    &gt;
126&amp;quot;  "
127&amp;apos;  '
128</pre><p>These are recognized when the XML document is read, and translated to their UTF-8 equivalents. For instance, text with the XML of: </p><pre class="fragment">Far &amp;amp; Away
129</pre><p>will have the Value() of "Far &amp; Away" when queried from the XMLText object, and will be written back to the XML stream/file as an ampersand.</p>
130<p>Additionally, any character can be specified by its Unicode code point: The syntax <code>&amp;#xA0;</code> or <code>&amp;#160;</code> are both to the non-breaking space character. This is called a 'numeric character reference'. Any numeric character reference that isn't one of the special entities above, will be read, but written as a regular code point. The output is correct, but the entity syntax isn't preserved.</p>
131<h3>Printing</h3>
132<h4>Print to file</h4>
133<p>You can directly use the convenience function: </p><pre class="fragment">XMLDocument doc;
134...
135doc.SaveFile( "foo.xml" );
136</pre><p>Or the XMLPrinter class: </p><pre class="fragment">XMLPrinter printer( fp );
137doc.Print( &amp;printer );
138</pre><h4>Print to memory</h4>
139<p>Printing to memory is supported by the XMLPrinter. </p><pre class="fragment">XMLPrinter printer;
140doc.Print( &amp;printer );
141// printer.CStr() has a const char* to the XML
142</pre><h4>Print without an XMLDocument</h4>
143<p>When loading, an XML parser is very useful. However, sometimes when saving, it just gets in the way. The code is often set up for streaming, and constructing the DOM is just overhead.</p>
144<p>The Printer supports the streaming case. The following code prints out a trivially simple XML file without ever creating an XML document. </p><pre class="fragment">XMLPrinter printer( fp );
145printer.OpenElement( "foo" );
146printer.PushAttribute( "foo", "bar" );
147printer.CloseElement();
148</pre><h2>Examples </h2>
149<h4>Load and parse an XML file.</h4>
150<pre class="fragment">/* ------ Example 1: Load and parse an XML file. ---- */
151{
152    XMLDocument doc;
153    doc.LoadFile( "dream.xml" );
154}
155</pre><h4>Lookup information.</h4>
156<pre class="fragment">/* ------ Example 2: Lookup information. ---- */
157{
158    XMLDocument doc;
159    doc.LoadFile( "dream.xml" );
160
161    // Structure of the XML file:
162    // - Element "PLAY"      the root Element, which is the
163    //                       FirstChildElement of the Document
164    // - - Element "TITLE"   child of the root PLAY Element
165    // - - - Text            child of the TITLE Element
166
167    // Navigate to the title, using the convenience function,
168    // with a dangerous lack of error checking.
169    const char* title = doc.FirstChildElement( "PLAY" )-&gt;FirstChildElement( "TITLE" )-&gt;GetText();
170    printf( "Name of play (1): %s\n", title );
171
172    // Text is just another Node to TinyXML-2. The more
173    // general way to get to the XMLText:
174    XMLText* textNode = doc.FirstChildElement( "PLAY" )-&gt;FirstChildElement( "TITLE" )-&gt;FirstChild()-&gt;ToText();
175    title = textNode-&gt;Value();
176    printf( "Name of play (2): %s\n", title );
177}
178</pre><h2>Using and Installing </h2>
179<p>There are 2 files in TinyXML-2:</p><ul>
180<li>tinyxml2.cpp</li>
181<li><a class="el" href="tinyxml2_8h_source.html">tinyxml2.h</a></li>
182</ul>
183<p>And additionally a test file:</p><ul>
184<li>xmltest.cpp</li>
185</ul>
186<p>Simply compile and run. There is a visual studio 2017 project included, a simple Makefile, an Xcode project, a Code::Blocks project, and a cmake CMakeLists.txt included to help you. The top of tinyxml.h even has a simple g++ command line if you are are Unix/Linuk/BSD and don't want to use a build system.</p>
187<h2>Versioning </h2>
188<p>TinyXML-2 uses semantic versioning. <a href="http://semver.org/">http://semver.org/</a> Releases are now tagged in github.</p>
189<p>Note that the major version will (probably) change fairly rapidly. API changes are fairly common.</p>
190<h2>Documentation </h2>
191<p>The documentation is build with Doxygen, using the 'dox' configuration file.</p>
192<h2>License </h2>
193<p>TinyXML-2 is released under the zlib license:</p>
194<p>This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.</p>
195<p>Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:</p>
196<ol type="1">
197<li>The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.</li>
198<li>Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.</li>
199<li>This notice may not be removed or altered from any source distribution.</li>
200</ol>
201<h2>Contributors </h2>
202<p>Thanks very much to everyone who sends suggestions, bugs, ideas, and encouragement. It all helps, and makes this project fun.</p>
203<p>The original TinyXML-1 has many contributors, who all deserve thanks in shaping what is a very successful library. Extra thanks to Yves Berquin and Andrew Ellerton who were key contributors.</p>
204<p>TinyXML-2 grew from that effort. Lee Thomason is the original author of TinyXML-2 (and TinyXML-1) but TinyXML-2 has been and is being improved by many contributors.</p>
205<p>Thanks to John Mackay at <a href="http://john.mackay.rosalilastudio.com">http://john.mackay.rosalilastudio.com</a> for the TinyXML-2 logo! </p>
206</div></div><!-- contents -->
207<!-- start footer part -->
208<hr class="footer"/><address class="footer"><small>
209Generated on Tue Nov 6 2018 09:38:26 for TinyXML-2 by &#160;<a href="http://www.doxygen.org/index.html">
210<img class="footer" src="doxygen.png" alt="doxygen"/>
211</a> 1.8.13
212</small></address>
213</body>
214</html>
215