tinyxml.h 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #define TIXML_USE_STL
  20. #ifndef TINYXML_INCLUDED
  21. #define TINYXML_INCLUDED
  22. #ifdef _MSC_VER
  23. #pragma warning( push )
  24. #pragma warning( disable : 4530 )
  25. #pragma warning( disable : 4786 )
  26. #endif
  27. #include <ctype.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. // Help out windows:
  33. #if defined( _DEBUG ) && !defined( DEBUG )
  34. #define DEBUG
  35. #endif
  36. #ifdef TIXML_USE_STL
  37. #include <string>
  38. #include <iostream>
  39. #include <sstream>
  40. #define TIXML_STRING std::string
  41. #else
  42. #include "tinystr.h"
  43. #define TIXML_STRING TiXmlString
  44. #endif
  45. // Deprecated library function hell. Compilers want to use the
  46. // new safe versions. This probably doesn't fully address the problem,
  47. // but it gets closer. There are too many compilers for me to fully
  48. // test. If you get compilation troubles, undefine TIXML_SAFE
  49. #define TIXML_SAFE
  50. #ifdef TIXML_SAFE
  51. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  52. // Microsoft visual studio, version 2005 and higher.
  53. #define TIXML_SNPRINTF _snprintf_s
  54. #define TIXML_SNSCANF _snscanf_s
  55. #define TIXML_SSCANF sscanf_s
  56. #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
  57. // Microsoft visual studio, version 6 and higher.
  58. //#pragma message( "Using _sn* functions." )
  59. #define TIXML_SNPRINTF _snprintf
  60. #define TIXML_SNSCANF _snscanf
  61. #define TIXML_SSCANF sscanf
  62. #elif defined(__GNUC__) && (__GNUC__ >= 3 )
  63. // GCC version 3 and higher.s
  64. //#warning( "Using sn* functions." )
  65. #define TIXML_SNPRINTF snprintf
  66. #define TIXML_SNSCANF snscanf
  67. #define TIXML_SSCANF sscanf
  68. #else
  69. #define TIXML_SSCANF sscanf
  70. #endif
  71. #endif
  72. class TiXmlDocument;
  73. class TiXmlElement;
  74. class TiXmlComment;
  75. class TiXmlUnknown;
  76. class TiXmlAttribute;
  77. class TiXmlText;
  78. class TiXmlDeclaration;
  79. class TiXmlParsingData;
  80. const int TIXML_MAJOR_VERSION = 2;
  81. const int TIXML_MINOR_VERSION = 6;
  82. const int TIXML_PATCH_VERSION = 0;
  83. /* Internal structure for tracking location of items
  84. in the XML file.
  85. */
  86. struct TiXmlCursor
  87. {
  88. TiXmlCursor() { Clear(); }
  89. void Clear() { row = col = -1; }
  90. int row; // 0 based.
  91. int col; // 0 based.
  92. };
  93. /**
  94. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  95. If you call the Accept() method, it requires being passed a TiXmlVisitor
  96. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  97. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
  98. are simply called with Visit().
  99. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  100. false, <b>no children of this node or its sibilings</b> will be Visited.
  101. All flavors of Visit methods have a default implementation that returns 'true' (continue
  102. visiting). You need to only override methods that are interesting to you.
  103. Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
  104. You should never change the document from a callback.
  105. @sa TiXmlNode::Accept()
  106. */
  107. class TiXmlVisitor
  108. {
  109. public:
  110. virtual ~TiXmlVisitor() {}
  111. /// Visit a document.
  112. virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) { return true; }
  113. /// Visit a document.
  114. virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) { return true; }
  115. /// Visit an element.
  116. virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) { return true; }
  117. /// Visit an element.
  118. virtual bool VisitExit( const TiXmlElement& /*element*/ ) { return true; }
  119. /// Visit a declaration
  120. virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) { return true; }
  121. /// Visit a text node
  122. virtual bool Visit( const TiXmlText& /*text*/ ) { return true; }
  123. /// Visit a comment node
  124. virtual bool Visit( const TiXmlComment& /*comment*/ ) { return true; }
  125. /// Visit an unknow node
  126. virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) { return true; }
  127. };
  128. // Only used by Attribute::Query functions
  129. enum
  130. {
  131. TIXML_SUCCESS,
  132. TIXML_NO_ATTRIBUTE,
  133. TIXML_WRONG_TYPE
  134. };
  135. // Used by the parsing routines.
  136. enum TiXmlEncoding
  137. {
  138. TIXML_ENCODING_UNKNOWN,
  139. TIXML_ENCODING_UTF8,
  140. TIXML_ENCODING_LEGACY
  141. };
  142. const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
  143. /** TiXmlBase is a base class for every class in TinyXml.
  144. It does little except to establish that TinyXml classes
  145. can be printed and provide some utility functions.
  146. In XML, the document and elements can contain
  147. other elements and other types of nodes.
  148. @verbatim
  149. A Document can contain: Element (container or leaf)
  150. Comment (leaf)
  151. Unknown (leaf)
  152. Declaration( leaf )
  153. An Element can contain: Element (container or leaf)
  154. Text (leaf)
  155. Attributes (not on tree)
  156. Comment (leaf)
  157. Unknown (leaf)
  158. A Decleration contains: Attributes (not on tree)
  159. @endverbatim
  160. */
  161. class TiXmlBase
  162. {
  163. friend class TiXmlNode;
  164. friend class TiXmlElement;
  165. friend class TiXmlDocument;
  166. public:
  167. TiXmlBase() : userData(0) {}
  168. virtual ~TiXmlBase() {}
  169. /** All TinyXml classes can print themselves to a filestream
  170. or the string class (TiXmlString in non-STL mode, std::string
  171. in STL mode.) Either or both cfile and str can be null.
  172. This is a formatted print, and will insert
  173. tabs and newlines.
  174. (For an unformatted stream, use the << operator.)
  175. */
  176. virtual void Print( FILE* cfile, int depth ) const = 0;
  177. /** The world does not agree on whether white space should be kept or
  178. not. In order to make everyone happy, these global, static functions
  179. are provided to set whether or not TinyXml will condense all white space
  180. into a single space or not. The default is to condense. Note changing this
  181. value is not thread safe.
  182. */
  183. static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
  184. /// Return the current white space setting.
  185. static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
  186. /** Return the position, in the original source file, of this node or attribute.
  187. The row and column are 1-based. (That is the first row and first column is
  188. 1,1). If the returns values are 0 or less, then the parser does not have
  189. a row and column value.
  190. Generally, the row and column value will be set when the TiXmlDocument::Load(),
  191. TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
  192. when the DOM was created from operator>>.
  193. The values reflect the initial load. Once the DOM is modified programmatically
  194. (by adding or changing nodes and attributes) the new values will NOT update to
  195. reflect changes in the document.
  196. There is a minor performance cost to computing the row and column. Computation
  197. can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
  198. @sa TiXmlDocument::SetTabSize()
  199. */
  200. int Row() const { return location.row + 1; }
  201. int Column() const { return location.col + 1; } ///< See Row()
  202. void SetUserData( void* user ) { userData = user; } ///< Set a pointer to arbitrary user data.
  203. void* GetUserData() { return userData; } ///< Get a pointer to arbitrary user data.
  204. const void* GetUserData() const { return userData; } ///< Get a pointer to arbitrary user data.
  205. // Table that returs, for a given lead byte, the total number of bytes
  206. // in the UTF-8 sequence.
  207. static const int utf8ByteTable[256];
  208. virtual const char* Parse( const char* p,
  209. TiXmlParsingData* data,
  210. TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
  211. /** Expands entities in a string. Note this should not contian the tag's '<', '>', etc,
  212. or they will be transformed into entities!
  213. */
  214. static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
  215. enum
  216. {
  217. TIXML_NO_ERROR = 0,
  218. TIXML_ERROR,
  219. TIXML_ERROR_OPENING_FILE,
  220. TIXML_ERROR_PARSING_ELEMENT,
  221. TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
  222. TIXML_ERROR_READING_ELEMENT_VALUE,
  223. TIXML_ERROR_READING_ATTRIBUTES,
  224. TIXML_ERROR_PARSING_EMPTY,
  225. TIXML_ERROR_READING_END_TAG,
  226. TIXML_ERROR_PARSING_UNKNOWN,
  227. TIXML_ERROR_PARSING_COMMENT,
  228. TIXML_ERROR_PARSING_DECLARATION,
  229. TIXML_ERROR_DOCUMENT_EMPTY,
  230. TIXML_ERROR_EMBEDDED_NULL,
  231. TIXML_ERROR_PARSING_CDATA,
  232. TIXML_ERROR_DOCUMENT_TOP_ONLY,
  233. TIXML_ERROR_STRING_COUNT
  234. };
  235. protected:
  236. static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
  237. inline static bool IsWhiteSpace( char c )
  238. {
  239. return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
  240. }
  241. inline static bool IsWhiteSpace( int c )
  242. {
  243. if ( c < 256 )
  244. return IsWhiteSpace( (char) c );
  245. return false; // Again, only truly correct for English/Latin...but usually works.
  246. }
  247. #ifdef TIXML_USE_STL
  248. static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
  249. static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
  250. #endif
  251. /* Reads an XML name into the string provided. Returns
  252. a pointer just past the last character of the name,
  253. or 0 if the function has an error.
  254. */
  255. static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
  256. /* Reads text. Returns a pointer past the given end tag.
  257. Wickedly complex options, but it keeps the (sensitive) code in one place.
  258. */
  259. static const char* ReadText( const char* in, // where to start
  260. TIXML_STRING* text, // the string read
  261. bool ignoreWhiteSpace, // whether to keep the white space
  262. const char* endTag, // what ends this text
  263. bool ignoreCase, // whether to ignore case in the end tag
  264. TiXmlEncoding encoding ); // the current encoding
  265. // If an entity has been found, transform it into a character.
  266. static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
  267. // Get a character, while interpreting entities.
  268. // The length can be from 0 to 4 bytes.
  269. inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
  270. {
  271. assert( p );
  272. if ( encoding == TIXML_ENCODING_UTF8 )
  273. {
  274. *length = utf8ByteTable[ *((const unsigned char*)p) ];
  275. assert( *length >= 0 && *length < 5 );
  276. }
  277. else
  278. {
  279. *length = 1;
  280. }
  281. if ( *length == 1 )
  282. {
  283. if ( *p == '&' )
  284. return GetEntity( p, _value, length, encoding );
  285. *_value = *p;
  286. return p+1;
  287. }
  288. else if ( *length )
  289. {
  290. //strncpy( _value, p, *length ); // lots of compilers don't like this function (unsafe),
  291. // and the null terminator isn't needed
  292. for( int i=0; p[i] && i<*length; ++i ) {
  293. _value[i] = p[i];
  294. }
  295. return p + (*length);
  296. }
  297. else
  298. {
  299. // Not valid text.
  300. return 0;
  301. }
  302. }
  303. // Return true if the next characters in the stream are any of the endTag sequences.
  304. // Ignore case only works for english, and should only be relied on when comparing
  305. // to English words: StringEqual( p, "version", true ) is fine.
  306. static bool StringEqual( const char* p,
  307. const char* endTag,
  308. bool ignoreCase,
  309. TiXmlEncoding encoding );
  310. static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
  311. TiXmlCursor location;
  312. /// Field containing a generic user pointer
  313. void* userData;
  314. // None of these methods are reliable for any language except English.
  315. // Good for approximation, not great for accuracy.
  316. static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
  317. static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
  318. inline static int ToLower( int v, TiXmlEncoding encoding )
  319. {
  320. if ( encoding == TIXML_ENCODING_UTF8 )
  321. {
  322. if ( v < 128 ) return tolower( v );
  323. return v;
  324. }
  325. else
  326. {
  327. return tolower( v );
  328. }
  329. }
  330. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  331. private:
  332. TiXmlBase( const TiXmlBase& ); // not implemented.
  333. void operator=( const TiXmlBase& base ); // not allowed.
  334. struct Entity
  335. {
  336. const char* str;
  337. unsigned int strLength;
  338. char chr;
  339. };
  340. enum
  341. {
  342. NUM_ENTITY = 5,
  343. MAX_ENTITY_LENGTH = 6
  344. };
  345. static Entity entity[ NUM_ENTITY ];
  346. static bool condenseWhiteSpace;
  347. };
  348. /** The parent class for everything in the Document Object Model.
  349. (Except for attributes).
  350. Nodes have siblings, a parent, and children. A node can be
  351. in a document, or stand on its own. The type of a TiXmlNode
  352. can be queried, and it can be cast to its more defined type.
  353. */
  354. class TiXmlNode : public TiXmlBase
  355. {
  356. friend class TiXmlDocument;
  357. friend class TiXmlElement;
  358. public:
  359. #ifdef TIXML_USE_STL
  360. /** An input stream operator, for every class. Tolerant of newlines and
  361. formatting, but doesn't expect them.
  362. */
  363. friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
  364. /** An output stream operator, for every class. Note that this outputs
  365. without any newlines or formatting, as opposed to Print(), which
  366. includes tabs and new lines.
  367. The operator<< and operator>> are not completely symmetric. Writing
  368. a node to a stream is very well defined. You'll get a nice stream
  369. of output, without any extra whitespace or newlines.
  370. But reading is not as well defined. (As it always is.) If you create
  371. a TiXmlElement (for example) and read that from an input stream,
  372. the text needs to define an element or junk will result. This is
  373. true of all input streams, but it's worth keeping in mind.
  374. A TiXmlDocument will read nodes until it reads a root element, and
  375. all the children of that root element.
  376. */
  377. friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
  378. /// Appends the XML node or attribute to a std::string.
  379. friend std::string& operator<< (std::string& out, const TiXmlNode& base );
  380. #endif
  381. /** The types of XML nodes supported by TinyXml. (All the
  382. unsupported types are picked up by UNKNOWN.)
  383. */
  384. enum NodeType
  385. {
  386. TINYXML_DOCUMENT,
  387. TINYXML_ELEMENT,
  388. TINYXML_COMMENT,
  389. TINYXML_UNKNOWN,
  390. TINYXML_TEXT,
  391. TINYXML_DECLARATION,
  392. TINYXML_TYPECOUNT
  393. };
  394. virtual ~TiXmlNode();
  395. /** The meaning of 'value' changes for the specific type of
  396. TiXmlNode.
  397. @verbatim
  398. Document: filename of the xml file
  399. Element: name of the element
  400. Comment: the comment text
  401. Unknown: the tag contents
  402. Text: the text string
  403. @endverbatim
  404. The subclasses will wrap this function.
  405. */
  406. const char *Value() const { return value.c_str (); }
  407. #ifdef TIXML_USE_STL
  408. /** Return Value() as a std::string. If you only use STL,
  409. this is more efficient than calling Value().
  410. Only available in STL mode.
  411. */
  412. const std::string& ValueStr() const { return value; }
  413. #endif
  414. const TIXML_STRING& ValueTStr() const { return value; }
  415. /** Changes the value of the node. Defined as:
  416. @verbatim
  417. Document: filename of the xml file
  418. Element: name of the element
  419. Comment: the comment text
  420. Unknown: the tag contents
  421. Text: the text string
  422. @endverbatim
  423. */
  424. void SetValue(const char * _value) { value = _value;}
  425. #ifdef TIXML_USE_STL
  426. /// STL std::string form.
  427. void SetValue( const std::string& _value ) { value = _value; }
  428. #endif
  429. /// Delete all the children of this node. Does not affect 'this'.
  430. void Clear();
  431. /// One step up the DOM.
  432. TiXmlNode* Parent() { return parent; }
  433. const TiXmlNode* Parent() const { return parent; }
  434. const TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
  435. TiXmlNode* FirstChild() { return firstChild; }
  436. const TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
  437. /// The first child of this node with the matching 'value'. Will be null if none found.
  438. TiXmlNode* FirstChild( const char * _value ) {
  439. // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
  440. // call the method, cast the return back to non-const.
  441. return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
  442. }
  443. const TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
  444. TiXmlNode* LastChild() { return lastChild; }
  445. const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
  446. TiXmlNode* LastChild( const char * _value ) {
  447. return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
  448. }
  449. #ifdef TIXML_USE_STL
  450. const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); } ///< STL std::string form.
  451. TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); } ///< STL std::string form.
  452. const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); } ///< STL std::string form.
  453. TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); } ///< STL std::string form.
  454. #endif
  455. /** An alternate way to walk the children of a node.
  456. One way to iterate over nodes is:
  457. @verbatim
  458. for( child = parent->FirstChild(); child; child = child->NextSibling() )
  459. @endverbatim
  460. IterateChildren does the same thing with the syntax:
  461. @verbatim
  462. child = 0;
  463. while( child = parent->IterateChildren( child ) )
  464. @endverbatim
  465. IterateChildren takes the previous child as input and finds
  466. the next one. If the previous child is null, it returns the
  467. first. IterateChildren will return null when done.
  468. */
  469. const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
  470. TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
  471. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
  472. }
  473. /// This flavor of IterateChildren searches for children with a particular 'value'
  474. const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
  475. TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
  476. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
  477. }
  478. #ifdef TIXML_USE_STL
  479. const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
  480. TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
  481. #endif
  482. /** Add a new node related to this. Adds a child past the LastChild.
  483. Returns a pointer to the new object or NULL if an error occured.
  484. */
  485. TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
  486. /** Add a new node related to this. Adds a child past the LastChild.
  487. NOTE: the node to be added is passed by pointer, and will be
  488. henceforth owned (and deleted) by tinyXml. This method is efficient
  489. and avoids an extra copy, but should be used with care as it
  490. uses a different memory model than the other insert functions.
  491. @sa InsertEndChild
  492. */
  493. TiXmlNode* LinkEndChild( TiXmlNode* addThis );
  494. /** Add a new node related to this. Adds a child before the specified child.
  495. Returns a pointer to the new object or NULL if an error occured.
  496. */
  497. TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
  498. /** Add a new node related to this. Adds a child after the specified child.
  499. Returns a pointer to the new object or NULL if an error occured.
  500. */
  501. TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
  502. /** Replace a child of this node.
  503. Returns a pointer to the new object or NULL if an error occured.
  504. */
  505. TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
  506. /// Delete a child of this node.
  507. bool RemoveChild( TiXmlNode* removeThis );
  508. /// Navigate to a sibling node.
  509. const TiXmlNode* PreviousSibling() const { return prev; }
  510. TiXmlNode* PreviousSibling() { return prev; }
  511. /// Navigate to a sibling node.
  512. const TiXmlNode* PreviousSibling( const char * ) const;
  513. TiXmlNode* PreviousSibling( const char *_prev ) {
  514. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
  515. }
  516. #ifdef TIXML_USE_STL
  517. const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
  518. TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
  519. const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); } ///< STL std::string form.
  520. TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); } ///< STL std::string form.
  521. #endif
  522. /// Navigate to a sibling node.
  523. const TiXmlNode* NextSibling() const { return next; }
  524. TiXmlNode* NextSibling() { return next; }
  525. /// Navigate to a sibling node with the given 'value'.
  526. const TiXmlNode* NextSibling( const char * ) const;
  527. TiXmlNode* NextSibling( const char* _next ) {
  528. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
  529. }
  530. /** Convenience function to get through elements.
  531. Calls NextSibling and ToElement. Will skip all non-Element
  532. nodes. Returns 0 if there is not another element.
  533. */
  534. const TiXmlElement* NextSiblingElement() const;
  535. TiXmlElement* NextSiblingElement() {
  536. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
  537. }
  538. /** Convenience function to get through elements.
  539. Calls NextSibling and ToElement. Will skip all non-Element
  540. nodes. Returns 0 if there is not another element.
  541. */
  542. const TiXmlElement* NextSiblingElement( const char * ) const;
  543. TiXmlElement* NextSiblingElement( const char *_next ) {
  544. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
  545. }
  546. #ifdef TIXML_USE_STL
  547. const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
  548. TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
  549. #endif
  550. /// Convenience function to get through elements.
  551. const TiXmlElement* FirstChildElement() const;
  552. TiXmlElement* FirstChildElement() {
  553. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
  554. }
  555. /// Convenience function to get through elements.
  556. const TiXmlElement* FirstChildElement( const char * _value ) const;
  557. TiXmlElement* FirstChildElement( const char * _value ) {
  558. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
  559. }
  560. #ifdef TIXML_USE_STL
  561. const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
  562. TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
  563. #endif
  564. /** Query the type (as an enumerated value, above) of this node.
  565. The possible types are: DOCUMENT, ELEMENT, COMMENT,
  566. UNKNOWN, TEXT, and DECLARATION.
  567. */
  568. int Type() const { return type; }
  569. /** Return a pointer to the Document this node lives in.
  570. Returns null if not in a document.
  571. */
  572. const TiXmlDocument* GetDocument() const;
  573. TiXmlDocument* GetDocument() {
  574. return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
  575. }
  576. /// Returns true if this node has no children.
  577. bool NoChildren() const { return !firstChild; }
  578. virtual const TiXmlDocument* ToDocument() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  579. virtual const TiXmlElement* ToElement() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  580. virtual const TiXmlComment* ToComment() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  581. virtual const TiXmlUnknown* ToUnknown() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  582. virtual const TiXmlText* ToText() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  583. virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  584. virtual TiXmlDocument* ToDocument() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  585. virtual TiXmlElement* ToElement() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  586. virtual TiXmlComment* ToComment() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  587. virtual TiXmlUnknown* ToUnknown() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  588. virtual TiXmlText* ToText() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  589. virtual TiXmlDeclaration* ToDeclaration() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  590. /** Create an exact duplicate of this node and return it. The memory must be deleted
  591. by the caller.
  592. */
  593. virtual TiXmlNode* Clone() const = 0;
  594. /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
  595. XML tree will be conditionally visited and the host will be called back
  596. via the TiXmlVisitor interface.
  597. This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
  598. the XML for the callbacks, so the performance of TinyXML is unchanged by using this
  599. interface versus any other.)
  600. The interface has been based on ideas from:
  601. - http://www.saxproject.org/
  602. - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  603. Which are both good references for "visiting".
  604. An example of using Accept():
  605. @verbatim
  606. TiXmlPrinter printer;
  607. tinyxmlDoc.Accept( &printer );
  608. const char* xmlcstr = printer.CStr();
  609. @endverbatim
  610. */
  611. virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
  612. protected:
  613. TiXmlNode( NodeType _type );
  614. // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
  615. // and the assignment operator.
  616. void CopyTo( TiXmlNode* target ) const;
  617. #ifdef TIXML_USE_STL
  618. // The real work of the input operator.
  619. virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
  620. #endif
  621. // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
  622. TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
  623. TiXmlNode* parent;
  624. NodeType type;
  625. TiXmlNode* firstChild;
  626. TiXmlNode* lastChild;
  627. TIXML_STRING value;
  628. TiXmlNode* prev;
  629. TiXmlNode* next;
  630. private:
  631. TiXmlNode( const TiXmlNode& ); // not implemented.
  632. void operator=( const TiXmlNode& base ); // not allowed.
  633. };
  634. /** An attribute is a name-value pair. Elements have an arbitrary
  635. number of attributes, each with a unique name.
  636. @note The attributes are not TiXmlNodes, since they are not
  637. part of the tinyXML document object model. There are other
  638. suggested ways to look at this problem.
  639. */
  640. class TiXmlAttribute : public TiXmlBase
  641. {
  642. friend class TiXmlAttributeSet;
  643. public:
  644. /// Construct an empty attribute.
  645. TiXmlAttribute() : TiXmlBase()
  646. {
  647. document = 0;
  648. prev = next = 0;
  649. }
  650. #ifdef TIXML_USE_STL
  651. /// std::string constructor.
  652. TiXmlAttribute( const std::string& _name, const std::string& _value )
  653. {
  654. name = _name;
  655. value = _value;
  656. document = 0;
  657. prev = next = 0;
  658. }
  659. #endif
  660. /// Construct an attribute with a name and value.
  661. TiXmlAttribute( const char * _name, const char * _value )
  662. {
  663. name = _name;
  664. value = _value;
  665. document = 0;
  666. prev = next = 0;
  667. }
  668. const char* Name() const { return name.c_str(); } ///< Return the name of this attribute.
  669. const char* Value() const { return value.c_str(); } ///< Return the value of this attribute.
  670. #ifdef TIXML_USE_STL
  671. const std::string& ValueStr() const { return value; } ///< Return the value of this attribute.
  672. #endif
  673. int IntValue() const; ///< Return the value of this attribute, converted to an integer.
  674. double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
  675. // Get the tinyxml string representation
  676. const TIXML_STRING& NameTStr() const { return name; }
  677. /** QueryIntValue examines the value string. It is an alternative to the
  678. IntValue() method with richer error checking.
  679. If the value is an integer, it is stored in 'value' and
  680. the call returns TIXML_SUCCESS. If it is not
  681. an integer, it returns TIXML_WRONG_TYPE.
  682. A specialized but useful call. Note that for success it returns 0,
  683. which is the opposite of almost all other TinyXml calls.
  684. */
  685. int QueryIntValue( int* _value ) const;
  686. /// QueryDoubleValue examines the value string. See QueryIntValue().
  687. int QueryDoubleValue( double* _value ) const;
  688. void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
  689. void SetValue( const char* _value ) { value = _value; } ///< Set the value.
  690. void SetIntValue( int _value ); ///< Set the value from an integer.
  691. void SetDoubleValue( double _value ); ///< Set the value from a double.
  692. #ifdef TIXML_USE_STL
  693. /// STL std::string form.
  694. void SetName( const std::string& _name ) { name = _name; }
  695. /// STL std::string form.
  696. void SetValue( const std::string& _value ) { value = _value; }
  697. #endif
  698. /// Get the next sibling attribute in the DOM. Returns null at end.
  699. const TiXmlAttribute* Next() const;
  700. TiXmlAttribute* Next() {
  701. return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() );
  702. }
  703. /// Get the previous sibling attribute in the DOM. Returns null at beginning.
  704. const TiXmlAttribute* Previous() const;
  705. TiXmlAttribute* Previous() {
  706. return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() );
  707. }
  708. bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
  709. bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
  710. bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
  711. /* Attribute parsing starts: first letter of the name
  712. returns: the next char after the value end quote
  713. */
  714. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  715. // Prints this Attribute to a FILE stream.
  716. virtual void Print( FILE* cfile, int depth ) const {
  717. Print( cfile, depth, 0 );
  718. }
  719. void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
  720. // [internal use]
  721. // Set the document pointer so the attribute can report errors.
  722. void SetDocument( TiXmlDocument* doc ) { document = doc; }
  723. private:
  724. TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
  725. void operator=( const TiXmlAttribute& base ); // not allowed.
  726. TiXmlDocument* document; // A pointer back to a document, for error reporting.
  727. TIXML_STRING name;
  728. TIXML_STRING value;
  729. TiXmlAttribute* prev;
  730. TiXmlAttribute* next;
  731. };
  732. /* A class used to manage a group of attributes.
  733. It is only used internally, both by the ELEMENT and the DECLARATION.
  734. The set can be changed transparent to the Element and Declaration
  735. classes that use it, but NOT transparent to the Attribute
  736. which has to implement a next() and previous() method. Which makes
  737. it a bit problematic and prevents the use of STL.
  738. This version is implemented with circular lists because:
  739. - I like circular lists
  740. - it demonstrates some independence from the (typical) doubly linked list.
  741. */
  742. class TiXmlAttributeSet
  743. {
  744. public:
  745. TiXmlAttributeSet();
  746. ~TiXmlAttributeSet();
  747. void Add( TiXmlAttribute* attribute );
  748. void Remove( TiXmlAttribute* attribute );
  749. const TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  750. TiXmlAttribute* First() { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  751. const TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  752. TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  753. TiXmlAttribute* Find( const char* _name ) const;
  754. TiXmlAttribute* FindOrCreate( const char* _name );
  755. # ifdef TIXML_USE_STL
  756. TiXmlAttribute* Find( const std::string& _name ) const;
  757. TiXmlAttribute* FindOrCreate( const std::string& _name );
  758. # endif
  759. private:
  760. //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
  761. //*ME: this class must be also use a hidden/disabled copy-constructor !!!
  762. TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
  763. void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
  764. TiXmlAttribute sentinel;
  765. };
  766. /** The element is a container class. It has a value, the element name,
  767. and can contain other elements, text, comments, and unknowns.
  768. Elements also contain an arbitrary number of attributes.
  769. */
  770. class TiXmlElement : public TiXmlNode
  771. {
  772. public:
  773. /// Construct an element.
  774. TiXmlElement (const char * in_value);
  775. #ifdef TIXML_USE_STL
  776. /// std::string constructor.
  777. TiXmlElement( const std::string& _value );
  778. #endif
  779. TiXmlElement( const TiXmlElement& );
  780. void operator=( const TiXmlElement& base );
  781. virtual ~TiXmlElement();
  782. /** Given an attribute name, Attribute() returns the value
  783. for the attribute of that name, or null if none exists.
  784. */
  785. const char* Attribute( const char* name ) const;
  786. /** Given an attribute name, Attribute() returns the value
  787. for the attribute of that name, or null if none exists.
  788. If the attribute exists and can be converted to an integer,
  789. the integer value will be put in the return 'i', if 'i'
  790. is non-null.
  791. */
  792. const char* Attribute( const char* name, int* i ) const;
  793. /** Given an attribute name, Attribute() returns the value
  794. for the attribute of that name, or null if none exists.
  795. If the attribute exists and can be converted to an double,
  796. the double value will be put in the return 'd', if 'd'
  797. is non-null.
  798. */
  799. const char* Attribute( const char* name, double* d ) const;
  800. /** QueryIntAttribute examines the attribute - it is an alternative to the
  801. Attribute() method with richer error checking.
  802. If the attribute is an integer, it is stored in 'value' and
  803. the call returns TIXML_SUCCESS. If it is not
  804. an integer, it returns TIXML_WRONG_TYPE. If the attribute
  805. does not exist, then TIXML_NO_ATTRIBUTE is returned.
  806. */
  807. int QueryIntAttribute( const char* name, int* _value ) const;
  808. /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
  809. int QueryDoubleAttribute( const char* name, double* _value ) const;
  810. /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
  811. int QueryFloatAttribute( const char* name, float* _value ) const {
  812. double d;
  813. int result = QueryDoubleAttribute( name, &d );
  814. if ( result == TIXML_SUCCESS ) {
  815. *_value = (float)d;
  816. }
  817. return result;
  818. }
  819. #ifdef TIXML_USE_STL
  820. /// QueryStringAttribute examines the attribute - see QueryIntAttribute().
  821. int QueryStringAttribute( const char* name, std::string* _value ) const {
  822. const char* cstr = Attribute( name );
  823. if ( cstr ) {
  824. *_value = std::string( cstr );
  825. return TIXML_SUCCESS;
  826. }
  827. return TIXML_NO_ATTRIBUTE;
  828. }
  829. /** Template form of the attribute query which will try to read the
  830. attribute into the specified type. Very easy, very powerful, but
  831. be careful to make sure to call this with the correct type.
  832. NOTE: This method doesn't work correctly for 'string' types that contain spaces.
  833. @return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE
  834. */
  835. template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
  836. {
  837. const TiXmlAttribute* node = attributeSet.Find( name );
  838. if ( !node )
  839. return TIXML_NO_ATTRIBUTE;
  840. std::stringstream sstream( node->ValueStr() );
  841. sstream >> *outValue;
  842. if ( !sstream.fail() )
  843. return TIXML_SUCCESS;
  844. return TIXML_WRONG_TYPE;
  845. }
  846. int QueryValueAttribute( const std::string& name, std::string* outValue ) const
  847. {
  848. const TiXmlAttribute* node = attributeSet.Find( name );
  849. if ( !node )
  850. return TIXML_NO_ATTRIBUTE;
  851. *outValue = node->ValueStr();
  852. return TIXML_SUCCESS;
  853. }
  854. #endif
  855. /** Sets an attribute of name to a given value. The attribute
  856. will be created if it does not exist, or changed if it does.
  857. */
  858. void SetAttribute( const char* name, const char * _value );
  859. #ifdef TIXML_USE_STL
  860. const std::string* Attribute( const std::string& name ) const;
  861. const std::string* Attribute( const std::string& name, int* i ) const;
  862. const std::string* Attribute( const std::string& name, double* d ) const;
  863. int QueryIntAttribute( const std::string& name, int* _value ) const;
  864. int QueryDoubleAttribute( const std::string& name, double* _value ) const;
  865. /// STL std::string form.
  866. void SetAttribute( const std::string& name, const std::string& _value );
  867. ///< STL std::string form.
  868. void SetAttribute( const std::string& name, int _value );
  869. ///< STL std::string form.
  870. void SetDoubleAttribute( const std::string& name, double value );
  871. #endif
  872. /** Sets an attribute of name to a given value. The attribute
  873. will be created if it does not exist, or changed if it does.
  874. */
  875. void SetAttribute( const char * name, int value );
  876. /** Sets an attribute of name to a given value. The attribute
  877. will be created if it does not exist, or changed if it does.
  878. */
  879. void SetDoubleAttribute( const char * name, double value );
  880. /** Deletes an attribute with the given name.
  881. */
  882. void RemoveAttribute( const char * name );
  883. #ifdef TIXML_USE_STL
  884. void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
  885. #endif
  886. const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
  887. TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }
  888. const TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
  889. TiXmlAttribute* LastAttribute() { return attributeSet.Last(); }
  890. /** Convenience function for easy access to the text inside an element. Although easy
  891. and concise, GetText() is limited compared to getting the TiXmlText child
  892. and accessing it directly.
  893. If the first child of 'this' is a TiXmlText, the GetText()
  894. returns the character string of the Text node, else null is returned.
  895. This is a convenient method for getting the text of simple contained text:
  896. @verbatim
  897. <foo>This is text</foo>
  898. const char* str = fooElement->GetText();
  899. @endverbatim
  900. 'str' will be a pointer to "This is text".
  901. Note that this function can be misleading. If the element foo was created from
  902. this XML:
  903. @verbatim
  904. <foo><b>This is text</b></foo>
  905. @endverbatim
  906. then the value of str would be null. The first child node isn't a text node, it is
  907. another element. From this XML:
  908. @verbatim
  909. <foo>This is <b>text</b></foo>
  910. @endverbatim
  911. GetText() will return "This is ".
  912. WARNING: GetText() accesses a child node - don't become confused with the
  913. similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are
  914. safe type casts on the referenced node.
  915. */
  916. const char* GetText() const;
  917. /// Creates a new Element and returns it - the returned element is a copy.
  918. virtual TiXmlNode* Clone() const;
  919. // Print the Element to a FILE stream.
  920. virtual void Print( FILE* cfile, int depth ) const;
  921. /* Attribtue parsing starts: next char past '<'
  922. returns: next char past '>'
  923. */
  924. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  925. virtual const TiXmlElement* ToElement() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  926. virtual TiXmlElement* ToElement() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  927. /** Walk the XML tree visiting this node and all of its children.
  928. */
  929. virtual bool Accept( TiXmlVisitor* visitor ) const;
  930. protected:
  931. void CopyTo( TiXmlElement* target ) const;
  932. void ClearThis(); // like clear, but initializes 'this' object as well
  933. // Used to be public [internal use]
  934. #ifdef TIXML_USE_STL
  935. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  936. #endif
  937. /* [internal use]
  938. Reads the "value" of the element -- another element, or text.
  939. This should terminate with the current end tag.
  940. */
  941. const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
  942. private:
  943. TiXmlAttributeSet attributeSet;
  944. };
  945. /** An XML comment.
  946. */
  947. class TiXmlComment : public TiXmlNode
  948. {
  949. public:
  950. /// Constructs an empty comment.
  951. TiXmlComment() : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {}
  952. /// Construct a comment from text.
  953. TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {
  954. SetValue( _value );
  955. }
  956. TiXmlComment( const TiXmlComment& );
  957. void operator=( const TiXmlComment& base );
  958. virtual ~TiXmlComment() {}
  959. /// Returns a copy of this Comment.
  960. virtual TiXmlNode* Clone() const;
  961. // Write this Comment to a FILE stream.
  962. virtual void Print( FILE* cfile, int depth ) const;
  963. /* Attribtue parsing starts: at the ! of the !--
  964. returns: next char past '>'
  965. */
  966. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  967. virtual const TiXmlComment* ToComment() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  968. virtual TiXmlComment* ToComment() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  969. /** Walk the XML tree visiting this node and all of its children.
  970. */
  971. virtual bool Accept( TiXmlVisitor* visitor ) const;
  972. protected:
  973. void CopyTo( TiXmlComment* target ) const;
  974. // used to be public
  975. #ifdef TIXML_USE_STL
  976. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  977. #endif
  978. // virtual void StreamOut( TIXML_OSTREAM * out ) const;
  979. private:
  980. };
  981. /** XML text. A text node can have 2 ways to output the next. "normal" output
  982. and CDATA. It will default to the mode it was parsed from the XML file and
  983. you generally want to leave it alone, but you can change the output mode with
  984. SetCDATA() and query it with CDATA().
  985. */
  986. class TiXmlText : public TiXmlNode
  987. {
  988. friend class TiXmlElement;
  989. public:
  990. /** Constructor for text element. By default, it is treated as
  991. normal, encoded text. If you want it be output as a CDATA text
  992. element, set the parameter _cdata to 'true'
  993. */
  994. TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
  995. {
  996. SetValue( initValue );
  997. cdata = false;
  998. }
  999. virtual ~TiXmlText() {}
  1000. #ifdef TIXML_USE_STL
  1001. /// Constructor.
  1002. TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
  1003. {
  1004. SetValue( initValue );
  1005. cdata = false;
  1006. }
  1007. #endif
  1008. TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TINYXML_TEXT ) { copy.CopyTo( this ); }
  1009. void operator=( const TiXmlText& base ) { base.CopyTo( this ); }
  1010. // Write this text object to a FILE stream.
  1011. virtual void Print( FILE* cfile, int depth ) const;
  1012. /// Queries whether this represents text using a CDATA section.
  1013. bool CDATA() const { return cdata; }
  1014. /// Turns on or off a CDATA representation of text.
  1015. void SetCDATA( bool _cdata ) { cdata = _cdata; }
  1016. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1017. virtual const TiXmlText* ToText() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1018. virtual TiXmlText* ToText() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1019. /** Walk the XML tree visiting this node and all of its children.
  1020. */
  1021. virtual bool Accept( TiXmlVisitor* content ) const;
  1022. protected :
  1023. /// [internal use] Creates a new Element and returns it.
  1024. virtual TiXmlNode* Clone() const;
  1025. void CopyTo( TiXmlText* target ) const;
  1026. bool Blank() const; // returns true if all white space and new lines
  1027. // [internal use]
  1028. #ifdef TIXML_USE_STL
  1029. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1030. #endif
  1031. private:
  1032. bool cdata; // true if this should be input and output as a CDATA style text element
  1033. };
  1034. /** In correct XML the declaration is the first entry in the file.
  1035. @verbatim
  1036. <?xml version="1.0" standalone="yes"?>
  1037. @endverbatim
  1038. TinyXml will happily read or write files without a declaration,
  1039. however. There are 3 possible attributes to the declaration:
  1040. version, encoding, and standalone.
  1041. Note: In this version of the code, the attributes are
  1042. handled as special cases, not generic attributes, simply
  1043. because there can only be at most 3 and they are always the same.
  1044. */
  1045. class TiXmlDeclaration : public TiXmlNode
  1046. {
  1047. public:
  1048. /// Construct an empty declaration.
  1049. TiXmlDeclaration() : TiXmlNode( TiXmlNode::TINYXML_DECLARATION ) {}
  1050. #ifdef TIXML_USE_STL
  1051. /// Constructor.
  1052. TiXmlDeclaration( const std::string& _version,
  1053. const std::string& _encoding,
  1054. const std::string& _standalone );
  1055. #endif
  1056. /// Construct.
  1057. TiXmlDeclaration( const char* _version,
  1058. const char* _encoding,
  1059. const char* _standalone );
  1060. TiXmlDeclaration( const TiXmlDeclaration& copy );
  1061. void operator=( const TiXmlDeclaration& copy );
  1062. virtual ~TiXmlDeclaration() {}
  1063. /// Version. Will return an empty string if none was found.
  1064. const char *Version() const { return version.c_str (); }
  1065. /// Encoding. Will return an empty string if none was found.
  1066. const char *Encoding() const { return encoding.c_str (); }
  1067. /// Is this a standalone document?
  1068. const char *Standalone() const { return standalone.c_str (); }
  1069. /// Creates a copy of this Declaration and returns it.
  1070. virtual TiXmlNode* Clone() const;
  1071. // Print this declaration to a FILE stream.
  1072. virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
  1073. virtual void Print( FILE* cfile, int depth ) const {
  1074. Print( cfile, depth, 0 );
  1075. }
  1076. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1077. virtual const TiXmlDeclaration* ToDeclaration() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1078. virtual TiXmlDeclaration* ToDeclaration() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1079. /** Walk the XML tree visiting this node and all of its children.
  1080. */
  1081. virtual bool Accept( TiXmlVisitor* visitor ) const;
  1082. protected:
  1083. void CopyTo( TiXmlDeclaration* target ) const;
  1084. // used to be public
  1085. #ifdef TIXML_USE_STL
  1086. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1087. #endif
  1088. private:
  1089. TIXML_STRING version;
  1090. TIXML_STRING encoding;
  1091. TIXML_STRING standalone;
  1092. };
  1093. /** Any tag that tinyXml doesn't recognize is saved as an
  1094. unknown. It is a tag of text, but should not be modified.
  1095. It will be written back to the XML, unchanged, when the file
  1096. is saved.
  1097. DTD tags get thrown into TiXmlUnknowns.
  1098. */
  1099. class TiXmlUnknown : public TiXmlNode
  1100. {
  1101. public:
  1102. TiXmlUnknown() : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) {}
  1103. virtual ~TiXmlUnknown() {}
  1104. TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) { copy.CopyTo( this ); }
  1105. void operator=( const TiXmlUnknown& copy ) { copy.CopyTo( this ); }
  1106. /// Creates a copy of this Unknown and returns it.
  1107. virtual TiXmlNode* Clone() const;
  1108. // Print this Unknown to a FILE stream.
  1109. virtual void Print( FILE* cfile, int depth ) const;
  1110. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1111. virtual const TiXmlUnknown* ToUnknown() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1112. virtual TiXmlUnknown* ToUnknown() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1113. /** Walk the XML tree visiting this node and all of its children.
  1114. */
  1115. virtual bool Accept( TiXmlVisitor* content ) const;
  1116. protected:
  1117. void CopyTo( TiXmlUnknown* target ) const;
  1118. #ifdef TIXML_USE_STL
  1119. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1120. #endif
  1121. private:
  1122. };
  1123. /** Always the top level node. A document binds together all the
  1124. XML pieces. It can be saved, loaded, and printed to the screen.
  1125. The 'value' of a document node is the xml file name.
  1126. */
  1127. class TiXmlDocument : public TiXmlNode
  1128. {
  1129. public:
  1130. /// Create an empty document, that has no name.
  1131. TiXmlDocument();
  1132. /// Create a document with a name. The name of the document is also the filename of the xml.
  1133. TiXmlDocument( const char * documentName );
  1134. #ifdef TIXML_USE_STL
  1135. /// Constructor.
  1136. TiXmlDocument( const std::string& documentName );
  1137. #endif
  1138. TiXmlDocument( const TiXmlDocument& copy );
  1139. void operator=( const TiXmlDocument& copy );
  1140. virtual ~TiXmlDocument() {}
  1141. /** Load a file using the current document value.
  1142. Returns true if successful. Will delete any existing
  1143. document data before loading.
  1144. */
  1145. bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1146. /// Save a file using the current document value. Returns true if successful.
  1147. bool SaveFile() const;
  1148. /// Load a file using the given filename. Returns true if successful.
  1149. bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1150. /// Save a file using the given filename. Returns true if successful.
  1151. bool SaveFile( const char * filename ) const;
  1152. /** Load a file using the given FILE*. Returns true if successful. Note that this method
  1153. doesn't stream - the entire object pointed at by the FILE*
  1154. will be interpreted as an XML file. TinyXML doesn't stream in XML from the current
  1155. file location. Streaming may be added in the future.
  1156. */
  1157. bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1158. /// Save a file using the given FILE*. Returns true if successful.
  1159. bool SaveFile( FILE* ) const;
  1160. #ifdef TIXML_USE_STL
  1161. bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) ///< STL std::string version.
  1162. {
  1163. return LoadFile( filename.c_str(), encoding );
  1164. }
  1165. bool SaveFile( const std::string& filename ) const ///< STL std::string version.
  1166. {
  1167. return SaveFile( filename.c_str() );
  1168. }
  1169. #endif
  1170. /** Parse the given null terminated block of xml data. Passing in an encoding to this
  1171. method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
  1172. to use that encoding, regardless of what TinyXml might otherwise try to detect.
  1173. */
  1174. virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1175. /** Get the root element -- the only top level element -- of the document.
  1176. In well formed XML, there should only be one. TinyXml is tolerant of
  1177. multiple elements at the document level.
  1178. */
  1179. const TiXmlElement* RootElement() const { return FirstChildElement(); }
  1180. TiXmlElement* RootElement() { return FirstChildElement(); }
  1181. /** If an error occurs, Error will be set to true. Also,
  1182. - The ErrorId() will contain the integer identifier of the error (not generally useful)
  1183. - The ErrorDesc() method will return the name of the error. (very useful)
  1184. - The ErrorRow() and ErrorCol() will return the location of the error (if known)
  1185. */
  1186. bool Error() const { return error; }
  1187. /// Contains a textual (english) description of the error if one occurs.
  1188. const char * ErrorDesc() const { return errorDesc.c_str (); }
  1189. /** Generally, you probably want the error string ( ErrorDesc() ). But if you
  1190. prefer the ErrorId, this function will fetch it.
  1191. */
  1192. int ErrorId() const { return errorId; }
  1193. /** Returns the location (if known) of the error. The first column is column 1,
  1194. and the first row is row 1. A value of 0 means the row and column wasn't applicable
  1195. (memory errors, for example, have no row/column) or the parser lost the error. (An
  1196. error in the error reporting, in that case.)
  1197. @sa SetTabSize, Row, Column
  1198. */
  1199. int ErrorRow() const { return errorLocation.row+1; }
  1200. int ErrorCol() const { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
  1201. /** SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol())
  1202. to report the correct values for row and column. It does not change the output
  1203. or input in any way.
  1204. By calling this method, with a tab size
  1205. greater than 0, the row and column of each node and attribute is stored
  1206. when the file is loaded. Very useful for tracking the DOM back in to
  1207. the source file.
  1208. The tab size is required for calculating the location of nodes. If not
  1209. set, the default of 4 is used. The tabsize is set per document. Setting
  1210. the tabsize to 0 disables row/column tracking.
  1211. Note that row and column tracking is not supported when using operator>>.
  1212. The tab size needs to be enabled before the parse or load. Correct usage:
  1213. @verbatim
  1214. TiXmlDocument doc;
  1215. doc.SetTabSize( 8 );
  1216. doc.Load( "myfile.xml" );
  1217. @endverbatim
  1218. @sa Row, Column
  1219. */
  1220. void SetTabSize( int _tabsize ) { tabsize = _tabsize; }
  1221. int TabSize() const { return tabsize; }
  1222. /** If you have handled the error, it can be reset with this call. The error
  1223. state is automatically cleared if you Parse a new XML block.
  1224. */
  1225. void ClearError() { error = false;
  1226. errorId = 0;
  1227. errorDesc = "";
  1228. errorLocation.row = errorLocation.col = 0;
  1229. //errorLocation.last = 0;
  1230. }
  1231. /** Write the document to standard out using formatted printing ("pretty print"). */
  1232. void Print() const { Print( stdout, 0 ); }
  1233. /* Write the document to a string using formatted printing ("pretty print"). This
  1234. will allocate a character array (new char[]) and return it as a pointer. The
  1235. calling code pust call delete[] on the return char* to avoid a memory leak.
  1236. */
  1237. //char* PrintToMemory() const;
  1238. /// Print this Document to a FILE stream.
  1239. virtual void Print( FILE* cfile, int depth = 0 ) const;
  1240. // [internal use]
  1241. void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
  1242. virtual const TiXmlDocument* ToDocument() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1243. virtual TiXmlDocument* ToDocument() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1244. /** Walk the XML tree visiting this node and all of its children.
  1245. */
  1246. virtual bool Accept( TiXmlVisitor* content ) const;
  1247. protected :
  1248. // [internal use]
  1249. virtual TiXmlNode* Clone() const;
  1250. #ifdef TIXML_USE_STL
  1251. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1252. #endif
  1253. private:
  1254. void CopyTo( TiXmlDocument* target ) const;
  1255. bool error;
  1256. int errorId;
  1257. TIXML_STRING errorDesc;
  1258. int tabsize;
  1259. TiXmlCursor errorLocation;
  1260. bool useMicrosoftBOM; // the UTF-8 BOM were found when read. Note this, and try to write.
  1261. };
  1262. /**
  1263. A TiXmlHandle is a class that wraps a node pointer with null checks; this is
  1264. an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
  1265. DOM structure. It is a separate utility class.
  1266. Take an example:
  1267. @verbatim
  1268. <Document>
  1269. <Element attributeA = "valueA">
  1270. <Child attributeB = "value1" />
  1271. <Child attributeB = "value2" />
  1272. </Element>
  1273. <Document>
  1274. @endverbatim
  1275. Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
  1276. easy to write a *lot* of code that looks like:
  1277. @verbatim
  1278. TiXmlElement* root = document.FirstChildElement( "Document" );
  1279. if ( root )
  1280. {
  1281. TiXmlElement* element = root->FirstChildElement( "Element" );
  1282. if ( element )
  1283. {
  1284. TiXmlElement* child = element->FirstChildElement( "Child" );
  1285. if ( child )
  1286. {
  1287. TiXmlElement* child2 = child->NextSiblingElement( "Child" );
  1288. if ( child2 )
  1289. {
  1290. // Finally do something useful.
  1291. @endverbatim
  1292. And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
  1293. of such code. A TiXmlHandle checks for null pointers so it is perfectly safe
  1294. and correct to use:
  1295. @verbatim
  1296. TiXmlHandle docHandle( &document );
  1297. TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
  1298. if ( child2 )
  1299. {
  1300. // do something useful
  1301. @endverbatim
  1302. Which is MUCH more concise and useful.
  1303. It is also safe to copy handles - internally they are nothing more than node pointers.
  1304. @verbatim
  1305. TiXmlHandle handleCopy = handle;
  1306. @endverbatim
  1307. What they should not be used for is iteration:
  1308. @verbatim
  1309. int i=0;
  1310. while ( true )
  1311. {
  1312. TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
  1313. if ( !child )
  1314. break;
  1315. // do something
  1316. ++i;
  1317. }
  1318. @endverbatim
  1319. It seems reasonable, but it is in fact two embedded while loops. The Child method is
  1320. a linear walk to find the element, so this code would iterate much more than it needs
  1321. to. Instead, prefer:
  1322. @verbatim
  1323. TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();
  1324. for( child; child; child=child->NextSiblingElement() )
  1325. {
  1326. // do something
  1327. }
  1328. @endverbatim
  1329. */
  1330. class TiXmlHandle
  1331. {
  1332. public:
  1333. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
  1334. TiXmlHandle( TiXmlNode* _node ) { this->node = _node; }
  1335. /// Copy constructor
  1336. TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
  1337. TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
  1338. /// Return a handle to the first child node.
  1339. TiXmlHandle FirstChild() const;
  1340. /// Return a handle to the first child node with the given name.
  1341. TiXmlHandle FirstChild( const char * value ) const;
  1342. /// Return a handle to the first child element.
  1343. TiXmlHandle FirstChildElement() const;
  1344. /// Return a handle to the first child element with the given name.
  1345. TiXmlHandle FirstChildElement( const char * value ) const;
  1346. /** Return a handle to the "index" child with the given name.
  1347. The first child is 0, the second 1, etc.
  1348. */
  1349. TiXmlHandle Child( const char* value, int index ) const;
  1350. /** Return a handle to the "index" child.
  1351. The first child is 0, the second 1, etc.
  1352. */
  1353. TiXmlHandle Child( int index ) const;
  1354. /** Return a handle to the "index" child element with the given name.
  1355. The first child element is 0, the second 1, etc. Note that only TiXmlElements
  1356. are indexed: other types are not counted.
  1357. */
  1358. TiXmlHandle ChildElement( const char* value, int index ) const;
  1359. /** Return a handle to the "index" child element.
  1360. The first child element is 0, the second 1, etc. Note that only TiXmlElements
  1361. are indexed: other types are not counted.
  1362. */
  1363. TiXmlHandle ChildElement( int index ) const;
  1364. #ifdef TIXML_USE_STL
  1365. TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
  1366. TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
  1367. TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
  1368. TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
  1369. #endif
  1370. /** Return the handle as a TiXmlNode. This may return null.
  1371. */
  1372. TiXmlNode* ToNode() const { return node; }
  1373. /** Return the handle as a TiXmlElement. This may return null.
  1374. */
  1375. TiXmlElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
  1376. /** Return the handle as a TiXmlText. This may return null.
  1377. */
  1378. TiXmlText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
  1379. /** Return the handle as a TiXmlUnknown. This may return null.
  1380. */
  1381. TiXmlUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
  1382. /** @deprecated use ToNode.
  1383. Return the handle as a TiXmlNode. This may return null.
  1384. */
  1385. TiXmlNode* Node() const { return ToNode(); }
  1386. /** @deprecated use ToElement.
  1387. Return the handle as a TiXmlElement. This may return null.
  1388. */
  1389. TiXmlElement* Element() const { return ToElement(); }
  1390. /** @deprecated use ToText()
  1391. Return the handle as a TiXmlText. This may return null.
  1392. */
  1393. TiXmlText* Text() const { return ToText(); }
  1394. /** @deprecated use ToUnknown()
  1395. Return the handle as a TiXmlUnknown. This may return null.
  1396. */
  1397. TiXmlUnknown* Unknown() const { return ToUnknown(); }
  1398. private:
  1399. TiXmlNode* node;
  1400. };
  1401. /** Print to memory functionality. The TiXmlPrinter is useful when you need to:
  1402. -# Print to memory (especially in non-STL mode)
  1403. -# Control formatting (line endings, etc.)
  1404. When constructed, the TiXmlPrinter is in its default "pretty printing" mode.
  1405. Before calling Accept() you can call methods to control the printing
  1406. of the XML document. After TiXmlNode::Accept() is called, the printed document can
  1407. be accessed via the CStr(), Str(), and Size() methods.
  1408. TiXmlPrinter uses the Visitor API.
  1409. @verbatim
  1410. TiXmlPrinter printer;
  1411. printer.SetIndent( "\t" );
  1412. doc.Accept( &printer );
  1413. fprintf( stdout, "%s", printer.CStr() );
  1414. @endverbatim
  1415. */
  1416. class TiXmlPrinter : public TiXmlVisitor
  1417. {
  1418. public:
  1419. TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
  1420. buffer(), indent( " " ), lineBreak( "\n" ) {}
  1421. virtual bool VisitEnter( const TiXmlDocument& doc );
  1422. virtual bool VisitExit( const TiXmlDocument& doc );
  1423. virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
  1424. virtual bool VisitExit( const TiXmlElement& element );
  1425. virtual bool Visit( const TiXmlDeclaration& declaration );
  1426. virtual bool Visit( const TiXmlText& text );
  1427. virtual bool Visit( const TiXmlComment& comment );
  1428. virtual bool Visit( const TiXmlUnknown& unknown );
  1429. /** Set the indent characters for printing. By default 4 spaces
  1430. but tab (\t) is also useful, or null/empty string for no indentation.
  1431. */
  1432. void SetIndent( const char* _indent ) { indent = _indent ? _indent : "" ; }
  1433. /// Query the indention string.
  1434. const char* Indent() { return indent.c_str(); }
  1435. /** Set the line breaking string. By default set to newline (\n).
  1436. Some operating systems prefer other characters, or can be
  1437. set to the null/empty string for no indenation.
  1438. */
  1439. void SetLineBreak( const char* _lineBreak ) { lineBreak = _lineBreak ? _lineBreak : ""; }
  1440. /// Query the current line breaking string.
  1441. const char* LineBreak() { return lineBreak.c_str(); }
  1442. /** Switch over to "stream printing" which is the most dense formatting without
  1443. linebreaks. Common when the XML is needed for network transmission.
  1444. */
  1445. void SetStreamPrinting() { indent = "";
  1446. lineBreak = "";
  1447. }
  1448. /// Return the result.
  1449. const char* CStr() { return buffer.c_str(); }
  1450. /// Return the length of the result string.
  1451. size_t Size() { return buffer.size(); }
  1452. #ifdef TIXML_USE_STL
  1453. /// Return the result.
  1454. const std::string& Str() { return buffer; }
  1455. #endif
  1456. private:
  1457. void DoIndent() {
  1458. for( int i=0; i<depth; ++i )
  1459. buffer += indent;
  1460. }
  1461. void DoLineBreak() {
  1462. buffer += lineBreak;
  1463. }
  1464. int depth;
  1465. bool simpleTextPrint;
  1466. TIXML_STRING buffer;
  1467. TIXML_STRING indent;
  1468. TIXML_STRING lineBreak;
  1469. };
  1470. #ifdef _MSC_VER
  1471. #pragma warning( pop )
  1472. #endif
  1473. #endif