readme.txt 9.6 KB

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