tinyxml.h 63 KB

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