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