readme.txt 9.3 KB

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