xmltest.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Purely doxygen documentation
  2. // What follows is the docs for the examples.
  3. // I'd like the docs to be just before the
  4. // actual examples in xmltest.cpp, but I
  5. // can't seem to get doxygen to do that. It
  6. // would be a wonderful patch if anyone figures
  7. // it out.
  8. /** @page Example-1 Load an XML File
  9. * @dontinclude ./xmltest.cpp
  10. * Basic XML file loading.
  11. * The basic syntax to load an XML file from
  12. * disk and check for an error. (ErrorID()
  13. * will return 0 for no error.)
  14. * @skip example_1()
  15. * @until }
  16. */
  17. /** @page Example-2 Parse an XML from char buffer
  18. * @dontinclude ./xmltest.cpp
  19. * Basic XML string parsing.
  20. * The basic syntax to parse an XML for
  21. * a char* and check for an error. (ErrorID()
  22. * will return 0 for no error.)
  23. * @skip example_2()
  24. * @until }
  25. */
  26. /** @page Example-3 Get information out of XML
  27. @dontinclude ./xmltest.cpp
  28. In this example, we navigate a simple XML
  29. file, and read some interesting text. Note
  30. that this is examlpe doesn't use error
  31. checking; working code should check for null
  32. pointers when walking an XML tree, or use
  33. XMLHandle.
  34. (The XML is an excerpt from "dream.xml").
  35. @skip example_3
  36. @until </PLAY>";
  37. The structure of the XML file is:
  38. <ul>
  39. <li>(declaration)</li>
  40. <li>(dtd stuff)</li>
  41. <li>Element "PLAY"</li>
  42. <ul>
  43. <li>Element "TITLE"</li>
  44. <ul>
  45. <li>Text "A Midsummer Night's Dream"</li>
  46. </ul>
  47. </ul>
  48. </ul>
  49. For this example, we want to print out the
  50. title of the play. The text of the title (what
  51. we want) is child of the "TITLE" element which
  52. is a child of the "PLAY" element.
  53. We want to skip the declaration and dtd, so the
  54. method FirstChildElement() is a good choice. The
  55. FirstChildElement() of the Document is the "PLAY"
  56. Element, the FirstChildElement() of the "PLAY" Element
  57. is the "TITLE" Element.
  58. @until ( "TITLE" );
  59. We can then use the convenience function GetText()
  60. to get the title of the play.
  61. @until title );
  62. Text is just another Node in the XML DOM. And in
  63. fact you should be a little cautious with it, as
  64. text nodes can contain elements.
  65. @verbatim
  66. Consider: A Midsummer Night's <b>Dream</b>
  67. @endverbatim
  68. It is more correct to actually query the Text Node
  69. if in doubt:
  70. @until title );
  71. Noting that here we use FirstChild() since we are
  72. looking for XMLText, not an element, and ToText()
  73. is a cast from a Node to a XMLText.
  74. */
  75. /** @page Example-4 Read attributes and text information.
  76. @dontinclude ./xmltest.cpp
  77. There are fundamentally 2 ways of writing a key-value
  78. pair into an XML file. (Something that's always annoyed
  79. me about XML.) Either by using attributes, or by writing
  80. the key name into an element and the value into
  81. the text node wrapped by the element. Both approaches
  82. are illustrated in this example, which shows two ways
  83. to encode the value "2" into the key "v":
  84. @skip example_4
  85. @until "</information>";
  86. TinyXML-2 has accessors for both approaches.
  87. When using an attribute, you navigate to the XMLElement
  88. with that attribute and use the QueryIntAttribute()
  89. group of methods. (Also QueryFloatAttribute(), etc.)
  90. @skip XMLElement* attributeApproachElement
  91. @until &v0 );
  92. When using the text approach, you need to navigate
  93. down one more step to the XMLElement that contains
  94. the text. Note the extra FirstChildElement( "v" )
  95. in the code below. The value of the text can then
  96. be safely queried with the QueryIntText() group
  97. of methods. (Also QueryFloatText(), etc.)
  98. @skip XMLElement* textApproachElement
  99. @until &v1 );
  100. */