readme.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /** @mainpage
  2. <h1> TinyXML-2 </h1>
  3. TinyXML is a simple, small, efficient, C++ XML parser that can be
  4. easily integrated into other programs.
  5. The master is hosted on github:
  6. github.com/leethomason/tinyxml2
  7. The online HTML version of these docs:
  8. http://grinninglizard.com/tinyxml2docs/index.html
  9. Where examples are in the "related pages" tab:
  10. http://grinninglizard.com/tinyxml2docs/pages.html
  11. <h2> What it does. </h2>
  12. In brief, TinyXML parses an XML document, and builds from that a
  13. Document Object Model (DOM) that can be read, modified, and saved.
  14. XML stands for "eXtensible Markup Language." It is a general purpose
  15. human and machine readable markup language to describe arbitrary data.
  16. All those random file formats created to store application data can
  17. all be replaced with XML. One parser for everything.
  18. http://en.wikipedia.org/wiki/XML
  19. There are different ways to access and interact with XML data.
  20. TinyXML-2 uses a Document Object Model (DOM), meaning the XML data is parsed
  21. into a C++ objects that can be browsed and manipulated, and then
  22. written to disk or another output stream. You can also construct an XML document
  23. from scratch with C++ objects and write this to disk or another output
  24. stream. You can even use TinyXML-2 to stream XML programmatically from
  25. code without creating a document first.
  26. TinyXML-2 is designed to be easy and fast to learn. It is one header and
  27. one cpp file. Simply add these to your project and off you go.
  28. There is an example file - xmltest.cpp - to get you started.
  29. TinyXML-2 is released under the ZLib license,
  30. so you can use it in open source or commercial code. The details
  31. of the license are at the top of every source file.
  32. TinyXML-2 attempts to be a flexible parser, but with truly correct and
  33. compliant XML output. TinyXML-2 should compile on any reasonably C++
  34. compliant system. It does not rely on exceptions, RTTI, or the STL.
  35. <h2> What it doesn't do. </h2>
  36. TinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs
  37. (eXtensible Stylesheet Language.) There are other parsers out there
  38. that are much more fully
  39. featured. But they are also much bigger, take longer to set up in
  40. your project, have a higher learning curve, and often have a more
  41. restrictive license. If you are working with browsers or have more
  42. complete XML needs, TinyXML-2 is not the parser for you.
  43. <h2> TinyXML-1 vs. TinyXML-2 </h2>
  44. Which should you use? TinyXML-2 uses a similar API to TinyXML-1 and the same
  45. rich test cases. But the implementation of the parser is completely re-written
  46. to make it more appropriate for use in a game. It uses less memory, is faster,
  47. and uses far few memory allocations.
  48. TinyXML-2 has no requirement for STL, but has also dropped all STL support. All
  49. strings are query and set as 'const char*'. This allows the use of internal
  50. allocators, and keeps the code much simpler.
  51. Both parsers:
  52. <ol>
  53. <li>Simple to use with similar APIs.</li>
  54. <li>DOM based parser.</li>
  55. <li>UTF-8 Unicode support. http://en.wikipedia.org/wiki/UTF-8 </li>
  56. </ol>
  57. Advantages of TinyXML-2
  58. <ol>
  59. <li>The focus of all future dev.</li>
  60. <li>Many fewer memory allocation (1/10th to 1/100th), uses less memory (about 40% of TinyXML-1), and faster.</li>
  61. <li>No STL requirement.</li>
  62. <li>More modern C++, including a proper namespace.</li>
  63. <li>Proper and useful handling of whitespace</li>
  64. </ol>
  65. Advantages of TinyXML-1
  66. <ol>
  67. <li>Can report the location of parsing errors.</li>
  68. <li>Support for some C++ STL conventions: streams and strings</li>
  69. <li>Very mature and well debugged code base.</li>
  70. </ol>
  71. <h2> Features </h2>
  72. <h3> Memory Model </h3>
  73. An XMLDocument is a C++ object like any other, that can be on the stack, or
  74. new'd and deleted on the heap.
  75. However, any sub-node of the Document, XMLElement, XMLText, etc, can only
  76. be created by calling the appropriate XMLDocument::NewElement, NewText, etc.
  77. method. Although you have pointers to these objects, they are still owned
  78. by the Document. When the Document is deleted, so are all the nodes it contains.
  79. <h3> White Space </h3>
  80. Microsoft has an excellent article on white space: http://msdn.microsoft.com/en-us/library/ms256097.aspx
  81. TinyXML-2 preserves white space in a (hopefully) sane way that is almost complient with the
  82. spec.(TinyXML-1 used a completely outdated model.)
  83. As a first step, all newlines / carriage-returns / line-feeds are normalized to a
  84. line-feed character, as required by the XML spec.
  85. White space in text is preserved. For example:
  86. <element> Hello, World</element>
  87. The leading space before the "Hello" and the double space after the comma are
  88. preserved. Line-feeds are preserved, as in this example:
  89. <element> Hello again,
  90. World</element>
  91. However, white space between elements is *not* preserved. Although not strictly
  92. compliant, tracking and reporting inter-element space is awkward, and not normally
  93. valuable. TinyXML-2 sees these as the same XML:
  94. <document>
  95. <data>1</data>
  96. <data>2</data>
  97. <data>3</data>
  98. </document>
  99. <document><data>1</data><data>2</data><data>3</data></document>
  100. <h3> Entities </h3>
  101. TinyXML-2 recognizes the pre-defined "character entities", meaning special
  102. characters. Namely:
  103. &amp; &
  104. &lt; <
  105. &gt; >
  106. &quot; "
  107. &apos; '
  108. These are recognized when the XML document is read, and translated to there
  109. UTF-8 equivalents. For instance, text with the XML of:
  110. Far &amp; Away
  111. will have the Value() of "Far & Away" when queried from the XMLText object,
  112. and will be written back to the XML stream/file as an ampersand.
  113. Additionally, any character can be specified by its Unicode code point:
  114. The syntax "&#xA0;" or "&#160;" are both to the non-breaking space characher.
  115. This is called a 'numeric character reference'. Any numeric character reference
  116. that isn't one of the special entities above, will be read, but written as a
  117. regular code point. The output is correct, but the entity syntax isn't preserved.
  118. <h3> Printing </h3>
  119. <h4> Print to file </h4>
  120. You can directly use the convenience function:
  121. XMLDocument doc;
  122. ...
  123. doc.Save( "foo.xml" );
  124. Or the XMLPrinter class:
  125. XMLPrinter printer( fp );
  126. doc.Print( &printer );
  127. <h4> Print to memory </h4>
  128. Printing to memory is supported by the XMLPrinter.
  129. XMLPrinter printer;
  130. doc->Print( &printer );
  131. // printer.CStr() has a const char* to the XML
  132. <h4> Print without an XMLDocument </h4>
  133. When loading, an XML parser is very useful. However, sometimes
  134. when saving, it just gets in the way. The code is often set up
  135. for streaming, and constructing the DOM is just overhead.
  136. The Printer supports the streaming case. The following code
  137. prints out a trivially simple XML file without ever creating
  138. an XML document.
  139. XMLPrinter printer( fp );
  140. printer.OpenElement( "foo" );
  141. printer.PushAttribute( "foo", "bar" );
  142. printer.CloseElement();
  143. <h2> Examples </h2>
  144. <h4> Load and parse an XML file. </h4>
  145. @verbatim
  146. /* ------ Example 1: Load and parse an XML file. ---- */
  147. {
  148. XMLDocument doc;
  149. doc.LoadFile( "dream.xml" );
  150. }
  151. @endverbatim
  152. <h4> Lookup information. </h4>
  153. @verbatim
  154. /* ------ Example 2: Lookup information. ---- */
  155. {
  156. XMLDocument doc;
  157. doc.LoadFile( "dream.xml" );
  158. // Structure of the XML file:
  159. // - Element "PLAY" the root Element, which is the
  160. // FirstChildElement of the Document
  161. // - - Element "TITLE" child of the root PLAY Element
  162. // - - - Text child of the TITLE Element
  163. // Navigate to the title, using the convenience function,
  164. // with a dangerous lack of error checking.
  165. const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
  166. printf( "Name of play (1): %s\n", title );
  167. // Text is just another Node to TinyXML-2. The more
  168. // general way to get to the XMLText:
  169. XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
  170. title = textNode->Value();
  171. printf( "Name of play (2): %s\n", title );
  172. }
  173. @endverbatim
  174. <h2> Using and Installing </h2>
  175. There are 2 files in TinyXML-2:
  176. <ol>
  177. <li>tinyxml2.cpp</li>
  178. <li>tinyxml2.h</li>
  179. </ol>
  180. And additionally a test file:
  181. <ol>
  182. <li>xmltest.cpp</li>
  183. </ol>
  184. Simply compile and run. There is a visual studio 2010 project included, a simple Makefile,
  185. an XCode project, and a cmake CMakeLists.txt included to help you. The top of tinyxml.h
  186. even has a simple g++ command line if you are are *nix and don't want to use a build system.
  187. <h2> Documentation </h2>
  188. The documentation is build with Doxygen, using the 'dox'
  189. configuration file.
  190. <h2> License </h2>
  191. TinyXML-2 is released under the zlib license:
  192. This software is provided 'as-is', without any express or implied
  193. warranty. In no event will the authors be held liable for any
  194. damages arising from the use of this software.
  195. Permission is granted to anyone to use this software for any
  196. purpose, including commercial applications, and to alter it and
  197. redistribute it freely, subject to the following restrictions:
  198. 1. The origin of this software must not be misrepresented; you must
  199. not claim that you wrote the original software. If you use this
  200. software in a product, an acknowledgment in the product documentation
  201. would be appreciated but is not required.
  202. 2. Altered source versions must be plainly marked as such, and
  203. must not be misrepresented as being the original software.
  204. 3. This notice may not be removed or altered from any source
  205. distribution.
  206. <h2> Contributors </h2>
  207. Thanks very much to everyone who sends suggestions, bugs, ideas, and
  208. encouragement. It all helps, and makes this project fun.
  209. The original TinyXML-1 has many contributors, who all deserve thanks
  210. in shaping what is a very successful library. Extra thanks to Yves
  211. Berquin and Andrew Ellerton who were key contributors.
  212. TinyXML-2 grew from that effort. Lee Thomason is the original author
  213. of TinyXML-2 (and TinyXML-1) but hopefully TinyXML-2 will be improved
  214. by many contributors.
  215. */