README 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. README - 2016-06-12
  2. -------------------
  3. INTRODUCTION
  4. This README file describes the Mini-XML library version 2.10.
  5. Mini-XML is a small XML parsing library that you can use to read XML and
  6. XML-like data files in your application without requiring large non-standard
  7. libraries. Mini-XML only requires an ANSI C compatible compiler (GCC works,
  8. as do most vendors' ANSI C compilers) and a "make" program.
  9. Mini-XML provides the following functionality:
  10. - Reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and
  11. strings.
  12. - Data is stored in a linked-list tree structure, preserving the XML
  13. data hierarchy.
  14. - Supports arbitrary element names, attributes, and attribute values
  15. with no preset limits, just available memory.
  16. - Supports integer, real, opaque ("cdata"), and text data types in
  17. "leaf" nodes.
  18. - Functions for creating and managing trees of data.
  19. - "Find" and "walk" functions for easily locating and navigating trees
  20. of data.
  21. Mini-XML doesn't do validation or other types of processing on the data
  22. based upon schema files or other sources of definition information.
  23. BUILDING Mini-XML
  24. Mini-XML comes with an autoconf-based configure script; just type the
  25. following command to get things going:
  26. ./configure
  27. The default install prefix is /usr/local, which can be overridden using the
  28. --prefix option:
  29. ./configure --prefix=/foo
  30. Other configure options can be found using the --help option:
  31. ./configure --help
  32. Once you have configured the software, type "make" to do the build and run
  33. the test program to verify that things are working, as follows:
  34. make
  35. If you are using Mini-XML under Microsoft Windows with Visual C++, use the
  36. included project files in the "vcnet" subdirectory to build the library
  37. instead. Note: The static library on Windows is NOT thread-safe.
  38. INSTALLING Mini-XML
  39. The "install" target will install Mini-XML in the lib and include
  40. directories:
  41. make install
  42. Once you have installed it, use the "-lmxml" option to link your application
  43. against it.
  44. DOCUMENTATION
  45. The documentation is available in the "doc" subdirectory in the files
  46. "mxml.html" (HTML) and "mxml.pdf" (PDF). You can also look at the
  47. "testmxml.c" and "mxmldoc.c" source files for examples of using Mini-XML.
  48. Mini-XML provides a single header file which you include:
  49. #include <mxml.h>
  50. Nodes are defined by the "mxml_node_t" structure; the "type" member defines
  51. the node type (element, integer, opaque, real, or text) which determines
  52. which value you want to look at in the "value" union. New nodes can be
  53. created using the "mxmlNewElement()", "mxmlNewInteger()", "mxmlNewOpaque()",
  54. "mxmlNewReal()", and "mxmlNewText()" functions. Only elements can have
  55. child nodes, and the top node must be an element, usually "?xml".
  56. You load an XML file using the "mxmlLoadFile()" function:
  57. FILE *fp;
  58. mxml_node_t *tree;
  59. fp = fopen("filename.xml", "r");
  60. tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
  61. fclose(fp);
  62. Similarly, you save an XML file using the "mxmlSaveFile()" function:
  63. FILE *fp;
  64. mxml_node_t *tree;
  65. fp = fopen("filename.xml", "w");
  66. mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
  67. fclose(fp);
  68. The "mxmlLoadString()", "mxmlSaveAllocString()", and "mxmlSaveString()"
  69. functions load XML node trees from and save XML node trees to strings:
  70. char buffer[8192];
  71. char *ptr;
  72. mxml_node_t *tree;
  73. ...
  74. tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
  75. ...
  76. mxmlSaveString(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK);
  77. ...
  78. ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
  79. You can find a named element/node using the "mxmlFindElement()" function:
  80. mxml_node_t *node = mxmlFindElement(tree, tree, "name", "attr",
  81. "value", MXML_DESCEND);
  82. The "name", "attr", and "value" arguments can be passed as NULL to act as
  83. wildcards, e.g.:
  84. /* Find the first "a" element */
  85. node = mxmlFindElement(tree, tree, "a", NULL, NULL, MXML_DESCEND);
  86. /* Find the first "a" element with "href" attribute */
  87. node = mxmlFindElement(tree, tree, "a", "href", NULL, MXML_DESCEND);
  88. /* Find the first "a" element with "href" to a URL */
  89. node = mxmlFindElement(tree, tree, "a", "href",
  90. "http://www.minixml.org/",
  91. MXML_DESCEND);
  92. /* Find the first element with a "src" attribute*/
  93. node = mxmlFindElement(tree, tree, NULL, "src", NULL, MXML_DESCEND);
  94. /* Find the first element with a "src" = "foo.jpg" */
  95. node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg",
  96. MXML_DESCEND);
  97. You can also iterate with the same function:
  98. mxml_node_t *node;
  99. for (node = mxmlFindElement(tree, tree, "name", NULL, NULL,
  100. MXML_DESCEND);
  101. node != NULL;
  102. node = mxmlFindElement(node, tree, "name", NULL, NULL,
  103. MXML_DESCEND))
  104. {
  105. ... do something ...
  106. }
  107. The "mxmlFindPath()" function finds the (first) value node under a specific
  108. element using a "path":
  109. mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar");
  110. The "mxmlGetInteger()", "mxmlGetOpaque()", "mxmlGetReal()", and
  111. "mxmlGetText()" functions retrieve the value from a node:
  112. mxml_node_t *node;
  113. int intvalue = mxmlGetInteger(node);
  114. const char *opaquevalue = mxmlGetOpaque(node);
  115. double realvalue = mxmlGetReal(node);
  116. int whitespacevalue;
  117. const char *textvalue = mxmlGetText(node, &whitespacevalue);
  118. Finally, once you are done with the XML data, use the "mxmlDelete()"
  119. function to recursively free the memory that is used for a particular node
  120. or the entire tree:
  121. mxmlDelete(tree);
  122. GETTING HELP AND REPORTING PROBLEMS
  123. The Mini-XML project page provides access to a discussion forum and bug
  124. reporting page:
  125. http://www.msweet.org/projects.php/Mini-XML
  126. LEGAL STUFF
  127. The Mini-XML library is Copyright 2003-2016 by Michael R Sweet. License
  128. terms are described in the file "COPYING".