docset.intro 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <h2 class='title'><a name='INTRODUCTION'>Introduction</a></h2>
  2. <p>Mini-XML is a small XML parsing library that you can use to read XML and
  3. XML-like data files in your application without requiring large non-standard
  4. libraries. Mini-XML only requires an ANSI C compatible compiler (GCC works,
  5. as do most vendors' ANSI C compilers) and a "make" program.</p>
  6. <p>Mini-XML provides the following functionality:</p>
  7. <ul>
  8. <li>Reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files
  9. and strings.</li>
  10. <li>Data is stored in a linked-list tree structure, preserving the XML
  11. data hierarchy.</li>
  12. <li>Supports arbitrary element names, attributes, and attribute values
  13. with no preset limits, just available memory.</li>
  14. <li>Supports integer, real, opaque ("CDATA"), and text data types in
  15. "leaf" nodes.</li>
  16. <li>Functions for creating, indexing, and managing trees of data.</li>
  17. <li>"Find" and "walk" functions for easily locating and navigating trees
  18. of data.</li>
  19. </ul>
  20. <p>Mini-XML doesn't do validation or other types of processing on the data based
  21. upon schema files or other sources of definition information, nor does it
  22. support character entities other than those required by the XML
  23. specification.</p>
  24. <h2 class='title'><a name='USING'>Using Mini-XML</a></h2>
  25. <p>Mini-XML provides a single header file which you include:</p>
  26. <pre class='example'>
  27. #include &lt;mxml.h&gt;
  28. </pre>
  29. <p>Nodes are defined by the "<a href='#mxml_node_s'>mxml_node_t</a>" structure;
  30. the "type" member defines the node type (element, integer, opaque, real, or
  31. text) which determines which value you want to look at in the "value" union.
  32. New nodes can be created using the
  33. "<a href='#mxmlNewElement'>mxmlNewElement()</a>",
  34. "<a href='#mxmlNewInteger'>mxmlNewInteger()</a>",
  35. "<a href='#mxmlNewOpaque'>mxmlNewOpaque()</a>",
  36. "<a href='#mxmlNewReal'>mxmlNewReal()</a>", and
  37. "<a href='#mxmlNewText'>mxmlNewText()</a>" functions. Only elements can have
  38. child nodes, and the top node must be an element, usually "?xml".</p>
  39. <p>You load an XML file using the "mxmlLoadFile()" function:</p>
  40. <pre class='example'>
  41. FILE *fp;
  42. mxml_node_t *tree;
  43. fp = fopen("filename.xml", "r");
  44. tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
  45. fclose(fp);
  46. </pre>
  47. <p>Similarly, you save an XML file using the
  48. "<a href='#mxmlSaveFile'>mxmlSaveFile()</a>" function:
  49. <pre class='example'>
  50. FILE *fp;
  51. mxml_node_t *tree;
  52. fp = fopen("filename.xml", "w");
  53. mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
  54. fclose(fp);
  55. </pre>
  56. <p>The "<a href='#mxmlLoadString'>mxmlLoadString()</a>",
  57. "<a href='#mxmlSaveAllocString'>mxmlSaveAllocString()</a>", and
  58. "<a href='#mxmlSaveString'>mxmlSaveString()</a>" functions load XML node trees
  59. from and save XML node trees to strings:</p>
  60. <pre class='example'>
  61. char buffer[8192];
  62. char *ptr;
  63. mxml_node_t *tree;
  64. ...
  65. tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
  66. ...
  67. mxmlSaveString(tree, buffer, sizeof(buffer),
  68. MXML_NO_CALLBACK);
  69. ...
  70. ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
  71. </pre>
  72. <p>You can find a named element/node using the
  73. "<a href='#mxmlFindElement'>mxmlFindElement()</a>" function:</p>
  74. <pre class='example'>
  75. mxml_node_t *node = mxmlFindElement(tree, tree, "name",
  76. "attr", "value",
  77. MXML_DESCEND);
  78. </pre>
  79. <p>The "name", "attr", and "value" arguments can be passed as
  80. <code>NULL</code> to act as wildcards, e.g.:</p>
  81. <pre class='example'>
  82. /* Find the first "a" element */
  83. node = mxmlFindElement(tree, tree, "a", NULL, NULL,
  84. MXML_DESCEND);
  85. /* Find the first "a" element with "href" attribute */
  86. node = mxmlFindElement(tree, tree, "a", "href", NULL,
  87. MXML_DESCEND);
  88. /* Find the first "a" element with "href" to a URL */
  89. node = mxmlFindElement(tree, tree, "a", "href",
  90. "http://www.easysw.com/~mike/mxml/",
  91. MXML_DESCEND);
  92. /* Find the first element with a "src" attribute*/
  93. node = mxmlFindElement(tree, tree, NULL, "src", NULL,
  94. MXML_DESCEND);
  95. /* Find the first element with a "src" = "foo.jpg" */
  96. node = mxmlFindElement(tree, tree, NULL, "src",
  97. "foo.jpg", MXML_DESCEND);
  98. </pre>
  99. <p>You can also iterate with the same function:</p>
  100. <pre class='example'>
  101. mxml_node_t *node;
  102. for (node = mxmlFindElement(tree, tree, "name", NULL,
  103. NULL, MXML_DESCEND);
  104. node != NULL;
  105. node = mxmlFindElement(node, tree, "name", NULL,
  106. NULL, MXML_DESCEND))
  107. {
  108. ... do something ...
  109. }
  110. </pre>
  111. <p>The "mxmlFindPath()" function finds the (first) value node under a specific
  112. element using a "path":</p>
  113. <pre class='example'>
  114. mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar");
  115. </pre>
  116. <p>The "mxmlGetInteger()", "mxmlGetOpaque()", "mxmlGetReal()", and
  117. "mxmlGetText()" functions retrieve the value from a node:</p>
  118. <pre class='example'>
  119. mxml_node_t *node;
  120. int intvalue = mxmlGetInteger(node);
  121. const char *opaquevalue = mxmlGetOpaque(node);
  122. double realvalue = mxmlGetReal(node);
  123. int whitespacevalue;
  124. const char *textvalue = mxmlGetText(node, &amp;whitespacevalue);
  125. </pre>
  126. <p>Finally, once you are done with the XML data, use the
  127. "<a href='#mxmlDelete'>mxmlDelete()</a>" function to recursively free the
  128. memory that is used for a particular node or the entire tree:</p>
  129. <pre class='example'>
  130. mxmlDelete(tree);
  131. </pre>