logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://anongit.hacktivis.me/git/libbulletml.git/

xmltest.cpp (5983B)


  1. #include "tinyxml.h"
  2. //
  3. // This file demonstrates some basic functionality of TinyXml.
  4. // Note that the example is very contrived. It presumes you know
  5. // what is in the XML file. But it does test the basic operations,
  6. // and show how to add and remove nodes.
  7. //
  8. int main()
  9. {
  10. //
  11. // We start with the 'demoStart' todo list. Process it. And
  12. // should hopefully end up with the todo list as illustrated.
  13. //
  14. const char* demoStart =
  15. "<?xml version=\"1.0\" standalone='no' >\n"
  16. "<!-- Our to do list \n data -->"
  17. "<ToDo>\n"
  18. "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>"
  19. "<Item priority=\"2\" distance='none'><![CDATA[ Do bills ]]></Item>"
  20. "<Item priority=\"2\" distance='far'> Look for Evil Dinosaurs! </Item>"
  21. "</ToDo>";
  22. /* What the todo list should look like after processing.
  23. <?xml version="1.0" standalone="no" ?>
  24. <!-- Our to do list data -->
  25. <ToDo>
  26. <Item priority="2" distance="close"> Go to the
  27. <bold>Toy store!
  28. </bold>
  29. </Item>
  30. <Item priority="1" distance="far"> Talk to:
  31. <Meeting where="School">
  32. <Attendee name="Marple" position="teacher" />
  33. <Attendee name="Voo" position="counselor" />
  34. </Meeting>
  35. <Meeting where="Lunch" />
  36. </Item>
  37. <Item priority="2" distance="here"> Do bills
  38. </Item>
  39. </ToDo>
  40. */
  41. // The example parses from the character string (above):
  42. {
  43. // Write to a file and read it back, to check file I/O.
  44. TiXmlDocument doc( "demotest.xml" );
  45. doc.Parse( demoStart );
  46. if ( doc.Error() )
  47. {
  48. printf( "Error in %s: %s\n", doc.Value().c_str(), doc.ErrorDesc().c_str() );
  49. exit( 1 );
  50. }
  51. doc.SaveFile();
  52. }
  53. TiXmlDocument doc( "demotest.xml" );
  54. doc.LoadFile();
  55. printf( "** Demo doc read from disk: ** \n\n" );
  56. doc.Print( stdout );
  57. TiXmlNode* node = 0;
  58. TiXmlElement* todoElement = 0;
  59. TiXmlElement* itemElement = 0;
  60. // --------------------------------------------------------
  61. // An example of changing existing attributes, and removing
  62. // an element from the document.
  63. // --------------------------------------------------------
  64. // Get the "ToDo" element.
  65. // It is a child of the document, and can be selected by name.
  66. node = doc.FirstChild( "ToDo" );
  67. assert( node );
  68. todoElement = node->ToElement();
  69. assert( todoElement );
  70. // Going to the toy store is now our second priority...
  71. // So set the "priority" attribute of the first item in the list.
  72. node = todoElement->FirstChild();
  73. assert( node );
  74. itemElement = node->ToElement();
  75. assert( itemElement );
  76. itemElement->SetAttribute( "priority", 2 );
  77. // Change the distance to "doing bills" from
  78. // "none" to "here". It's the next sibling element.
  79. itemElement = itemElement->NextSiblingElement();
  80. itemElement->SetAttribute( "distance", "here" );
  81. // Remove the "Look for Evil Dinosours!" item.
  82. // It is 1 more sibling away. We ask the parent to remove
  83. // a particular child.
  84. itemElement = itemElement->NextSiblingElement();
  85. todoElement->RemoveChild( itemElement );
  86. itemElement = 0;
  87. // --------------------------------------------------------
  88. // What follows is an example of created elements and text
  89. // nodes and adding them to the document.
  90. // --------------------------------------------------------
  91. // Add some meetings.
  92. TiXmlElement item( "Item" );
  93. item.SetAttribute( "priority", "1" );
  94. item.SetAttribute( "distance", "far" );
  95. TiXmlText text;
  96. text.SetValue( "Talk to:" );
  97. TiXmlElement meeting1( "Meeting" );
  98. meeting1.SetAttribute( "where", "School" );
  99. TiXmlElement meeting2( "Meeting" );
  100. meeting2.SetAttribute( "where", "Lunch" );
  101. TiXmlElement attendee1( "Attendee" );
  102. attendee1.SetAttribute( "name", "Marple" );
  103. attendee1.SetAttribute( "position", "teacher" );
  104. TiXmlElement attendee2( "Attendee" );
  105. attendee2.SetAttribute( "name", "Voo" );
  106. attendee2.SetAttribute( "position", "counselor" );
  107. // Assemble the nodes we've created:
  108. meeting1.InsertEndChild( attendee1 );
  109. meeting1.InsertEndChild( attendee2 );
  110. item.InsertEndChild( text );
  111. item.InsertEndChild( meeting1 );
  112. item.InsertEndChild( meeting2 );
  113. // And add the node to the existing list after the first child.
  114. node = todoElement->FirstChild( "Item" );
  115. assert( node );
  116. itemElement = node->ToElement();
  117. assert( itemElement );
  118. todoElement->InsertAfterChild( itemElement, item );
  119. printf( "\n** Demo doc processed: ** \n\n" );
  120. doc.Print( stdout );
  121. // --------------------------------------------------------
  122. // Different ways to walk the XML document.
  123. // --------------------------------------------------------
  124. int count = 0;
  125. TiXmlElement* element;
  126. // Walk all the top level nodes of the document.
  127. count = 0;
  128. for( node = doc.FirstChild();
  129. node;
  130. node = node->NextSibling() )
  131. {
  132. count++;
  133. }
  134. printf( "The document contains %d top level nodes. (3)\n", count );
  135. // Walk all the top level nodes of the document,
  136. // using a different sytax.
  137. count = 0;
  138. for( node = doc.IterateChildren( 0 );
  139. node;
  140. node = doc.IterateChildren( node ) )
  141. {
  142. count++;
  143. }
  144. printf( "The document contains %d top level nodes. (3)\n", count );
  145. // Walk all the elements in a node.
  146. count = 0;
  147. for( element = todoElement->FirstChildElement();
  148. element;
  149. element = element->NextSiblingElement() )
  150. {
  151. count++;
  152. }
  153. printf( "The 'ToDo' element contains %d elements. (3)\n", count );
  154. // Walk all the elements in a node by value.
  155. count = 0;
  156. for( node = todoElement->FirstChild( "Item" );
  157. node;
  158. node = node->NextSibling( "Item" ) )
  159. {
  160. count++;
  161. }
  162. printf( "The 'ToDo' element contains %d nodes with the value of 'Item'. (3)\n", count );
  163. /*
  164. for( int i=0; i<1000; i++ )
  165. doc.LoadFile( "SmallRuleset1.xml" );
  166. doc.SaveFile( "smalltest.xml" );
  167. */
  168. return 0;
  169. }