xmltest.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. #if defined( _MSC_VER )
  2. #define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
  3. #endif
  4. #include "tinyxml2.h"
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <ctime>
  8. #if defined( _MSC_VER )
  9. #include <direct.h> // _mkdir
  10. #include <crtdbg.h>
  11. #define WIN32_LEAN_AND_MEAN
  12. #include <windows.h>
  13. _CrtMemState startMemState;
  14. _CrtMemState endMemState;
  15. #elif defined(MINGW32) || defined(__MINGW32__)
  16. #include <io.h> // mkdir
  17. #else
  18. #include <sys/stat.h> // mkdir
  19. #endif
  20. using namespace tinyxml2;
  21. int gPass = 0;
  22. int gFail = 0;
  23. bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
  24. {
  25. bool pass = !strcmp( expected, found );
  26. if ( pass )
  27. printf ("[pass]");
  28. else
  29. printf ("[fail]");
  30. if ( !echo ) {
  31. printf (" %s\n", testString);
  32. }
  33. else {
  34. if ( extraNL ) {
  35. printf( " %s\n", testString );
  36. printf( "%s\n", expected );
  37. printf( "%s\n", found );
  38. }
  39. else {
  40. printf (" %s [%s][%s]\n", testString, expected, found);
  41. }
  42. }
  43. if ( pass )
  44. ++gPass;
  45. else
  46. ++gFail;
  47. return pass;
  48. }
  49. template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
  50. {
  51. bool pass = ( expected == found );
  52. if ( pass )
  53. printf ("[pass]");
  54. else
  55. printf ("[fail]");
  56. if ( !echo )
  57. printf (" %s\n", testString);
  58. else
  59. printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
  60. if ( pass )
  61. ++gPass;
  62. else
  63. ++gFail;
  64. return pass;
  65. }
  66. void NullLineEndings( char* p )
  67. {
  68. while( p && *p ) {
  69. if ( *p == '\n' || *p == '\r' ) {
  70. *p = 0;
  71. return;
  72. }
  73. ++p;
  74. }
  75. }
  76. int example_1()
  77. {
  78. XMLDocument doc;
  79. doc.LoadFile( "resources/dream.xml" );
  80. return doc.ErrorID();
  81. }
  82. /** @page Example-1 Load an XML File
  83. * @dontinclude ./xmltest.cpp
  84. * Basic XML file loading.
  85. * The basic syntax to load an XML file from
  86. * disk and check for an error. (ErrorID()
  87. * will return 0 for no error.)
  88. * @skip example_1()
  89. * @until }
  90. */
  91. int example_2()
  92. {
  93. static const char* xml = "<element/>";
  94. XMLDocument doc;
  95. doc.Parse( xml );
  96. return doc.ErrorID();
  97. }
  98. /** @page Example-2 Parse an XML from char buffer
  99. * @dontinclude ./xmltest.cpp
  100. * Basic XML string parsing.
  101. * The basic syntax to parse an XML for
  102. * a char* and check for an error. (ErrorID()
  103. * will return 0 for no error.)
  104. * @skip example_2()
  105. * @until }
  106. */
  107. int example_3()
  108. {
  109. static const char* xml =
  110. "<?xml version=\"1.0\"?>"
  111. "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
  112. "<PLAY>"
  113. "<TITLE>A Midsummer Night's Dream</TITLE>"
  114. "</PLAY>";
  115. XMLDocument doc;
  116. doc.Parse( xml );
  117. XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
  118. const char* title = titleElement->GetText();
  119. printf( "Name of play (1): %s\n", title );
  120. XMLText* textNode = titleElement->FirstChild()->ToText();
  121. title = textNode->Value();
  122. printf( "Name of play (2): %s\n", title );
  123. return doc.ErrorID();
  124. }
  125. /** @page Example-3 Get information out of XML
  126. @dontinclude ./xmltest.cpp
  127. In this example, we navigate a simple XML
  128. file, and read some interesting text. Note
  129. that this example doesn't use error
  130. checking; working code should check for null
  131. pointers when walking an XML tree, or use
  132. XMLHandle.
  133. (The XML is an excerpt from "dream.xml").
  134. @skip example_3()
  135. @until </PLAY>";
  136. The structure of the XML file is:
  137. <ul>
  138. <li>(declaration)</li>
  139. <li>(dtd stuff)</li>
  140. <li>Element "PLAY"</li>
  141. <ul>
  142. <li>Element "TITLE"</li>
  143. <ul>
  144. <li>Text "A Midsummer Night's Dream"</li>
  145. </ul>
  146. </ul>
  147. </ul>
  148. For this example, we want to print out the
  149. title of the play. The text of the title (what
  150. we want) is child of the "TITLE" element which
  151. is a child of the "PLAY" element.
  152. We want to skip the declaration and dtd, so the
  153. method FirstChildElement() is a good choice. The
  154. FirstChildElement() of the Document is the "PLAY"
  155. Element, the FirstChildElement() of the "PLAY" Element
  156. is the "TITLE" Element.
  157. @until ( "TITLE" );
  158. We can then use the convenience function GetText()
  159. to get the title of the play.
  160. @until title );
  161. Text is just another Node in the XML DOM. And in
  162. fact you should be a little cautious with it, as
  163. text nodes can contain elements.
  164. @verbatim
  165. Consider: A Midsummer Night's <b>Dream</b>
  166. @endverbatim
  167. It is more correct to actually query the Text Node
  168. if in doubt:
  169. @until title );
  170. Noting that here we use FirstChild() since we are
  171. looking for XMLText, not an element, and ToText()
  172. is a cast from a Node to a XMLText.
  173. */
  174. bool example_4()
  175. {
  176. static const char* xml =
  177. "<information>"
  178. " <attributeApproach v='2' />"
  179. " <textApproach>"
  180. " <v>2</v>"
  181. " </textApproach>"
  182. "</information>";
  183. XMLDocument doc;
  184. doc.Parse( xml );
  185. int v0 = 0;
  186. int v1 = 0;
  187. XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
  188. attributeApproachElement->QueryIntAttribute( "v", &v0 );
  189. XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
  190. textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
  191. printf( "Both values are the same: %d and %d\n", v0, v1 );
  192. return !doc.Error() && ( v0 == v1 );
  193. }
  194. /** @page Example-4 Read attributes and text information.
  195. @dontinclude ./xmltest.cpp
  196. There are fundamentally 2 ways of writing a key-value
  197. pair into an XML file. (Something that's always annoyed
  198. me about XML.) Either by using attributes, or by writing
  199. the key name into an element and the value into
  200. the text node wrapped by the element. Both approaches
  201. are illustrated in this example, which shows two ways
  202. to encode the value "2" into the key "v":
  203. @skip example_4()
  204. @until "</information>";
  205. TinyXML-2 has accessors for both approaches.
  206. When using an attribute, you navigate to the XMLElement
  207. with that attribute and use the QueryIntAttribute()
  208. group of methods. (Also QueryFloatAttribute(), etc.)
  209. @skip XMLElement* attributeApproachElement
  210. @until &v0 );
  211. When using the text approach, you need to navigate
  212. down one more step to the XMLElement that contains
  213. the text. Note the extra FirstChildElement( "v" )
  214. in the code below. The value of the text can then
  215. be safely queried with the QueryIntText() group
  216. of methods. (Also QueryFloatText(), etc.)
  217. @skip XMLElement* textApproachElement
  218. @until &v1 );
  219. */
  220. int main( int argc, const char ** argv )
  221. {
  222. #if defined( _MSC_VER ) && defined( DEBUG )
  223. _CrtMemCheckpoint( &startMemState );
  224. #endif
  225. #if defined(_MSC_VER) || defined(MINGW32) || defined(__MINGW32__)
  226. #if defined __MINGW64_VERSION_MAJOR && defined __MINGW64_VERSION_MINOR
  227. //MINGW64: both 32 and 64-bit
  228. mkdir( "resources/out/" );
  229. #else
  230. _mkdir( "resources/out/" );
  231. #endif
  232. #else
  233. mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  234. #endif
  235. if ( argc > 1 ) {
  236. XMLDocument* doc = new XMLDocument();
  237. clock_t startTime = clock();
  238. doc->LoadFile( argv[1] );
  239. clock_t loadTime = clock();
  240. int errorID = doc->ErrorID();
  241. delete doc; doc = 0;
  242. clock_t deleteTime = clock();
  243. printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
  244. if ( !errorID ) {
  245. printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
  246. printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
  247. printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
  248. }
  249. exit(0);
  250. }
  251. FILE* fp = fopen( "resources/dream.xml", "r" );
  252. if ( !fp ) {
  253. printf( "Error opening test file 'dream.xml'.\n"
  254. "Is your working directory the same as where \n"
  255. "the xmltest.cpp and dream.xml file are?\n\n"
  256. #if defined( _MSC_VER )
  257. "In windows Visual Studio you may need to set\n"
  258. "Properties->Debugging->Working Directory to '..'\n"
  259. #endif
  260. );
  261. exit( 1 );
  262. }
  263. fclose( fp );
  264. XMLTest( "Example-1", 0, example_1() );
  265. XMLTest( "Example-2", 0, example_2() );
  266. XMLTest( "Example-3", 0, example_3() );
  267. XMLTest( "Example-4", true, example_4() );
  268. /* ------ Example 2: Lookup information. ---- */
  269. {
  270. static const char* test[] = { "<element />",
  271. "<element></element>",
  272. "<element><subelement/></element>",
  273. "<element><subelement></subelement></element>",
  274. "<element><subelement><subsub/></subelement></element>",
  275. "<!--comment beside elements--><element><subelement></subelement></element>",
  276. "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
  277. "<element attrib1='foo' attrib2=\"bar\" ></element>",
  278. "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
  279. "<element>Text inside element.</element>",
  280. "<element><b></b></element>",
  281. "<element>Text inside and <b>bolded</b> in the element.</element>",
  282. "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
  283. "<element>This &amp; That.</element>",
  284. "<element attrib='This&lt;That' />",
  285. 0
  286. };
  287. for( int i=0; test[i]; ++i ) {
  288. XMLDocument doc;
  289. doc.Parse( test[i] );
  290. doc.Print();
  291. printf( "----------------------------------------------\n" );
  292. }
  293. }
  294. #if 1
  295. {
  296. static const char* test = "<!--hello world\n"
  297. " line 2\r"
  298. " line 3\r\n"
  299. " line 4\n\r"
  300. " line 5\r-->";
  301. XMLDocument doc;
  302. doc.Parse( test );
  303. doc.Print();
  304. }
  305. {
  306. static const char* test = "<element>Text before.</element>";
  307. XMLDocument doc;
  308. doc.Parse( test );
  309. XMLElement* root = doc.FirstChildElement();
  310. XMLElement* newElement = doc.NewElement( "Subelement" );
  311. root->InsertEndChild( newElement );
  312. doc.Print();
  313. }
  314. {
  315. XMLDocument* doc = new XMLDocument();
  316. static const char* test = "<element><sub/></element>";
  317. doc->Parse( test );
  318. delete doc;
  319. }
  320. {
  321. // Test: Programmatic DOM
  322. // Build:
  323. // <element>
  324. // <!--comment-->
  325. // <sub attrib="1" />
  326. // <sub attrib="2" />
  327. // <sub attrib="3" >& Text!</sub>
  328. // <element>
  329. XMLDocument* doc = new XMLDocument();
  330. XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
  331. XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
  332. for( int i=0; i<3; ++i ) {
  333. sub[i]->SetAttribute( "attrib", i );
  334. }
  335. element->InsertEndChild( sub[2] );
  336. XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
  337. element->InsertAfterChild( comment, sub[0] );
  338. element->InsertAfterChild( sub[0], sub[1] );
  339. sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
  340. doc->Print();
  341. XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
  342. XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
  343. XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
  344. XMLTest( "Programmatic DOM", "& Text!",
  345. doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
  346. // And now deletion:
  347. element->DeleteChild( sub[2] );
  348. doc->DeleteNode( comment );
  349. element->FirstChildElement()->SetAttribute( "attrib", true );
  350. element->LastChildElement()->DeleteAttribute( "attrib" );
  351. XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
  352. int value = 10;
  353. int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
  354. XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
  355. XMLTest( "Programmatic DOM", value, 10 );
  356. doc->Print();
  357. {
  358. XMLPrinter streamer;
  359. doc->Print( &streamer );
  360. printf( "%s", streamer.CStr() );
  361. }
  362. {
  363. XMLPrinter streamer( 0, true );
  364. doc->Print( &streamer );
  365. XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
  366. }
  367. doc->SaveFile( "./resources/out/pretty.xml" );
  368. doc->SaveFile( "./resources/out/compact.xml", true );
  369. delete doc;
  370. }
  371. {
  372. // Test: Dream
  373. // XML1 : 1,187,569 bytes in 31,209 allocations
  374. // XML2 : 469,073 bytes in 323 allocations
  375. //int newStart = gNew;
  376. XMLDocument doc;
  377. doc.LoadFile( "resources/dream.xml" );
  378. doc.SaveFile( "resources/out/dreamout.xml" );
  379. doc.PrintError();
  380. XMLTest( "Dream", "xml version=\"1.0\"",
  381. doc.FirstChild()->ToDeclaration()->Value() );
  382. XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  383. XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  384. doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
  385. XMLTest( "Dream", "And Robin shall restore amends.",
  386. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  387. XMLTest( "Dream", "And Robin shall restore amends.",
  388. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  389. XMLDocument doc2;
  390. doc2.LoadFile( "resources/out/dreamout.xml" );
  391. XMLTest( "Dream-out", "xml version=\"1.0\"",
  392. doc2.FirstChild()->ToDeclaration()->Value() );
  393. XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  394. XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  395. doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
  396. XMLTest( "Dream-out", "And Robin shall restore amends.",
  397. doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  398. //gNewTotal = gNew - newStart;
  399. }
  400. {
  401. const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
  402. "<passages count=\"006\" formatversion=\"20020620\">\n"
  403. " <wrong error>\n"
  404. "</passages>";
  405. XMLDocument doc;
  406. doc.Parse( error );
  407. XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
  408. }
  409. {
  410. const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
  411. XMLDocument doc;
  412. doc.Parse( str );
  413. XMLElement* ele = doc.FirstChildElement();
  414. int iVal, result;
  415. double dVal;
  416. result = ele->QueryDoubleAttribute( "attr0", &dVal );
  417. XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
  418. XMLTest( "Query attribute: int as double", (int)dVal, 1 );
  419. result = ele->QueryDoubleAttribute( "attr1", &dVal );
  420. XMLTest( "Query attribute: double as double", result, (int)XML_NO_ERROR );
  421. XMLTest( "Query attribute: double as double", (int)dVal, 2 );
  422. result = ele->QueryIntAttribute( "attr1", &iVal );
  423. XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
  424. XMLTest( "Query attribute: double as int", iVal, 2 );
  425. result = ele->QueryIntAttribute( "attr2", &iVal );
  426. XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
  427. result = ele->QueryIntAttribute( "bar", &iVal );
  428. XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
  429. }
  430. {
  431. const char* str = "<doc/>";
  432. XMLDocument doc;
  433. doc.Parse( str );
  434. XMLElement* ele = doc.FirstChildElement();
  435. int iVal, iVal2;
  436. double dVal, dVal2;
  437. ele->SetAttribute( "str", "strValue" );
  438. ele->SetAttribute( "int", 1 );
  439. ele->SetAttribute( "double", -1.0 );
  440. const char* cStr = ele->Attribute( "str" );
  441. ele->QueryIntAttribute( "int", &iVal );
  442. ele->QueryDoubleAttribute( "double", &dVal );
  443. ele->QueryAttribute( "int", &iVal2 );
  444. ele->QueryAttribute( "double", &dVal2 );
  445. XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
  446. XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
  447. XMLTest( "Attribute round trip. int.", 1, iVal );
  448. XMLTest( "Attribute round trip. double.", -1, (int)dVal );
  449. XMLTest( "Alternate query", true, iVal == iVal2 );
  450. XMLTest( "Alternate query", true, dVal == dVal2 );
  451. }
  452. {
  453. XMLDocument doc;
  454. doc.LoadFile( "resources/utf8test.xml" );
  455. // Get the attribute "value" from the "Russian" element and check it.
  456. XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
  457. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  458. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  459. XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
  460. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  461. 0xd1U, 0x81U, 0xd1U, 0x81U,
  462. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  463. 0xd0U, 0xb9U, 0 };
  464. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  465. XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
  466. XMLTest( "UTF-8: Browsing russian element name.",
  467. russianText,
  468. text->Value() );
  469. // Now try for a round trip.
  470. doc.SaveFile( "resources/out/utf8testout.xml" );
  471. // Check the round trip.
  472. int okay = 0;
  473. FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
  474. FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
  475. if ( saved && verify )
  476. {
  477. okay = 1;
  478. char verifyBuf[256];
  479. while ( fgets( verifyBuf, 256, verify ) )
  480. {
  481. char savedBuf[256];
  482. fgets( savedBuf, 256, saved );
  483. NullLineEndings( verifyBuf );
  484. NullLineEndings( savedBuf );
  485. if ( strcmp( verifyBuf, savedBuf ) )
  486. {
  487. printf( "verify:%s<\n", verifyBuf );
  488. printf( "saved :%s<\n", savedBuf );
  489. okay = 0;
  490. break;
  491. }
  492. }
  493. }
  494. if ( saved )
  495. fclose( saved );
  496. if ( verify )
  497. fclose( verify );
  498. XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  499. }
  500. // --------GetText()-----------
  501. {
  502. const char* str = "<foo>This is text</foo>";
  503. XMLDocument doc;
  504. doc.Parse( str );
  505. const XMLElement* element = doc.RootElement();
  506. XMLTest( "GetText() normal use.", "This is text", element->GetText() );
  507. str = "<foo><b>This is text</b></foo>";
  508. doc.Parse( str );
  509. element = doc.RootElement();
  510. XMLTest( "GetText() contained element.", element->GetText() == 0, true );
  511. }
  512. // --------SetText()-----------
  513. {
  514. const char* str = "<foo></foo>";
  515. XMLDocument doc;
  516. doc.Parse( str );
  517. XMLElement* element = doc.RootElement();
  518. element->SetText("darkness.");
  519. XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() );
  520. element->SetText("blue flame.");
  521. XMLTest( "SetText() replace.", "blue flame.", element->GetText() );
  522. str = "<foo/>";
  523. doc.Parse( str );
  524. element = doc.RootElement();
  525. element->SetText("The driver");
  526. XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() );
  527. element->SetText("<b>horses</b>");
  528. XMLTest( "SetText() replace with tag-like text.", "<b>horses</b>", element->GetText() );
  529. //doc.Print();
  530. str = "<foo><bar>Text in nested element</bar></foo>";
  531. doc.Parse( str );
  532. element = doc.RootElement();
  533. element->SetText("wolves");
  534. XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() );
  535. str = "<foo/>";
  536. doc.Parse( str );
  537. element = doc.RootElement();
  538. element->SetText( "str" );
  539. XMLTest( "SetText types", "str", element->GetText() );
  540. element->SetText( 1 );
  541. XMLTest( "SetText types", "1", element->GetText() );
  542. element->SetText( 1U );
  543. XMLTest( "SetText types", "1", element->GetText() );
  544. element->SetText( true );
  545. XMLTest( "SetText types", "1", element->GetText() ); // TODO: should be 'true'?
  546. element->SetText( 1.5f );
  547. XMLTest( "SetText types", "1.5", element->GetText() );
  548. element->SetText( 1.5 );
  549. XMLTest( "SetText types", "1.5", element->GetText() );
  550. }
  551. // ---------- CDATA ---------------
  552. {
  553. const char* str = "<xmlElement>"
  554. "<![CDATA["
  555. "I am > the rules!\n"
  556. "...since I make symbolic puns"
  557. "]]>"
  558. "</xmlElement>";
  559. XMLDocument doc;
  560. doc.Parse( str );
  561. doc.Print();
  562. XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  563. "I am > the rules!\n...since I make symbolic puns",
  564. false );
  565. }
  566. // ----------- CDATA -------------
  567. {
  568. const char* str = "<xmlElement>"
  569. "<![CDATA["
  570. "<b>I am > the rules!</b>\n"
  571. "...since I make symbolic puns"
  572. "]]>"
  573. "</xmlElement>";
  574. XMLDocument doc;
  575. doc.Parse( str );
  576. doc.Print();
  577. XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  578. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  579. false );
  580. }
  581. // InsertAfterChild causes crash.
  582. {
  583. // InsertBeforeChild and InsertAfterChild causes crash.
  584. XMLDocument doc;
  585. XMLElement* parent = doc.NewElement( "Parent" );
  586. doc.InsertFirstChild( parent );
  587. XMLElement* childText0 = doc.NewElement( "childText0" );
  588. XMLElement* childText1 = doc.NewElement( "childText1" );
  589. XMLNode* childNode0 = parent->InsertEndChild( childText0 );
  590. XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
  591. XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
  592. }
  593. {
  594. // Entities not being written correctly.
  595. // From Lynn Allen
  596. const char* passages =
  597. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  598. "<passages count=\"006\" formatversion=\"20020620\">"
  599. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  600. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  601. "</passages>";
  602. XMLDocument doc;
  603. doc.Parse( passages );
  604. XMLElement* psg = doc.RootElement()->FirstChildElement();
  605. const char* context = psg->Attribute( "context" );
  606. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  607. XMLTest( "Entity transformation: read. ", expected, context, true );
  608. FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
  609. if ( textfile )
  610. {
  611. XMLPrinter streamer( textfile );
  612. psg->Accept( &streamer );
  613. fclose( textfile );
  614. }
  615. textfile = fopen( "resources/out/textfile.txt", "r" );
  616. TIXMLASSERT( textfile );
  617. if ( textfile )
  618. {
  619. char buf[ 1024 ];
  620. fgets( buf, 1024, textfile );
  621. XMLTest( "Entity transformation: write. ",
  622. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  623. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
  624. buf, false );
  625. fclose( textfile );
  626. }
  627. }
  628. {
  629. // Suppress entities.
  630. const char* passages =
  631. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  632. "<passages count=\"006\" formatversion=\"20020620\">"
  633. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
  634. "</passages>";
  635. XMLDocument doc( false );
  636. doc.Parse( passages );
  637. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
  638. "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
  639. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
  640. "Crazy &ttk;" );
  641. doc.Print();
  642. }
  643. {
  644. const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
  645. XMLDocument doc;
  646. doc.Parse( test );
  647. XMLTest( "dot in names", doc.Error(), false );
  648. XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
  649. XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
  650. }
  651. {
  652. const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
  653. XMLDocument doc;
  654. doc.Parse( test );
  655. XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  656. XMLTest( "Entity with one digit.",
  657. text->Value(), "1.1 Start easy ignore fin thickness\n",
  658. false );
  659. }
  660. {
  661. // DOCTYPE not preserved (950171)
  662. //
  663. const char* doctype =
  664. "<?xml version=\"1.0\" ?>"
  665. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  666. "<!ELEMENT title (#PCDATA)>"
  667. "<!ELEMENT books (title,authors)>"
  668. "<element />";
  669. XMLDocument doc;
  670. doc.Parse( doctype );
  671. doc.SaveFile( "resources/out/test7.xml" );
  672. doc.DeleteChild( doc.RootElement() );
  673. doc.LoadFile( "resources/out/test7.xml" );
  674. doc.Print();
  675. const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
  676. XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
  677. }
  678. {
  679. // Comments do not stream out correctly.
  680. const char* doctype =
  681. "<!-- Somewhat<evil> -->";
  682. XMLDocument doc;
  683. doc.Parse( doctype );
  684. XMLComment* comment = doc.FirstChild()->ToComment();
  685. XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  686. }
  687. {
  688. // Double attributes
  689. const char* doctype = "<element attr='red' attr='blue' />";
  690. XMLDocument doc;
  691. doc.Parse( doctype );
  692. XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
  693. doc.PrintError();
  694. }
  695. {
  696. // Embedded null in stream.
  697. const char* doctype = "<element att\0r='red' attr='blue' />";
  698. XMLDocument doc;
  699. doc.Parse( doctype );
  700. XMLTest( "Embedded null throws error.", true, doc.Error() );
  701. }
  702. {
  703. // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
  704. const char* str = " ";
  705. XMLDocument doc;
  706. doc.Parse( str );
  707. XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
  708. }
  709. {
  710. // Low entities
  711. XMLDocument doc;
  712. doc.Parse( "<test>&#x0e;</test>" );
  713. const char result[] = { 0x0e, 0 };
  714. XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
  715. doc.Print();
  716. }
  717. {
  718. // Attribute values with trailing quotes not handled correctly
  719. XMLDocument doc;
  720. doc.Parse( "<foo attribute=bar\" />" );
  721. XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
  722. }
  723. {
  724. // [ 1663758 ] Failure to report error on bad XML
  725. XMLDocument xml;
  726. xml.Parse("<x>");
  727. XMLTest("Missing end tag at end of input", xml.Error(), true);
  728. xml.Parse("<x> ");
  729. XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
  730. xml.Parse("<x></y>");
  731. XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
  732. }
  733. {
  734. // [ 1475201 ] TinyXML parses entities in comments
  735. XMLDocument xml;
  736. xml.Parse("<!-- declarations for <head> & <body> -->"
  737. "<!-- far &amp; away -->" );
  738. XMLNode* e0 = xml.FirstChild();
  739. XMLNode* e1 = e0->NextSibling();
  740. XMLComment* c0 = e0->ToComment();
  741. XMLComment* c1 = e1->ToComment();
  742. XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  743. XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  744. }
  745. {
  746. XMLDocument xml;
  747. xml.Parse( "<Parent>"
  748. "<child1 att=''/>"
  749. "<!-- With this comment, child2 will not be parsed! -->"
  750. "<child2 att=''/>"
  751. "</Parent>" );
  752. xml.Print();
  753. int count = 0;
  754. for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
  755. ele;
  756. ele = ele->NextSibling() )
  757. {
  758. ++count;
  759. }
  760. XMLTest( "Comments iterate correctly.", 3, count );
  761. }
  762. {
  763. // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
  764. unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
  765. buf[60] = 239;
  766. buf[61] = 0;
  767. XMLDocument doc;
  768. doc.Parse( (const char*)buf);
  769. }
  770. {
  771. // bug 1827248 Error while parsing a little bit malformed file
  772. // Actually not malformed - should work.
  773. XMLDocument xml;
  774. xml.Parse( "<attributelist> </attributelist >" );
  775. XMLTest( "Handle end tag whitespace", false, xml.Error() );
  776. }
  777. {
  778. // This one must not result in an infinite loop
  779. XMLDocument xml;
  780. xml.Parse( "<infinite>loop" );
  781. XMLTest( "Infinite loop test.", true, true );
  782. }
  783. #endif
  784. {
  785. const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
  786. XMLDocument doc;
  787. doc.Parse( pub );
  788. XMLDocument clone;
  789. for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
  790. XMLNode* copy = node->ShallowClone( &clone );
  791. clone.InsertEndChild( copy );
  792. }
  793. clone.Print();
  794. int count=0;
  795. const XMLNode* a=clone.FirstChild();
  796. const XMLNode* b=doc.FirstChild();
  797. for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
  798. ++count;
  799. XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
  800. }
  801. XMLTest( "Clone and Equal", 4, count );
  802. }
  803. {
  804. // This shouldn't crash.
  805. XMLDocument doc;
  806. if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
  807. {
  808. doc.PrintError();
  809. }
  810. XMLTest( "Error in snprinf handling.", true, doc.Error() );
  811. }
  812. {
  813. // Attribute ordering.
  814. static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
  815. XMLDocument doc;
  816. doc.Parse( xml );
  817. XMLElement* ele = doc.FirstChildElement();
  818. const XMLAttribute* a = ele->FirstAttribute();
  819. XMLTest( "Attribute order", "1", a->Value() );
  820. a = a->Next();
  821. XMLTest( "Attribute order", "2", a->Value() );
  822. a = a->Next();
  823. XMLTest( "Attribute order", "3", a->Value() );
  824. XMLTest( "Attribute order", "attrib3", a->Name() );
  825. ele->DeleteAttribute( "attrib2" );
  826. a = ele->FirstAttribute();
  827. XMLTest( "Attribute order", "1", a->Value() );
  828. a = a->Next();
  829. XMLTest( "Attribute order", "3", a->Value() );
  830. ele->DeleteAttribute( "attrib1" );
  831. ele->DeleteAttribute( "attrib3" );
  832. XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
  833. }
  834. {
  835. // Make sure an attribute with a space in it succeeds.
  836. static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
  837. static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
  838. static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
  839. XMLDocument doc0;
  840. doc0.Parse( xml0 );
  841. XMLDocument doc1;
  842. doc1.Parse( xml1 );
  843. XMLDocument doc2;
  844. doc2.Parse( xml2 );
  845. XMLElement* ele = 0;
  846. ele = doc0.FirstChildElement();
  847. XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
  848. ele = doc1.FirstChildElement();
  849. XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
  850. ele = doc2.FirstChildElement();
  851. XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
  852. }
  853. {
  854. // Make sure we don't go into an infinite loop.
  855. static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
  856. XMLDocument doc;
  857. doc.Parse( xml );
  858. XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
  859. XMLElement* ele1 = ele0->NextSiblingElement();
  860. bool equal = ele0->ShallowEqual( ele1 );
  861. XMLTest( "Infinite loop in shallow equal.", true, equal );
  862. }
  863. // -------- Handles ------------
  864. {
  865. static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
  866. XMLDocument doc;
  867. doc.Parse( xml );
  868. XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
  869. XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
  870. XMLHandle docH( doc );
  871. ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
  872. XMLTest( "Handle, dne, mutable", false, ele != 0 );
  873. }
  874. {
  875. static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
  876. XMLDocument doc;
  877. doc.Parse( xml );
  878. XMLConstHandle docH( doc );
  879. const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
  880. XMLTest( "Handle, success, const", ele->Value(), "sub" );
  881. ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
  882. XMLTest( "Handle, dne, const", false, ele != 0 );
  883. }
  884. {
  885. // Default Declaration & BOM
  886. XMLDocument doc;
  887. doc.InsertEndChild( doc.NewDeclaration() );
  888. doc.SetBOM( true );
  889. XMLPrinter printer;
  890. doc.Print( &printer );
  891. static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  892. XMLTest( "BOM and default declaration", printer.CStr(), result, false );
  893. XMLTest( "CStrSize", printer.CStrSize(), 42, false );
  894. }
  895. {
  896. const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
  897. XMLDocument doc;
  898. doc.Parse( xml );
  899. XMLTest( "Ill formed XML", true, doc.Error() );
  900. }
  901. // QueryXYZText
  902. {
  903. const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
  904. XMLDocument doc;
  905. doc.Parse( xml );
  906. const XMLElement* pointElement = doc.RootElement();
  907. int intValue = 0;
  908. unsigned unsignedValue = 0;
  909. float floatValue = 0;
  910. double doubleValue = 0;
  911. bool boolValue = false;
  912. pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
  913. pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
  914. pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
  915. pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
  916. pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
  917. XMLTest( "QueryIntText", intValue, 1, false );
  918. XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
  919. XMLTest( "QueryFloatText", floatValue, 1.2f, false );
  920. XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
  921. XMLTest( "QueryBoolText", boolValue, true, false );
  922. }
  923. {
  924. const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
  925. XMLDocument doc;
  926. doc.Parse( xml );
  927. XMLTest( "Non-alpha element lead letter parses.", doc.Error(), false );
  928. }
  929. {
  930. const char* xml = "<element _attr1=\"foo\" :attr2=\"bar\"></element>";
  931. XMLDocument doc;
  932. doc.Parse( xml );
  933. XMLTest("Non-alpha attribute lead character parses.", doc.Error(), false);
  934. }
  935. {
  936. const char* xml = "<3lement></3lement>";
  937. XMLDocument doc;
  938. doc.Parse( xml );
  939. XMLTest("Element names with lead digit fail to parse.", doc.Error(), true);
  940. }
  941. {
  942. const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
  943. XMLDocument doc;
  944. doc.Parse( xml, 10 );
  945. XMLTest( "Set length of incoming data", doc.Error(), false );
  946. }
  947. {
  948. XMLDocument doc;
  949. doc.LoadFile( "resources/dream.xml" );
  950. doc.Clear();
  951. XMLTest( "Document Clear()'s", doc.NoChildren(), true );
  952. }
  953. // ----------- Whitespace ------------
  954. {
  955. const char* xml = "<element>"
  956. "<a> This \nis &apos; text &apos; </a>"
  957. "<b> This is &apos; text &apos; \n</b>"
  958. "<c>This is &apos; \n\n text &apos;</c>"
  959. "</element>";
  960. XMLDocument doc( true, COLLAPSE_WHITESPACE );
  961. doc.Parse( xml );
  962. const XMLElement* element = doc.FirstChildElement();
  963. for( const XMLElement* parent = element->FirstChildElement();
  964. parent;
  965. parent = parent->NextSiblingElement() )
  966. {
  967. XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
  968. }
  969. }
  970. #if 0
  971. {
  972. // Passes if assert doesn't fire.
  973. XMLDocument xmlDoc;
  974. xmlDoc.NewDeclaration();
  975. xmlDoc.NewComment("Configuration file");
  976. XMLElement *root = xmlDoc.NewElement("settings");
  977. root->SetAttribute("version", 2);
  978. }
  979. #endif
  980. {
  981. const char* xml = "<element> </element>";
  982. XMLDocument doc( true, COLLAPSE_WHITESPACE );
  983. doc.Parse( xml );
  984. XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
  985. }
  986. {
  987. // An assert should not fire.
  988. const char* xml = "<element/>";
  989. XMLDocument doc;
  990. doc.Parse( xml );
  991. XMLElement* ele = doc.NewElement( "unused" ); // This will get cleaned up with the 'doc' going out of scope.
  992. XMLTest( "Tracking unused elements", true, ele != 0, false );
  993. }
  994. {
  995. const char* xml = "<parent><child>abc</child></parent>";
  996. XMLDocument doc;
  997. doc.Parse( xml );
  998. XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
  999. XMLPrinter printer;
  1000. ele->Accept( &printer );
  1001. XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
  1002. }
  1003. {
  1004. XMLDocument doc;
  1005. XMLError error = doc.LoadFile( "resources/empty.xml" );
  1006. XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error );
  1007. }
  1008. {
  1009. // BOM preservation
  1010. static const char* xml_bom_preservation = "\xef\xbb\xbf<element/>\n";
  1011. {
  1012. XMLDocument doc;
  1013. XMLTest( "BOM preservation (parse)", XML_NO_ERROR, doc.Parse( xml_bom_preservation ), false );
  1014. XMLPrinter printer;
  1015. doc.Print( &printer );
  1016. XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
  1017. doc.SaveFile( "resources/bomtest.xml" );
  1018. }
  1019. {
  1020. XMLDocument doc;
  1021. doc.LoadFile( "resources/bomtest.xml" );
  1022. XMLTest( "BOM preservation (load)", true, doc.HasBOM(), false );
  1023. XMLPrinter printer;
  1024. doc.Print( &printer );
  1025. XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
  1026. }
  1027. }
  1028. {
  1029. // Insertion with Removal
  1030. const char* xml = "<?xml version=\"1.0\" ?>"
  1031. "<root>"
  1032. "<one>"
  1033. "<subtree>"
  1034. "<elem>element 1</elem>text<!-- comment -->"
  1035. "</subtree>"
  1036. "</one>"
  1037. "<two/>"
  1038. "</root>";
  1039. const char* xmlInsideTwo = "<?xml version=\"1.0\" ?>"
  1040. "<root>"
  1041. "<one/>"
  1042. "<two>"
  1043. "<subtree>"
  1044. "<elem>element 1</elem>text<!-- comment -->"
  1045. "</subtree>"
  1046. "</two>"
  1047. "</root>";
  1048. const char* xmlAfterOne = "<?xml version=\"1.0\" ?>"
  1049. "<root>"
  1050. "<one/>"
  1051. "<subtree>"
  1052. "<elem>element 1</elem>text<!-- comment -->"
  1053. "</subtree>"
  1054. "<two/>"
  1055. "</root>";
  1056. const char* xmlAfterTwo = "<?xml version=\"1.0\" ?>"
  1057. "<root>"
  1058. "<one/>"
  1059. "<two/>"
  1060. "<subtree>"
  1061. "<elem>element 1</elem>text<!-- comment -->"
  1062. "</subtree>"
  1063. "</root>";
  1064. XMLDocument doc;
  1065. doc.Parse(xml);
  1066. XMLElement* subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
  1067. XMLElement* two = doc.RootElement()->FirstChildElement("two");
  1068. two->InsertFirstChild(subtree);
  1069. XMLPrinter printer1(0, true);
  1070. doc.Accept(&printer1);
  1071. XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
  1072. doc.Parse(xml);
  1073. subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
  1074. two = doc.RootElement()->FirstChildElement("two");
  1075. doc.RootElement()->InsertAfterChild(two, subtree);
  1076. XMLPrinter printer2(0, true);
  1077. doc.Accept(&printer2);
  1078. XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
  1079. doc.Parse(xml);
  1080. XMLNode* one = doc.RootElement()->FirstChildElement("one");
  1081. subtree = one->FirstChildElement("subtree");
  1082. doc.RootElement()->InsertAfterChild(one, subtree);
  1083. XMLPrinter printer3(0, true);
  1084. doc.Accept(&printer3);
  1085. XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
  1086. doc.Parse(xml);
  1087. subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
  1088. two = doc.RootElement()->FirstChildElement("two");
  1089. doc.RootElement()->InsertEndChild(subtree);
  1090. XMLPrinter printer4(0, true);
  1091. doc.Accept(&printer4);
  1092. XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
  1093. }
  1094. {
  1095. const char* xml = "<svg width = \"128\" height = \"128\">"
  1096. " <text> </text>"
  1097. "</svg>";
  1098. XMLDocument doc;
  1099. doc.Parse(xml);
  1100. doc.Print();
  1101. }
  1102. #if 1
  1103. // the question being explored is what kind of print to use:
  1104. // https://github.com/leethomason/tinyxml2/issues/63
  1105. {
  1106. //const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9' attrC='1.0e-10' attrD='1001000000.000000' attrE='0.1234567890123456789'/>";
  1107. const char* xml = "<element/>";
  1108. XMLDocument doc;
  1109. doc.Parse( xml );
  1110. doc.FirstChildElement()->SetAttribute( "attrA-f64", 123456789.123456789 );
  1111. doc.FirstChildElement()->SetAttribute( "attrB-f64", 1.001e9 );
  1112. doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e9 );
  1113. doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e20 );
  1114. doc.FirstChildElement()->SetAttribute( "attrD-f64", 1.0e-10 );
  1115. doc.FirstChildElement()->SetAttribute( "attrD-f64", 0.123456789 );
  1116. doc.FirstChildElement()->SetAttribute( "attrA-f32", 123456789.123456789f );
  1117. doc.FirstChildElement()->SetAttribute( "attrB-f32", 1.001e9f );
  1118. doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e9f );
  1119. doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e20f );
  1120. doc.FirstChildElement()->SetAttribute( "attrD-f32", 1.0e-10f );
  1121. doc.FirstChildElement()->SetAttribute( "attrD-f32", 0.123456789f );
  1122. doc.Print();
  1123. /* The result of this test is platform, compiler, and library version dependent. :("
  1124. XMLPrinter printer;
  1125. doc.Print( &printer );
  1126. XMLTest( "Float and double formatting.",
  1127. "<element attrA-f64=\"123456789.12345679\" attrB-f64=\"1001000000\" attrC-f64=\"1e+20\" attrD-f64=\"0.123456789\" attrA-f32=\"1.2345679e+08\" attrB-f32=\"1.001e+09\" attrC-f32=\"1e+20\" attrD-f32=\"0.12345679\"/>\n",
  1128. printer.CStr(),
  1129. true );
  1130. */
  1131. }
  1132. #endif
  1133. // ----------- Performance tracking --------------
  1134. {
  1135. #if defined( _MSC_VER )
  1136. __int64 start, end, freq;
  1137. QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
  1138. #endif
  1139. FILE* fp = fopen( "resources/dream.xml", "r" );
  1140. fseek( fp, 0, SEEK_END );
  1141. long size = ftell( fp );
  1142. fseek( fp, 0, SEEK_SET );
  1143. char* mem = new char[size+1];
  1144. fread( mem, size, 1, fp );
  1145. fclose( fp );
  1146. mem[size] = 0;
  1147. #if defined( _MSC_VER )
  1148. QueryPerformanceCounter( (LARGE_INTEGER*) &start );
  1149. #else
  1150. clock_t cstart = clock();
  1151. #endif
  1152. static const int COUNT = 10;
  1153. for( int i=0; i<COUNT; ++i ) {
  1154. XMLDocument doc;
  1155. doc.Parse( mem );
  1156. }
  1157. #if defined( _MSC_VER )
  1158. QueryPerformanceCounter( (LARGE_INTEGER*) &end );
  1159. #else
  1160. clock_t cend = clock();
  1161. #endif
  1162. delete [] mem;
  1163. static const char* note =
  1164. #ifdef DEBUG
  1165. "DEBUG";
  1166. #else
  1167. "Release";
  1168. #endif
  1169. #if defined( _MSC_VER )
  1170. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
  1171. #else
  1172. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
  1173. #endif
  1174. }
  1175. #if defined( _MSC_VER ) && defined( DEBUG )
  1176. _CrtMemCheckpoint( &endMemState );
  1177. //_CrtMemDumpStatistics( &endMemState );
  1178. _CrtMemState diffMemState;
  1179. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  1180. _CrtMemDumpStatistics( &diffMemState );
  1181. //printf( "new total=%d\n", gNewTotal );
  1182. #endif
  1183. printf ("\nPass %d, Fail %d\n", gPass, gFail);
  1184. return gFail;
  1185. }