tinyxml2.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. Original code by Lee Thomason (www.grinninglizard.com)
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #ifndef TINYXML2_INCLUDED
  19. #define TINYXML2_INCLUDED
  20. #if 1
  21. #include <cctype>
  22. #include <climits>
  23. #include <cstdio>
  24. #include <cstring>
  25. #include <cstdarg>
  26. #else
  27. // Not completely sure all the interesting systems
  28. // can handle the new headers; can switch this if
  29. // there is an include problem.
  30. #include <limits.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. #include <memory.h> // Needed by mac.
  34. #endif
  35. /*
  36. TODO: add 'lastAttribute' for faster parsing.
  37. TODO: intern strings instead of allocation.
  38. */
  39. /*
  40. gcc: g++ -Wall tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
  41. */
  42. #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
  43. #ifndef DEBUG
  44. #define DEBUG
  45. #endif
  46. #endif
  47. #if defined(DEBUG)
  48. #if defined(_MSC_VER)
  49. #define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
  50. #elif defined (ANDROID_NDK)
  51. #include <android/log.h>
  52. #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
  53. #else
  54. #include <assert.h>
  55. #define TIXMLASSERT assert
  56. #endif
  57. #else
  58. #define TIXMLASSERT( x ) {}
  59. #endif
  60. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  61. // Microsoft visual studio, version 2005 and higher.
  62. /*int _snprintf_s(
  63. char *buffer,
  64. size_t sizeOfBuffer,
  65. size_t count,
  66. const char *format [,
  67. argument] ...
  68. );*/
  69. inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) {
  70. va_list va;
  71. va_start( va, format );
  72. int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
  73. va_end( va );
  74. return result;
  75. }
  76. #define TIXML_SSCANF sscanf_s
  77. #elif defined(__GNUC__) && (__GNUC__ >= 3 )
  78. // GCC version 3 and higher
  79. //#warning( "Using sn* functions." )
  80. #define TIXML_SNPRINTF snprintf
  81. #define TIXML_SSCANF sscanf
  82. #else
  83. #define TIXML_SNPRINTF snprintf( buf, size, x ) snprintf( buf, size, x )
  84. #define TIXML_SSCANF sscanf
  85. #endif
  86. static const int TIXML2_MAJOR_VERSION = 0;
  87. static const int TIXML2_MINOR_VERSION = 9;
  88. static const int TIXML2_PATCH_VERSION = 3;
  89. namespace tinyxml2
  90. {
  91. class XMLDocument;
  92. class XMLElement;
  93. class XMLAttribute;
  94. class XMLComment;
  95. class XMLNode;
  96. class XMLText;
  97. class XMLDeclaration;
  98. class XMLUnknown;
  99. class XMLPrinter;
  100. /*
  101. A class that wraps strings. Normally stores the start and end
  102. pointers into the XML file itself, and will apply normalization
  103. and entity translation if actually read. Can also store (and memory
  104. manage) a traditional char[]
  105. */
  106. class StrPair
  107. {
  108. public:
  109. enum {
  110. NEEDS_ENTITY_PROCESSING = 0x01,
  111. NEEDS_NEWLINE_NORMALIZATION = 0x02,
  112. TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  113. TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  114. ATTRIBUTE_NAME = 0,
  115. ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  116. ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  117. COMMENT = NEEDS_NEWLINE_NORMALIZATION
  118. };
  119. StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
  120. ~StrPair();
  121. void Set( char* _start, char* _end, int _flags ) {
  122. Reset();
  123. this->start = _start; this->end = _end; this->flags = _flags | NEEDS_FLUSH;
  124. }
  125. const char* GetStr();
  126. bool Empty() const { return start == end; }
  127. void SetInternedStr( const char* str ) { Reset(); this->start = const_cast<char*>(str); }
  128. void SetStr( const char* str, int flags=0 );
  129. char* ParseText( char* in, const char* endTag, int strFlags );
  130. char* ParseName( char* in );
  131. private:
  132. void Reset();
  133. enum {
  134. NEEDS_FLUSH = 0x100,
  135. NEEDS_DELETE = 0x200
  136. };
  137. // After parsing, if *end != 0, it can be set to zero.
  138. int flags;
  139. char* start;
  140. char* end;
  141. };
  142. /*
  143. A dynamic array of Plain Old Data. Doesn't support constructors, etc.
  144. Has a small initial memory pool, so that low or no usage will not
  145. cause a call to new/delete
  146. */
  147. template <class T, int INIT>
  148. class DynArray
  149. {
  150. public:
  151. DynArray< T, INIT >()
  152. {
  153. mem = pool;
  154. allocated = INIT;
  155. size = 0;
  156. }
  157. ~DynArray()
  158. {
  159. if ( mem != pool ) {
  160. delete mem;
  161. }
  162. }
  163. void Push( T t )
  164. {
  165. EnsureCapacity( size+1 );
  166. mem[size++] = t;
  167. }
  168. T* PushArr( int count )
  169. {
  170. EnsureCapacity( size+count );
  171. T* ret = &mem[size];
  172. size += count;
  173. return ret;
  174. }
  175. T Pop() {
  176. return mem[--size];
  177. }
  178. void PopArr( int count )
  179. {
  180. TIXMLASSERT( size >= count );
  181. size -= count;
  182. }
  183. bool Empty() const { return size == 0; }
  184. T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
  185. const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
  186. int Size() const { return size; }
  187. int Capacity() const { return allocated; }
  188. const T* Mem() const { return mem; }
  189. T* Mem() { return mem; }
  190. private:
  191. void EnsureCapacity( int cap ) {
  192. if ( cap > allocated ) {
  193. int newAllocated = cap * 2;
  194. T* newMem = new T[newAllocated];
  195. memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs
  196. if ( mem != pool ) delete [] mem;
  197. mem = newMem;
  198. allocated = newAllocated;
  199. }
  200. }
  201. T* mem;
  202. T pool[INIT];
  203. int allocated; // objects allocated
  204. int size; // number objects in use
  205. };
  206. /*
  207. Parent virtual class a a pool for fast allocation
  208. and deallocation of objects.
  209. */
  210. class MemPool
  211. {
  212. public:
  213. MemPool() {}
  214. virtual ~MemPool() {}
  215. virtual int ItemSize() const = 0;
  216. virtual void* Alloc() = 0;
  217. virtual void Free( void* ) = 0;
  218. };
  219. /*
  220. Template child class to create pools of the correct type.
  221. */
  222. template< int SIZE >
  223. class MemPoolT : public MemPool
  224. {
  225. public:
  226. MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
  227. ~MemPoolT() {
  228. // Delete the blocks.
  229. for( int i=0; i<blockPtrs.Size(); ++i ) {
  230. delete blockPtrs[i];
  231. }
  232. }
  233. virtual int ItemSize() const { return SIZE; }
  234. int CurrentAllocs() const { return currentAllocs; }
  235. virtual void* Alloc() {
  236. if ( !root ) {
  237. // Need a new block.
  238. Block* block = new Block();
  239. blockPtrs.Push( block );
  240. for( int i=0; i<COUNT-1; ++i ) {
  241. block->chunk[i].next = &block->chunk[i+1];
  242. }
  243. block->chunk[COUNT-1].next = 0;
  244. root = block->chunk;
  245. }
  246. void* result = root;
  247. root = root->next;
  248. ++currentAllocs;
  249. if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
  250. nAllocs++;
  251. return result;
  252. }
  253. virtual void Free( void* mem ) {
  254. if ( !mem ) return;
  255. --currentAllocs;
  256. Chunk* chunk = (Chunk*)mem;
  257. memset( chunk, 0xfe, sizeof(Chunk) );
  258. chunk->next = root;
  259. root = chunk;
  260. }
  261. void Trace( const char* name ) {
  262. printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
  263. name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
  264. }
  265. private:
  266. enum { COUNT = 1024/SIZE };
  267. union Chunk {
  268. Chunk* next;
  269. char mem[SIZE];
  270. };
  271. struct Block {
  272. Chunk chunk[COUNT];
  273. };
  274. DynArray< Block*, 10 > blockPtrs;
  275. Chunk* root;
  276. int currentAllocs;
  277. int nAllocs;
  278. int maxAllocs;
  279. };
  280. /**
  281. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  282. If you call the Accept() method, it requires being passed a XMLVisitor
  283. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  284. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
  285. are simply called with Visit().
  286. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  287. false, <b>no children of this node or its sibilings</b> will be Visited.
  288. All flavors of Visit methods have a default implementation that returns 'true' (continue
  289. visiting). You need to only override methods that are interesting to you.
  290. Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
  291. You should never change the document from a callback.
  292. @sa XMLNode::Accept()
  293. */
  294. class XMLVisitor
  295. {
  296. public:
  297. virtual ~XMLVisitor() {}
  298. /// Visit a document.
  299. virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
  300. /// Visit a document.
  301. virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
  302. /// Visit an element.
  303. virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; }
  304. /// Visit an element.
  305. virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; }
  306. /// Visit a declaration
  307. virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; }
  308. /// Visit a text node
  309. virtual bool Visit( const XMLText& /*text*/ ) { return true; }
  310. /// Visit a comment node
  311. virtual bool Visit( const XMLComment& /*comment*/ ) { return true; }
  312. /// Visit an unknown node
  313. virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; }
  314. };
  315. /*
  316. Utility functionality.
  317. */
  318. class XMLUtil
  319. {
  320. public:
  321. // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
  322. // correct, but simple, and usually works.
  323. static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
  324. static char* SkipWhiteSpace( char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
  325. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  326. int n = 0;
  327. if ( p == q ) {
  328. return true;
  329. }
  330. while( *p && *q && *p == *q && n<nChar ) {
  331. ++p; ++q; ++n;
  332. }
  333. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  334. return true;
  335. }
  336. return false;
  337. }
  338. inline static int IsUTF8Continuation( const char p ) { return p & 0x80; }
  339. inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalnum( anyByte ) : 1; }
  340. inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalpha( anyByte ) : 1; }
  341. static const char* ReadBOM( const char* p, bool* hasBOM );
  342. // p is the starting location,
  343. // the UTF-8 value of the entity will be placed in value, and length filled in.
  344. static const char* GetCharacterRef( const char* p, char* value, int* length );
  345. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  346. };
  347. /** XMLNode is a base class for every object that is in the
  348. XML Document Object Model (DOM), except XMLAttributes.
  349. Nodes have siblings, a parent, and children which can
  350. be navigated. A node is always in a XMLDocument.
  351. The type of a TiXmlNode can be queried, and it can
  352. be cast to its more defined type.
  353. An XMLDocument allocates memory for all its Nodes.
  354. When the XMLDocument gets deleted, all its Nodes
  355. will also be deleted.
  356. @verbatim
  357. A Document can contain: Element (container or leaf)
  358. Comment (leaf)
  359. Unknown (leaf)
  360. Declaration( leaf )
  361. An Element can contain: Element (container or leaf)
  362. Text (leaf)
  363. Attributes (not on tree)
  364. Comment (leaf)
  365. Unknown (leaf)
  366. @endverbatim
  367. */
  368. class XMLNode
  369. {
  370. friend class XMLDocument;
  371. friend class XMLElement;
  372. public:
  373. /// Get the XMLDocument that owns this XMLNode.
  374. const XMLDocument* GetDocument() const { return document; }
  375. /// Get the XMLDocument that owns this XMLNode.
  376. XMLDocument* GetDocument() { return document; }
  377. virtual XMLElement* ToElement() { return 0; } ///< Safely cast to an Element, or null.
  378. virtual XMLText* ToText() { return 0; } ///< Safely cast to Text, or null.
  379. virtual XMLComment* ToComment() { return 0; } ///< Safely cast to a Comment, or null.
  380. virtual XMLDocument* ToDocument() { return 0; } ///< Safely cast to a Document, or null.
  381. virtual XMLDeclaration* ToDeclaration() { return 0; } ///< Safely cast to a Declaration, or null.
  382. virtual XMLUnknown* ToUnknown() { return 0; } ///< Safely cast to an Unknown, or null.
  383. virtual const XMLElement* ToElement() const { return 0; }
  384. virtual const XMLText* ToText() const { return 0; }
  385. virtual const XMLComment* ToComment() const { return 0; }
  386. virtual const XMLDocument* ToDocument() const { return 0; }
  387. virtual const XMLDeclaration* ToDeclaration() const { return 0; }
  388. virtual const XMLUnknown* ToUnknown() const { return 0; }
  389. /** The meaning of 'value' changes for the specific type.
  390. @verbatim
  391. Document: empy
  392. Element: name of the element
  393. Comment: the comment text
  394. Unknown: the tag contents
  395. Text: the text string
  396. @endverbatim
  397. */
  398. const char* Value() const { return value.GetStr(); }
  399. /** Set the Value of an XML node.
  400. @sa Value()
  401. */
  402. void SetValue( const char* val, bool staticMem=false );
  403. /// Get the parent of this node on the DOM.
  404. const XMLNode* Parent() const { return parent; }
  405. XMLNode* Parent() { return parent; }
  406. /// Returns true if this node has no children.
  407. bool NoChildren() const { return !firstChild; }
  408. /// Get the first child node, or null if none exists.
  409. const XMLNode* FirstChild() const { return firstChild; }
  410. XMLNode* FirstChild() { return firstChild; }
  411. /** Get the first child element, or optionally the first child
  412. element with the specified name.
  413. */
  414. const XMLElement* FirstChildElement( const char* value=0 ) const;
  415. XMLElement* FirstChildElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( _value )); }
  416. /// Get the last child node, or null if none exists.
  417. const XMLNode* LastChild() const { return lastChild; }
  418. XMLNode* LastChild() { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() ); }
  419. /** Get the last child element or optionally the last child
  420. element with the specified name.
  421. */
  422. const XMLElement* LastChildElement( const char* value=0 ) const;
  423. XMLElement* LastChildElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(_value) ); }
  424. /// Get the previous (left) sibling node of this node.
  425. const XMLNode* PreviousSibling() const { return prev; }
  426. XMLNode* PreviousSibling() { return prev; }
  427. /// Get the previous (left) sibling element of this node, with an opitionally supplied name.
  428. const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
  429. XMLElement* PreviousSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( _value ) ); }
  430. /// Get the next (right) sibling node of this node.
  431. const XMLNode* NextSibling() const { return next; }
  432. XMLNode* NextSibling() { return next; }
  433. /// Get the next (right) sibling element of this node, with an opitionally supplied name.
  434. const XMLElement* NextSiblingElement( const char* value=0 ) const;
  435. XMLElement* NextSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( _value ) ); }
  436. /**
  437. Add a child node as the last (right) child.
  438. */
  439. XMLNode* InsertEndChild( XMLNode* addThis );
  440. XMLNode* LinkEndChild( XMLNode* addThis ) { return InsertEndChild( addThis ); }
  441. /**
  442. Add a child node as the first (left) child.
  443. */
  444. XMLNode* InsertFirstChild( XMLNode* addThis );
  445. /**
  446. Add a node after the specified child node.
  447. */
  448. XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
  449. /**
  450. Delete all the children of this node.
  451. */
  452. void DeleteChildren();
  453. /**
  454. Delete a child of this node.
  455. */
  456. void DeleteChild( XMLNode* node );
  457. /**
  458. Make a copy of this node, but not its children.
  459. You may pass in a Document pointer that will be
  460. the owner of the new Node. If the 'document' is
  461. null, then the node returned will be allocated
  462. from the current Document. (this->GetDocument())
  463. Note: if called on a XMLDocument, this will return null.
  464. */
  465. virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
  466. /**
  467. Test if 2 nodes are the same, but don't test children.
  468. The 2 nodes do not need to be in the same Document.
  469. Note: if called on a XMLDocument, this will return false.
  470. */
  471. virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
  472. /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
  473. XML tree will be conditionally visited and the host will be called back
  474. via the TiXmlVisitor interface.
  475. This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
  476. the XML for the callbacks, so the performance of TinyXML is unchanged by using this
  477. interface versus any other.)
  478. The interface has been based on ideas from:
  479. - http://www.saxproject.org/
  480. - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  481. Which are both good references for "visiting".
  482. An example of using Accept():
  483. @verbatim
  484. TiXmlPrinter printer;
  485. tinyxmlDoc.Accept( &printer );
  486. const char* xmlcstr = printer.CStr();
  487. @endverbatim
  488. */
  489. virtual bool Accept( XMLVisitor* visitor ) const = 0;
  490. // internal
  491. virtual char* ParseDeep( char*, StrPair* );
  492. protected:
  493. XMLNode( XMLDocument* );
  494. virtual ~XMLNode();
  495. XMLNode( const XMLNode& ); // not supported
  496. void operator=( const XMLNode& ); // not supported
  497. XMLDocument* document;
  498. XMLNode* parent;
  499. mutable StrPair value;
  500. XMLNode* firstChild;
  501. XMLNode* lastChild;
  502. XMLNode* prev;
  503. XMLNode* next;
  504. private:
  505. MemPool* memPool;
  506. void Unlink( XMLNode* child );
  507. };
  508. /** XML text.
  509. Note that a text node can have child element nodes, for example:
  510. @verbatim
  511. <root>This is <b>bold</b></root>
  512. @endverbatim
  513. A text node can have 2 ways to output the next. "normal" output
  514. and CDATA. It will default to the mode it was parsed from the XML file and
  515. you generally want to leave it alone, but you can change the output mode with
  516. SetCDATA() and query it with CDATA().
  517. */
  518. class XMLText : public XMLNode
  519. {
  520. friend class XMLBase;
  521. friend class XMLDocument;
  522. public:
  523. virtual bool Accept( XMLVisitor* visitor ) const;
  524. virtual XMLText* ToText() { return this; }
  525. virtual const XMLText* ToText() const { return this; }
  526. /// Declare whether this should be CDATA or standard text.
  527. void SetCData( bool _isCData ) { this->isCData = _isCData; }
  528. /// Returns true if this is a CDATA text element.
  529. bool CData() const { return isCData; }
  530. char* ParseDeep( char*, StrPair* endTag );
  531. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  532. virtual bool ShallowEqual( const XMLNode* compare ) const;
  533. protected:
  534. XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {}
  535. virtual ~XMLText() {}
  536. XMLText( const XMLText& ); // not supported
  537. void operator=( const XMLText& ); // not supported
  538. private:
  539. bool isCData;
  540. };
  541. /** An XML Comment. */
  542. class XMLComment : public XMLNode
  543. {
  544. friend class XMLDocument;
  545. public:
  546. virtual XMLComment* ToComment() { return this; }
  547. virtual const XMLComment* ToComment() const { return this; }
  548. virtual bool Accept( XMLVisitor* visitor ) const;
  549. char* ParseDeep( char*, StrPair* endTag );
  550. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  551. virtual bool ShallowEqual( const XMLNode* compare ) const;
  552. protected:
  553. XMLComment( XMLDocument* doc );
  554. virtual ~XMLComment();
  555. XMLComment( const XMLComment& ); // not supported
  556. void operator=( const XMLComment& ); // not supported
  557. private:
  558. };
  559. /** In correct XML the declaration is the first entry in the file.
  560. @verbatim
  561. <?xml version="1.0" standalone="yes"?>
  562. @endverbatim
  563. TinyXML2 will happily read or write files without a declaration,
  564. however.
  565. The text of the declaration isn't interpreted. It is parsed
  566. and written as a string.
  567. */
  568. class XMLDeclaration : public XMLNode
  569. {
  570. friend class XMLDocument;
  571. public:
  572. virtual XMLDeclaration* ToDeclaration() { return this; }
  573. virtual const XMLDeclaration* ToDeclaration() const { return this; }
  574. virtual bool Accept( XMLVisitor* visitor ) const;
  575. char* ParseDeep( char*, StrPair* endTag );
  576. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  577. virtual bool ShallowEqual( const XMLNode* compare ) const;
  578. protected:
  579. XMLDeclaration( XMLDocument* doc );
  580. virtual ~XMLDeclaration();
  581. XMLDeclaration( const XMLDeclaration& ); // not supported
  582. void operator=( const XMLDeclaration& ); // not supported
  583. };
  584. /** Any tag that tinyXml doesn't recognize is saved as an
  585. unknown. It is a tag of text, but should not be modified.
  586. It will be written back to the XML, unchanged, when the file
  587. is saved.
  588. DTD tags get thrown into TiXmlUnknowns.
  589. */
  590. class XMLUnknown : public XMLNode
  591. {
  592. friend class XMLDocument;
  593. public:
  594. virtual XMLUnknown* ToUnknown() { return this; }
  595. virtual const XMLUnknown* ToUnknown() const { return this; }
  596. virtual bool Accept( XMLVisitor* visitor ) const;
  597. char* ParseDeep( char*, StrPair* endTag );
  598. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  599. virtual bool ShallowEqual( const XMLNode* compare ) const;
  600. protected:
  601. XMLUnknown( XMLDocument* doc );
  602. virtual ~XMLUnknown();
  603. XMLUnknown( const XMLUnknown& ); // not supported
  604. void operator=( const XMLUnknown& ); // not supported
  605. };
  606. enum {
  607. XML_NO_ERROR = 0,
  608. XML_SUCCESS = 0,
  609. XML_NO_ATTRIBUTE,
  610. XML_WRONG_ATTRIBUTE_TYPE,
  611. XML_ERROR_FILE_NOT_FOUND,
  612. XML_ERROR_FILE_COULD_NOT_BE_OPENED,
  613. XML_ERROR_ELEMENT_MISMATCH,
  614. XML_ERROR_PARSING_ELEMENT,
  615. XML_ERROR_PARSING_ATTRIBUTE,
  616. XML_ERROR_IDENTIFYING_TAG,
  617. XML_ERROR_PARSING_TEXT,
  618. XML_ERROR_PARSING_CDATA,
  619. XML_ERROR_PARSING_COMMENT,
  620. XML_ERROR_PARSING_DECLARATION,
  621. XML_ERROR_PARSING_UNKNOWN,
  622. XML_ERROR_EMPTY_DOCUMENT,
  623. XML_ERROR_MISMATCHED_ELEMENT,
  624. XML_ERROR_PARSING
  625. };
  626. /** An attribute is a name-value pair. Elements have an arbitrary
  627. number of attributes, each with a unique name.
  628. @note The attributes are not XMLNodes. You may only query the
  629. Next() attribute in a list.
  630. */
  631. class XMLAttribute
  632. {
  633. friend class XMLElement;
  634. public:
  635. const char* Name() const { return name.GetStr(); } ///< The name of the attribute.
  636. const char* Value() const { return value.GetStr(); } ///< The value of the attribute.
  637. const XMLAttribute* Next() const { return next; } ///< The next attribute in the list.
  638. /** IntAttribute interprets the attribute as an integer, and returns the value.
  639. If the value isn't an integer, 0 will be returned. There is no error checking;
  640. use QueryIntAttribute() if you need error checking.
  641. */
  642. int IntValue() const { int i=0; QueryIntValue( &i ); return i; }
  643. /// Query as an unsigned integer. See IntAttribute()
  644. unsigned UnsignedValue() const { unsigned i=0; QueryUnsignedValue( &i ); return i; }
  645. /// Query as a boolean. See IntAttribute()
  646. bool BoolValue() const { bool b=false; QueryBoolValue( &b ); return b; }
  647. /// Query as a double. See IntAttribute()
  648. double DoubleValue() const { double d=0; QueryDoubleValue( &d ); return d; }
  649. /// Query as a float. See IntAttribute()
  650. float FloatValue() const { float f=0; QueryFloatValue( &f ); return f; }
  651. /** QueryIntAttribute interprets the attribute as an integer, and returns the value
  652. in the provided paremeter. The function will return XML_NO_ERROR on success,
  653. and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
  654. */
  655. int QueryIntValue( int* value ) const;
  656. /// See QueryIntAttribute
  657. int QueryUnsignedValue( unsigned int* value ) const;
  658. /// See QueryIntAttribute
  659. int QueryBoolValue( bool* value ) const;
  660. /// See QueryIntAttribute
  661. int QueryDoubleValue( double* value ) const;
  662. /// See QueryIntAttribute
  663. int QueryFloatValue( float* value ) const;
  664. /// Set the attribute to a string value.
  665. void SetAttribute( const char* value );
  666. /// Set the attribute to value.
  667. void SetAttribute( int value );
  668. /// Set the attribute to value.
  669. void SetAttribute( unsigned value );
  670. /// Set the attribute to value.
  671. void SetAttribute( bool value );
  672. /// Set the attribute to value.
  673. void SetAttribute( double value );
  674. /// Set the attribute to value.
  675. void SetAttribute( float value );
  676. private:
  677. enum { BUF_SIZE = 200 };
  678. XMLAttribute() : next( 0 ) {}
  679. virtual ~XMLAttribute() {}
  680. XMLAttribute( const XMLAttribute& ); // not supported
  681. void operator=( const XMLAttribute& ); // not supported
  682. void SetName( const char* name );
  683. char* ParseDeep( char* p, bool processEntities );
  684. mutable StrPair name;
  685. mutable StrPair value;
  686. XMLAttribute* next;
  687. MemPool* memPool;
  688. };
  689. /** The element is a container class. It has a value, the element name,
  690. and can contain other elements, text, comments, and unknowns.
  691. Elements also contain an arbitrary number of attributes.
  692. */
  693. class XMLElement : public XMLNode
  694. {
  695. friend class XMLBase;
  696. friend class XMLDocument;
  697. public:
  698. /// Get the name of an element (which is the Value() of the node.)
  699. const char* Name() const { return Value(); }
  700. /// Set the name of the element.
  701. void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); }
  702. virtual XMLElement* ToElement() { return this; }
  703. virtual const XMLElement* ToElement() const { return this; }
  704. virtual bool Accept( XMLVisitor* visitor ) const;
  705. /** Given an attribute name, Attribute() returns the value
  706. for the attribute of that name, or null if none
  707. exists. For example:
  708. @verbatim
  709. const char* value = ele->Attribute( "foo" );
  710. @endverbatim
  711. The 'value' parameter is normally null. However, if specified,
  712. the attribute will only be returned if the 'name' and 'value'
  713. match. This allow you to write code:
  714. @verbatim
  715. if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
  716. @endverbatim
  717. rather than:
  718. @verbatim
  719. if ( ele->Attribute( "foo" ) ) {
  720. if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
  721. }
  722. @endverbatim
  723. */
  724. const char* Attribute( const char* name, const char* value=0 ) const;
  725. /** Given an attribute name, IntAttribute() returns the value
  726. of the attribute interpreted as an integer. 0 will be
  727. returned if there is an error. For a method with error
  728. checking, see QueryIntAttribute()
  729. */
  730. int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; }
  731. /// See IntAttribute()
  732. unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; }
  733. /// See IntAttribute()
  734. bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; }
  735. /// See IntAttribute()
  736. double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; }
  737. /// See IntAttribute()
  738. float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; }
  739. /** Given an attribute name, QueryIntAttribute() returns
  740. XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
  741. can't be performed, or XML_NO_ATTRIBUTE if the attribute
  742. doesn't exist. If successful, the result of the conversion
  743. will be written to 'value'. If not successful, nothing will
  744. be written to 'value'. This allows you to provide default
  745. value:
  746. @verbatim
  747. int value = 10;
  748. QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
  749. @endverbatim
  750. */
  751. int QueryIntAttribute( const char* name, int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryIntValue( _value ); }
  752. /// See QueryIntAttribute()
  753. int QueryUnsignedAttribute( const char* name, unsigned int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryUnsignedValue( _value ); }
  754. /// See QueryIntAttribute()
  755. int QueryBoolAttribute( const char* name, bool* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryBoolValue( _value ); }
  756. /// See QueryIntAttribute()
  757. int QueryDoubleAttribute( const char* name, double* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryDoubleValue( _value ); }
  758. /// See QueryIntAttribute()
  759. int QueryFloatAttribute( const char* name, float* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryFloatValue( _value ); }
  760. /// Sets the named attribute to value.
  761. void SetAttribute( const char* name, const char* _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
  762. /// Sets the named attribute to value.
  763. void SetAttribute( const char* name, int _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
  764. /// Sets the named attribute to value.
  765. void SetAttribute( const char* name, unsigned _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
  766. /// Sets the named attribute to value.
  767. void SetAttribute( const char* name, bool _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
  768. /// Sets the named attribute to value.
  769. void SetAttribute( const char* name, double _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
  770. /**
  771. Delete an attribute.
  772. */
  773. void DeleteAttribute( const char* name );
  774. /// Return the first attribute in the list.
  775. const XMLAttribute* FirstAttribute() const { return rootAttribute; }
  776. /// Query a specific attribute in the list.
  777. const XMLAttribute* FindAttribute( const char* name ) const;
  778. /** Convenience function for easy access to the text inside an element. Although easy
  779. and concise, GetText() is limited compared to getting the TiXmlText child
  780. and accessing it directly.
  781. If the first child of 'this' is a TiXmlText, the GetText()
  782. returns the character string of the Text node, else null is returned.
  783. This is a convenient method for getting the text of simple contained text:
  784. @verbatim
  785. <foo>This is text</foo>
  786. const char* str = fooElement->GetText();
  787. @endverbatim
  788. 'str' will be a pointer to "This is text".
  789. Note that this function can be misleading. If the element foo was created from
  790. this XML:
  791. @verbatim
  792. <foo><b>This is text</b></foo>
  793. @endverbatim
  794. then the value of str would be null. The first child node isn't a text node, it is
  795. another element. From this XML:
  796. @verbatim
  797. <foo>This is <b>text</b></foo>
  798. @endverbatim
  799. GetText() will return "This is ".
  800. */
  801. const char* GetText() const;
  802. // internal:
  803. enum {
  804. OPEN, // <foo>
  805. CLOSED, // <foo/>
  806. CLOSING // </foo>
  807. };
  808. int ClosingType() const { return closingType; }
  809. char* ParseDeep( char* p, StrPair* endTag );
  810. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  811. virtual bool ShallowEqual( const XMLNode* compare ) const;
  812. private:
  813. XMLElement( XMLDocument* doc );
  814. virtual ~XMLElement();
  815. XMLElement( const XMLElement& ); // not supported
  816. void operator=( const XMLElement& ); // not supported
  817. XMLAttribute* FindAttribute( const char* name );
  818. XMLAttribute* FindOrCreateAttribute( const char* name );
  819. void LinkAttribute( XMLAttribute* attrib );
  820. char* ParseAttributes( char* p );
  821. int closingType;
  822. XMLAttribute* rootAttribute;
  823. };
  824. /** A document binds together all the functionality.
  825. It can be saved, loaded, and printed to the screen.
  826. All Nodes are connected and allocated to a Document.
  827. If the Document is deleted, all its Nodes are also deleted.
  828. */
  829. class XMLDocument : public XMLNode
  830. {
  831. friend class XMLElement;
  832. public:
  833. /// constructor
  834. XMLDocument( bool processEntities = true );
  835. ~XMLDocument();
  836. virtual XMLDocument* ToDocument() { return this; }
  837. virtual const XMLDocument* ToDocument() const { return this; }
  838. /**
  839. Parse an XML file from a character string.
  840. Returns XML_NO_ERROR (0) on success, or
  841. an errorID.
  842. */
  843. int Parse( const char* xml );
  844. /**
  845. Load an XML file from disk.
  846. Returns XML_NO_ERROR (0) on success, or
  847. an errorID.
  848. */
  849. int LoadFile( const char* filename );
  850. /**
  851. Load an XML file from disk. You are responsible
  852. for providing and closing the FILE*.
  853. Returns XML_NO_ERROR (0) on success, or
  854. an errorID.
  855. */
  856. int LoadFile( FILE* );
  857. /**
  858. Save the XML file to disk.
  859. */
  860. void SaveFile( const char* filename );
  861. bool ProcessEntities() const { return processEntities; }
  862. /**
  863. Returns true if this document has a leading Byte Order Mark of UTF8.
  864. */
  865. bool HasBOM() const { return writeBOM; }
  866. /** Return the root element of DOM. Equivalent to FirstChildElement().
  867. To get the first node, use FirstChild().
  868. */
  869. XMLElement* RootElement() { return FirstChildElement(); }
  870. const XMLElement* RootElement() const { return FirstChildElement(); }
  871. /** Print the Document. If the Printer is not provided, it will
  872. print to stdout. If you provide Printer, this can print to a file:
  873. @verbatim
  874. XMLPrinter printer( fp );
  875. doc.Print( &printer );
  876. @endverbatim
  877. Or you can use a printer to print to memory:
  878. @verbatim
  879. XMLPrinter printer;
  880. doc->Print( &printer );
  881. // printer.CStr() has a const char* to the XML
  882. @endverbatim
  883. */
  884. void Print( XMLPrinter* streamer=0 );
  885. virtual bool Accept( XMLVisitor* visitor ) const;
  886. /**
  887. Create a new Element associated with
  888. this Document. The memory for the Element
  889. is managed by the Document.
  890. */
  891. XMLElement* NewElement( const char* name );
  892. /**
  893. Create a new Comment associated with
  894. this Document. The memory for the Comment
  895. is managed by the Document.
  896. */
  897. XMLComment* NewComment( const char* comment );
  898. /**
  899. Create a new Text associated with
  900. this Document. The memory for the Text
  901. is managed by the Document.
  902. */
  903. XMLText* NewText( const char* text );
  904. /**
  905. Create a new Declaration associated with
  906. this Document. The memory for the object
  907. is managed by the Document.
  908. */
  909. XMLDeclaration* NewDeclaration( const char* text );
  910. /**
  911. Create a new Unknown associated with
  912. this Document. The memory for the object
  913. is managed by the Document.
  914. */
  915. XMLUnknown* NewUnknown( const char* text );
  916. /**
  917. Delete a node associated with this documented.
  918. It will be unlinked from the DOM.
  919. */
  920. void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); }
  921. void SetError( int error, const char* str1, const char* str2 );
  922. /// Return true if there was an error parsing the document.
  923. bool Error() const { return errorID != XML_NO_ERROR; }
  924. /// Return the errorID.
  925. int ErrorID() const { return errorID; }
  926. /// Return a possibly helpful diagnostic location or string.
  927. const char* GetErrorStr1() const { return errorStr1; }
  928. /// Return possibly helpful secondary diagnostic location or string.
  929. const char* GetErrorStr2() const { return errorStr2; }
  930. /// If there is an error, print it to stdout
  931. void PrintError() const;
  932. // internal
  933. char* Identify( char* p, XMLNode** node );
  934. virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { return 0; }
  935. virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { return false; }
  936. private:
  937. XMLDocument( const XMLDocument& ); // not supported
  938. void operator=( const XMLDocument& ); // not supported
  939. void InitDocument();
  940. bool writeBOM;
  941. bool processEntities;
  942. int errorID;
  943. const char* errorStr1;
  944. const char* errorStr2;
  945. char* charBuffer;
  946. MemPoolT< sizeof(XMLElement) > elementPool;
  947. MemPoolT< sizeof(XMLAttribute) > attributePool;
  948. MemPoolT< sizeof(XMLText) > textPool;
  949. MemPoolT< sizeof(XMLComment) > commentPool;
  950. };
  951. /**
  952. Printing functionality. The XMLPrinter gives you more
  953. options than the XMLDocument::Print() method.
  954. It can:
  955. -# Print to memory.
  956. -# Print to a file you provide
  957. -# Print XML without a XMLDocument.
  958. Print to Memory
  959. @verbatim
  960. XMLPrinter printer;
  961. doc->Print( &printer );
  962. SomeFunctior( printer.CStr() );
  963. @endverbatim
  964. Print to a File
  965. You provide the file pointer.
  966. @verbatim
  967. XMLPrinter printer( fp );
  968. doc.Print( &printer );
  969. @endverbatim
  970. Print without a XMLDocument
  971. When loading, an XML parser is very useful. However, sometimes
  972. when saving, it just gets in the way. The code is often set up
  973. for streaming, and constructing the DOM is just overhead.
  974. The Printer supports the streaming case. The following code
  975. prints out a trivially simple XML file without ever creating
  976. an XML document.
  977. @verbatim
  978. XMLPrinter printer( fp );
  979. printer.OpenElement( "foo" );
  980. printer.PushAttribute( "foo", "bar" );
  981. printer.CloseElement();
  982. @endverbatim
  983. */
  984. class XMLPrinter : public XMLVisitor
  985. {
  986. public:
  987. /** Construct the printer. If the FILE* is specified,
  988. this will print to the FILE. Else it will print
  989. to memory, and the result is available in CStr()
  990. */
  991. XMLPrinter( FILE* file=0 );
  992. ~XMLPrinter() {}
  993. /** If streaming, write the BOM and declaration. */
  994. void PushHeader( bool writeBOM, bool writeDeclaration );
  995. /** If streaming, start writing an element.
  996. The element must be closed with CloseElement()
  997. */
  998. void OpenElement( const char* name );
  999. /// If streaming, add an attribute to an open element.
  1000. void PushAttribute( const char* name, const char* value );
  1001. void PushAttribute( const char* name, int value );
  1002. void PushAttribute( const char* name, unsigned value );
  1003. void PushAttribute( const char* name, bool value );
  1004. void PushAttribute( const char* name, double value );
  1005. /// If streaming, close the Element.
  1006. void CloseElement();
  1007. /// Add a text node.
  1008. void PushText( const char* text, bool cdata=false );
  1009. /// Add a comment
  1010. void PushComment( const char* comment );
  1011. void PushDeclaration( const char* value );
  1012. void PushUnknown( const char* value );
  1013. virtual bool VisitEnter( const XMLDocument& /*doc*/ );
  1014. virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
  1015. virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
  1016. virtual bool VisitExit( const XMLElement& element );
  1017. virtual bool Visit( const XMLText& text );
  1018. virtual bool Visit( const XMLComment& comment );
  1019. virtual bool Visit( const XMLDeclaration& declaration );
  1020. virtual bool Visit( const XMLUnknown& unknown );
  1021. /**
  1022. If in print to memory mode, return a pointer to
  1023. the XML file in memory.
  1024. */
  1025. const char* CStr() const { return buffer.Mem(); }
  1026. private:
  1027. void SealElement();
  1028. void PrintSpace( int depth );
  1029. void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
  1030. void Print( const char* format, ... );
  1031. bool elementJustOpened;
  1032. bool firstElement;
  1033. FILE* fp;
  1034. int depth;
  1035. int textDepth;
  1036. bool processEntities;
  1037. enum {
  1038. ENTITY_RANGE = 64,
  1039. BUF_SIZE = 200
  1040. };
  1041. bool entityFlag[ENTITY_RANGE];
  1042. bool restrictedEntityFlag[ENTITY_RANGE];
  1043. DynArray< const char*, 10 > stack;
  1044. DynArray< char, 20 > buffer, accumulator;
  1045. };
  1046. } // tinyxml2
  1047. // What follows is the docs for the examples.
  1048. // I'd like the docs to be just before the
  1049. // actual examples in xmltest.cpp, but I
  1050. // can't seem to get doxygen to do that. It
  1051. // would be a wonderful patch if anyone figures
  1052. // it out.
  1053. /** @page Example-1 Load an XML File
  1054. * @dontinclude ./xmltest.cpp
  1055. * Basic XML file loading.
  1056. * The basic syntax to load an XML file from
  1057. * disk and check for an error. (ErrorID()
  1058. * will return 0 for no error.)
  1059. * @skip example_1()
  1060. * @until }
  1061. */
  1062. /** @page Example-2 Parse an XML from char buffer
  1063. * @dontinclude ./xmltest.cpp
  1064. * Basic XML string parsing.
  1065. * The basic syntax to parse an XML for
  1066. * a char* and check for an error. (ErrorID()
  1067. * will return 0 for no error.)
  1068. * @skip example_2()
  1069. * @until }
  1070. */
  1071. /** @page Example-3 Get information out of XML
  1072. @dontinclude ./xmltest.cpp
  1073. In this example, we navigate a simple XML
  1074. file, and read some interesting text. Note
  1075. that this is examlpe doesn't use error
  1076. checking; working code should check for null
  1077. pointers when walking an XML tree, or use
  1078. XMLHandle.
  1079. (The XML is an excerpt from "dream.xml").
  1080. @skip example_3
  1081. @until </PLAY>";
  1082. The structure of the XML file is:
  1083. <ul>
  1084. <li>(declaration)</li>
  1085. <li>(dtd stuff)</li>
  1086. <li>Element "PLAY"</li>
  1087. <ul>
  1088. <li>Element "TITLE"</li>
  1089. <ul>
  1090. <li>Text "A Midsummer Night's Dream"</li>
  1091. </ul>
  1092. </ul>
  1093. </ul>
  1094. For this example, we want to print out the
  1095. title of the play. The text of the title (what
  1096. we want) is child of the "TITLE" element which
  1097. is a child of the "PLAY" element.
  1098. We want to skip the declaration and dtd, so the
  1099. method FirstChildElement() is a good choice. The
  1100. FirstChildElement() of the Document is the "PLAY"
  1101. Element, the FirstChildElement() of the "PLAY" Element
  1102. is the "TITLE" Element.
  1103. @until ( "TITLE" );
  1104. We can then use the convenience function GetText()
  1105. to get the title of the play.
  1106. @until title );
  1107. Text is just another Node in the XML DOM. And in
  1108. fact you should be a little cautious with it, as
  1109. text nodes can contain elements.
  1110. @verbatim
  1111. Consider: A Midsummer Night's <b>Dream</b>
  1112. @endverbatim
  1113. It is more correct to actually query the Text Node
  1114. if in doubt:
  1115. @until title );
  1116. Noting that here we use FirstChild() since we are
  1117. looking for XMLText, not an element, and ToText()
  1118. is a cast from a Node to a XMLText.
  1119. */
  1120. #endif // TINYXML2_INCLUDED