1*7485b225SElliott HughesTinyXML-2 2*7485b225SElliott Hughes========= 3*7485b225SElliott Hughes 4*7485b225SElliott Hughes[](https://github.com/leethomason/tinyxml2/actions/workflows/test.yml) 5*7485b225SElliott Hughes 6*7485b225SElliott HughesTinyXML-2 is a simple, small, efficient, C++ XML parser that can be 7*7485b225SElliott Hugheseasily integrated into other programs. 8*7485b225SElliott Hughes 9*7485b225SElliott HughesThe master is hosted on github: 10*7485b225SElliott Hugheshttps://github.com/leethomason/tinyxml2 11*7485b225SElliott Hughes 12*7485b225SElliott HughesThe online HTML version of these docs: 13*7485b225SElliott Hugheshttp://leethomason.github.io/tinyxml2/ 14*7485b225SElliott Hughes 15*7485b225SElliott HughesExamples are in the "related pages" tab of the HTML docs. 16*7485b225SElliott Hughes 17*7485b225SElliott HughesWhat it does. 18*7485b225SElliott Hughes------------- 19*7485b225SElliott Hughes 20*7485b225SElliott HughesIn brief, TinyXML-2 parses an XML document, and builds from that a 21*7485b225SElliott HughesDocument Object Model (DOM) that can be read, modified, and saved. 22*7485b225SElliott Hughes 23*7485b225SElliott HughesXML stands for "eXtensible Markup Language." It is a general purpose 24*7485b225SElliott Hugheshuman and machine readable markup language to describe arbitrary data. 25*7485b225SElliott HughesAll those random file formats created to store application data can 26*7485b225SElliott Hughesall be replaced with XML. One parser for everything. 27*7485b225SElliott Hughes 28*7485b225SElliott Hugheshttp://en.wikipedia.org/wiki/XML 29*7485b225SElliott Hughes 30*7485b225SElliott HughesThere are different ways to access and interact with XML data. 31*7485b225SElliott HughesTinyXML-2 uses a Document Object Model (DOM), meaning the XML data is parsed 32*7485b225SElliott Hughesinto a C++ objects that can be browsed and manipulated, and then 33*7485b225SElliott Hugheswritten to disk or another output stream. You can also construct an XML document 34*7485b225SElliott Hughesfrom scratch with C++ objects and write this to disk or another output 35*7485b225SElliott Hughesstream. You can even use TinyXML-2 to stream XML programmatically from 36*7485b225SElliott Hughescode without creating a document first. 37*7485b225SElliott Hughes 38*7485b225SElliott HughesTinyXML-2 is designed to be easy and fast to learn. It is one header and 39*7485b225SElliott Hughesone cpp file. Simply add these to your project and off you go. 40*7485b225SElliott HughesThere is an example file - xmltest.cpp - to get you started. 41*7485b225SElliott Hughes 42*7485b225SElliott HughesTinyXML-2 is released under the ZLib license, 43*7485b225SElliott Hughesso you can use it in open source or commercial code. The details 44*7485b225SElliott Hughesof the license are at the top of every source file. 45*7485b225SElliott Hughes 46*7485b225SElliott HughesTinyXML-2 attempts to be a flexible parser, but with truly correct and 47*7485b225SElliott Hughescompliant XML output. TinyXML-2 should compile on any reasonably C++ 48*7485b225SElliott Hughescompliant system. It does not rely on exceptions, RTTI, or the STL. 49*7485b225SElliott Hughes 50*7485b225SElliott HughesWhat it doesn't do. 51*7485b225SElliott Hughes------------------- 52*7485b225SElliott Hughes 53*7485b225SElliott HughesTinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs 54*7485b225SElliott Hughes(eXtensible Stylesheet Language.) There are other parsers out there 55*7485b225SElliott Hughesthat are much more fully featured. But they are generally bigger and 56*7485b225SElliott Hughesmore difficult to use. If you are working with 57*7485b225SElliott Hughesbrowsers or have more complete XML needs, TinyXML-2 is not the parser for you. 58*7485b225SElliott Hughes 59*7485b225SElliott HughesTinyXML-1 vs. TinyXML-2 60*7485b225SElliott Hughes----------------------- 61*7485b225SElliott Hughes 62*7485b225SElliott HughesTinyXML-2 long been the focus of all development. It is well tested 63*7485b225SElliott Hughesand should be used instead of TinyXML-1. 64*7485b225SElliott Hughes 65*7485b225SElliott HughesTinyXML-2 uses a similar API to TinyXML-1 and the same 66*7485b225SElliott Hughesrich test cases. But the implementation of the parser is completely re-written 67*7485b225SElliott Hughesto make it more appropriate for use in a game. It uses less memory, is faster, 68*7485b225SElliott Hughesand uses far fewer memory allocations. 69*7485b225SElliott Hughes 70*7485b225SElliott HughesTinyXML-2 has no requirement or support for STL. 71*7485b225SElliott Hughes 72*7485b225SElliott HughesFeatures 73*7485b225SElliott Hughes-------- 74*7485b225SElliott Hughes 75*7485b225SElliott Hughes### Code Page 76*7485b225SElliott Hughes 77*7485b225SElliott HughesTinyXML-2 uses UTF-8 exclusively when interpreting XML. All XML is assumed to 78*7485b225SElliott Hughesbe UTF-8. 79*7485b225SElliott Hughes 80*7485b225SElliott HughesFilenames for loading / saving are passed unchanged to the underlying OS. 81*7485b225SElliott Hughes 82*7485b225SElliott Hughes### Memory Model 83*7485b225SElliott Hughes 84*7485b225SElliott HughesAn XMLDocument is a C++ object like any other, that can be on the stack, or 85*7485b225SElliott Hughesnew'd and deleted on the heap. 86*7485b225SElliott Hughes 87*7485b225SElliott HughesHowever, any sub-node of the Document, XMLElement, XMLText, etc, can only 88*7485b225SElliott Hughesbe created by calling the appropriate XMLDocument::NewElement, NewText, etc. 89*7485b225SElliott Hughesmethod. Although you have pointers to these objects, they are still owned 90*7485b225SElliott Hughesby the Document. When the Document is deleted, so are all the nodes it contains. 91*7485b225SElliott Hughes 92*7485b225SElliott Hughes### White Space 93*7485b225SElliott Hughes 94*7485b225SElliott Hughes#### Whitespace Preservation (default, PRESERVE_WHITESPACE) 95*7485b225SElliott Hughes 96*7485b225SElliott HughesMicrosoft has an excellent article on white space: http://msdn.microsoft.com/en-us/library/ms256097.aspx 97*7485b225SElliott Hughes 98*7485b225SElliott HughesBy default, TinyXML-2 preserves white space in a (hopefully) sane way that is almost compliant with the 99*7485b225SElliott Hughesspec. (TinyXML-1 used a completely different model, much more similar to 'collapse', below.) 100*7485b225SElliott Hughes 101*7485b225SElliott HughesAs a first step, all newlines / carriage-returns / line-feeds are normalized to a 102*7485b225SElliott Hughesline-feed character, as required by the XML spec. 103*7485b225SElliott Hughes 104*7485b225SElliott HughesWhite space in text is preserved. For example: 105*7485b225SElliott Hughes 106*7485b225SElliott Hughes <element> Hello, World</element> 107*7485b225SElliott Hughes 108*7485b225SElliott HughesThe leading space before the "Hello" and the double space after the comma are 109*7485b225SElliott Hughespreserved. Line-feeds are preserved, as in this example: 110*7485b225SElliott Hughes 111*7485b225SElliott Hughes <element> Hello again, 112*7485b225SElliott Hughes World</element> 113*7485b225SElliott Hughes 114*7485b225SElliott HughesHowever, white space between elements is **not** preserved. Although not strictly 115*7485b225SElliott Hughescompliant, tracking and reporting inter-element space is awkward, and not normally 116*7485b225SElliott Hughesvaluable. TinyXML-2 sees these as the same XML: 117*7485b225SElliott Hughes 118*7485b225SElliott Hughes <document> 119*7485b225SElliott Hughes <data>1</data> 120*7485b225SElliott Hughes <data>2</data> 121*7485b225SElliott Hughes <data>3</data> 122*7485b225SElliott Hughes </document> 123*7485b225SElliott Hughes 124*7485b225SElliott Hughes <document><data>1</data><data>2</data><data>3</data></document> 125*7485b225SElliott Hughes 126*7485b225SElliott Hughes#### Whitespace Collapse (COLLAPSE_WHITESPACE) 127*7485b225SElliott Hughes 128*7485b225SElliott HughesFor some applications, it is preferable to collapse whitespace. Collapsing 129*7485b225SElliott Hugheswhitespace gives you "HTML-like" behavior, which is sometimes more suitable 130*7485b225SElliott Hughesfor hand typed documents. 131*7485b225SElliott Hughes 132*7485b225SElliott HughesTinyXML-2 supports this with the 'whitespace' parameter to the XMLDocument constructor. 133*7485b225SElliott Hughes(The default is to preserve whitespace, as described above.) 134*7485b225SElliott Hughes 135*7485b225SElliott HughesHowever, you may also use COLLAPSE_WHITESPACE, which will: 136*7485b225SElliott Hughes 137*7485b225SElliott Hughes* Remove leading and trailing whitespace 138*7485b225SElliott Hughes* Convert newlines and line-feeds into a space character 139*7485b225SElliott Hughes* Collapse a run of any number of space characters into a single space character 140*7485b225SElliott Hughes 141*7485b225SElliott HughesNote that (currently) there is a performance impact for using COLLAPSE_WHITESPACE. 142*7485b225SElliott HughesIt essentially causes the XML to be parsed twice. 143*7485b225SElliott Hughes 144*7485b225SElliott Hughes#### Pedantic Whitespace (PEDANTIC_WHITESPACE) 145*7485b225SElliott Hughes 146*7485b225SElliott HughesFor applications that need to know about text nodes that are composed entirely of 147*7485b225SElliott Hugheswhitespace, PEDANTIC_WHITESPACE is available. PEDANTIC_WHITESPACE maintains all the 148*7485b225SElliott Hugheswhilespace between elements. 149*7485b225SElliott Hughes 150*7485b225SElliott HughesPEDANTIC_WHITESPACE is a new mode and not as tested as the other whitespace modes. 151*7485b225SElliott Hughes 152*7485b225SElliott Hughes### Error Reporting 153*7485b225SElliott Hughes 154*7485b225SElliott HughesTinyXML-2 reports the line number of any errors in an XML document that 155*7485b225SElliott Hughescannot be parsed correctly. In addition, all nodes (elements, declarations, 156*7485b225SElliott Hughestext, comments etc.) and attributes have a line number recorded as they are parsed. 157*7485b225SElliott HughesThis allows an application that performs additional validation of the parsed 158*7485b225SElliott HughesXML document (e.g. application-implemented DTD validation) to report 159*7485b225SElliott Hughesline number information for error messages. 160*7485b225SElliott Hughes 161*7485b225SElliott Hughes### Entities 162*7485b225SElliott Hughes 163*7485b225SElliott HughesTinyXML-2 recognizes the pre-defined "character entities", meaning special 164*7485b225SElliott Hughescharacters. Namely: 165*7485b225SElliott Hughes 166*7485b225SElliott Hughes & & 167*7485b225SElliott Hughes < < 168*7485b225SElliott Hughes > > 169*7485b225SElliott Hughes " " 170*7485b225SElliott Hughes ' ' 171*7485b225SElliott Hughes 172*7485b225SElliott HughesThese are recognized when the XML document is read, and translated to their 173*7485b225SElliott HughesUTF-8 equivalents. For instance, text with the XML of: 174*7485b225SElliott Hughes 175*7485b225SElliott Hughes Far & Away 176*7485b225SElliott Hughes 177*7485b225SElliott Hugheswill have the Value() of "Far & Away" when queried from the XMLText object, 178*7485b225SElliott Hughesand will be written back to the XML stream/file as an ampersand. 179*7485b225SElliott Hughes 180*7485b225SElliott HughesAdditionally, any character can be specified by its Unicode code point: 181*7485b225SElliott HughesThe syntax ` ` or ` ` are both to the non-breaking space character. 182*7485b225SElliott HughesThis is called a 'numeric character reference'. Any numeric character reference 183*7485b225SElliott Hughesthat isn't one of the special entities above, will be read, but written as a 184*7485b225SElliott Hughesregular code point. The output is correct, but the entity syntax isn't preserved. 185*7485b225SElliott Hughes 186*7485b225SElliott Hughes### Printing 187*7485b225SElliott Hughes 188*7485b225SElliott Hughes#### Print to file 189*7485b225SElliott HughesYou can directly use the convenience function: 190*7485b225SElliott Hughes 191*7485b225SElliott Hughes XMLDocument doc; 192*7485b225SElliott Hughes ... 193*7485b225SElliott Hughes doc.SaveFile( "foo.xml" ); 194*7485b225SElliott Hughes 195*7485b225SElliott HughesOr the XMLPrinter class: 196*7485b225SElliott Hughes 197*7485b225SElliott Hughes XMLPrinter printer( fp ); 198*7485b225SElliott Hughes doc.Print( &printer ); 199*7485b225SElliott Hughes 200*7485b225SElliott Hughes#### Print to memory 201*7485b225SElliott HughesPrinting to memory is supported by the XMLPrinter. 202*7485b225SElliott Hughes 203*7485b225SElliott Hughes XMLPrinter printer; 204*7485b225SElliott Hughes doc.Print( &printer ); 205*7485b225SElliott Hughes // printer.CStr() has a const char* to the XML 206*7485b225SElliott Hughes 207*7485b225SElliott Hughes#### Print without an XMLDocument 208*7485b225SElliott Hughes 209*7485b225SElliott HughesWhen loading, an XML parser is very useful. However, sometimes 210*7485b225SElliott Hugheswhen saving, it just gets in the way. The code is often set up 211*7485b225SElliott Hughesfor streaming, and constructing the DOM is just overhead. 212*7485b225SElliott Hughes 213*7485b225SElliott HughesThe Printer supports the streaming case. The following code 214*7485b225SElliott Hughesprints out a trivially simple XML file without ever creating 215*7485b225SElliott Hughesan XML document. 216*7485b225SElliott Hughes 217*7485b225SElliott Hughes XMLPrinter printer( fp ); 218*7485b225SElliott Hughes printer.OpenElement( "foo" ); 219*7485b225SElliott Hughes printer.PushAttribute( "foo", "bar" ); 220*7485b225SElliott Hughes printer.CloseElement(); 221*7485b225SElliott Hughes 222*7485b225SElliott HughesExamples 223*7485b225SElliott Hughes-------- 224*7485b225SElliott Hughes 225*7485b225SElliott Hughes#### Load and parse an XML file. 226*7485b225SElliott Hughes 227*7485b225SElliott Hughes /* ------ Example 1: Load and parse an XML file. ---- */ 228*7485b225SElliott Hughes { 229*7485b225SElliott Hughes XMLDocument doc; 230*7485b225SElliott Hughes doc.LoadFile( "dream.xml" ); 231*7485b225SElliott Hughes } 232*7485b225SElliott Hughes 233*7485b225SElliott Hughes#### Lookup information. 234*7485b225SElliott Hughes 235*7485b225SElliott Hughes /* ------ Example 2: Lookup information. ---- */ 236*7485b225SElliott Hughes { 237*7485b225SElliott Hughes XMLDocument doc; 238*7485b225SElliott Hughes doc.LoadFile( "dream.xml" ); 239*7485b225SElliott Hughes 240*7485b225SElliott Hughes // Structure of the XML file: 241*7485b225SElliott Hughes // - Element "PLAY" the root Element, which is the 242*7485b225SElliott Hughes // FirstChildElement of the Document 243*7485b225SElliott Hughes // - - Element "TITLE" child of the root PLAY Element 244*7485b225SElliott Hughes // - - - Text child of the TITLE Element 245*7485b225SElliott Hughes 246*7485b225SElliott Hughes // Navigate to the title, using the convenience function, 247*7485b225SElliott Hughes // with a dangerous lack of error checking. 248*7485b225SElliott Hughes const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText(); 249*7485b225SElliott Hughes printf( "Name of play (1): %s\n", title ); 250*7485b225SElliott Hughes 251*7485b225SElliott Hughes // Text is just another Node to TinyXML-2. The more 252*7485b225SElliott Hughes // general way to get to the XMLText: 253*7485b225SElliott Hughes XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText(); 254*7485b225SElliott Hughes title = textNode->Value(); 255*7485b225SElliott Hughes printf( "Name of play (2): %s\n", title ); 256*7485b225SElliott Hughes } 257*7485b225SElliott Hughes 258*7485b225SElliott HughesUsing and Installing 259*7485b225SElliott Hughes-------------------- 260*7485b225SElliott Hughes 261*7485b225SElliott HughesThere are 2 files in TinyXML-2: 262*7485b225SElliott Hughes* tinyxml2.cpp 263*7485b225SElliott Hughes* tinyxml2.h 264*7485b225SElliott Hughes 265*7485b225SElliott HughesAnd additionally a test file: 266*7485b225SElliott Hughes* xmltest.cpp 267*7485b225SElliott Hughes 268*7485b225SElliott HughesGenerally speaking, the intent is that you simply include the tinyxml2.cpp and 269*7485b225SElliott Hughestinyxml2.h files in your project and build with your other source code. 270*7485b225SElliott Hughes 271*7485b225SElliott HughesThere is also a CMake build included. CMake is the general build for TinyXML-2. 272*7485b225SElliott Hughes 273*7485b225SElliott Hughes(Additional build systems are costly to maintain, and tend to bit-rot. They are 274*7485b225SElliott Hughesbeing removed over time.) 275*7485b225SElliott Hughes 276*7485b225SElliott HughesBuilding TinyXML-2 - Using vcpkg 277*7485b225SElliott Hughes-------------------------------- 278*7485b225SElliott Hughes 279*7485b225SElliott HughesYou can download and install TinyXML-2 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager: 280*7485b225SElliott Hughes 281*7485b225SElliott Hughes git clone https://github.com/Microsoft/vcpkg.git 282*7485b225SElliott Hughes cd vcpkg 283*7485b225SElliott Hughes ./bootstrap-vcpkg.sh 284*7485b225SElliott Hughes ./vcpkg integrate install 285*7485b225SElliott Hughes ./vcpkg install tinyxml2 286*7485b225SElliott Hughes 287*7485b225SElliott HughesThe TinyXML-2 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository. 288*7485b225SElliott Hughes 289*7485b225SElliott HughesVersioning 290*7485b225SElliott Hughes---------- 291*7485b225SElliott Hughes 292*7485b225SElliott HughesTinyXML-2 uses semantic versioning. http://semver.org/ Releases are now tagged in github. 293*7485b225SElliott Hughes 294*7485b225SElliott HughesNote that the major version will (probably) change fairly rapidly. API changes are fairly 295*7485b225SElliott Hughescommon. 296*7485b225SElliott Hughes 297*7485b225SElliott HughesLicense 298*7485b225SElliott Hughes------- 299*7485b225SElliott Hughes 300*7485b225SElliott HughesTinyXML-2 is released under the zlib license: 301*7485b225SElliott Hughes 302*7485b225SElliott HughesThis software is provided 'as-is', without any express or implied 303*7485b225SElliott Hugheswarranty. In no event will the authors be held liable for any 304*7485b225SElliott Hughesdamages arising from the use of this software. 305*7485b225SElliott Hughes 306*7485b225SElliott HughesPermission is granted to anyone to use this software for any 307*7485b225SElliott Hughespurpose, including commercial applications, and to alter it and 308*7485b225SElliott Hughesredistribute it freely, subject to the following restrictions: 309*7485b225SElliott Hughes 310*7485b225SElliott Hughes1. The origin of this software must not be misrepresented; you must 311*7485b225SElliott Hughesnot claim that you wrote the original software. If you use this 312*7485b225SElliott Hughessoftware in a product, an acknowledgment in the product documentation 313*7485b225SElliott Hugheswould be appreciated but is not required. 314*7485b225SElliott Hughes2. Altered source versions must be plainly marked as such, and 315*7485b225SElliott Hughesmust not be misrepresented as being the original software. 316*7485b225SElliott Hughes3. This notice may not be removed or altered from any source 317*7485b225SElliott Hughesdistribution. 318*7485b225SElliott Hughes 319*7485b225SElliott HughesContributors 320*7485b225SElliott Hughes------------ 321*7485b225SElliott Hughes 322*7485b225SElliott HughesThanks very much to everyone who sends suggestions, bugs, ideas, and 323*7485b225SElliott Hughesencouragement. It all helps, and makes this project fun. 324*7485b225SElliott Hughes 325*7485b225SElliott HughesThe original TinyXML-1 has many contributors, who all deserve thanks 326*7485b225SElliott Hughesin shaping what is a very successful library. Extra thanks to Yves 327*7485b225SElliott HughesBerquin and Andrew Ellerton who were key contributors. 328*7485b225SElliott Hughes 329*7485b225SElliott HughesTinyXML-2 grew from that effort. Lee Thomason is the original author 330*7485b225SElliott Hughesof TinyXML-2 (and TinyXML-1) but TinyXML-2 has been and is being improved 331*7485b225SElliott Hughesby many contributors. 332*7485b225SElliott Hughes 333*7485b225SElliott HughesThanks to John Mackay at http://john.mackay.rosalilastudio.com for the TinyXML-2 logo! 334*7485b225SElliott Hughes 335*7485b225SElliott Hughes 336