Simple XML subset parser comparable to glib's Markup parser, but without any dependencies in one self contained file. Forked from https://github.com/ooxi/xml.c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.4 KiB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. xml.c
  2. =====
  3. Similar to the [GLib Markup parser](http://developer.gnome.org/glib/2.34/glib-Simple-XML-Subset-Parser.html),
  4. which also just parses an xml subset, [xml.c](https://github.com/ooxi/xml.c) is
  5. a simple, small and self contained xml parser in one file. Ideal for embedding
  6. into other projects without the need for big external dependencies.
  7. [![Build Status](https://secure.travis-ci.org/ooxi/xml.c.png)](http://travis-ci.org/ooxi/xml.c)
  8. Downloads
  9. ---------
  10. All releases are based on master, so the preferred way of using xml.c is adding
  11. the repository as [git submodule](http://git-scm.com/book/en/Git-Tools-Submodules).
  12. If you prefer formal releases, check out the [release tags](https://github.com/ooxi/xml.c/tags).
  13. [Prebuild binaries](https://github.com/ooxi/xml.c/downloads) are also available.
  14. Usage
  15. -----
  16. This example is also [included in the repository](https://github.com/ooxi/xml.c/blob/master/test/example.c)
  17. and will be build by default. Most of the code is C boilerplate, the important
  18. functions are `xml_parse_document`, `xml_document_root`, `xml_node_name`,
  19. `xml_node_content` and `xml_node_child` / `xml_node_children`.
  20. ```c
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <xml.h>
  25. int main(int argc, char** argv) {
  26. /* XML source, could be read from disk
  27. */
  28. uint8_t* source = ""
  29. "<Root>"
  30. "<Hello>World</Hello>"
  31. "<This>"
  32. "<Is>:-)</Is>"
  33. "<An>:-O</An>"
  34. "<Example>:-D</Example>"
  35. "</This>"
  36. "</Root>"
  37. ;
  38. /* Parse the document
  39. *
  40. * Watch out: Remember not to free the source until you have freed the
  41. * document itself. If you have to free the source before, supply a
  42. * copy to xml_parse_document which can be freed together with the
  43. * document (`free_buffer' argument to `xml_document_free')
  44. */
  45. struct xml_document* document = xml_parse_document(source, strlen(source));
  46. /* You _have_ to check the result of `xml_parse_document', if it's 0
  47. * then the source could not be parsed. If you think this is a bug in
  48. * xml.c, than use a debug build (cmake -DCMAKE_BUILD_TYPE=Debug) which
  49. * will verbosely tell you about the parsing process
  50. */
  51. if (!document) {
  52. printf("Could parse document\n");
  53. exit(EXIT_FAILURE);
  54. }
  55. struct xml_node* root = xml_document_root(document);
  56. /* Say Hello World :-)
  57. */
  58. struct xml_node* root_hello = xml_node_child(root, 0);
  59. struct xml_string* hello = xml_node_name(root_hello);
  60. struct xml_string* world = xml_node_content(root_hello);
  61. /* Watch out: `xml_string_copy' will not 0-terminate your buffers! (but
  62. * `calloc' will :-)
  63. */
  64. uint8_t* hello_0 = calloc(xml_string_length(hello) + 1, sizeof(uint8_t));
  65. uint8_t* world_0 = calloc(xml_string_length(world) + 1, sizeof(uint8_t));
  66. xml_string_copy(hello, hello_0, xml_string_length(hello));
  67. xml_string_copy(world, world_0, xml_string_length(world));
  68. printf("%s %s\n", hello_0, world_0);
  69. free(hello_0);
  70. free(world_0);
  71. /* Extract amount of Root/This children
  72. */
  73. struct xml_node* root_this = xml_node_child(root, 1);
  74. printf("Root/This has %lu children\n", (unsigned long)xml_node_children(root_this));
  75. /* Remember to free the document or you'll risk a memory leak
  76. */
  77. xml_document_free(document, false);
  78. }
  79. ```
  80. Another usage example can be found in the [unit case](https://github.com/ooxi/xml.c/blob/master/test/test-xml.c).
  81. License
  82. -------
  83. [libpng/zlib](https://github.com/ooxi/xml.c/blob/master/LICENSE) (BSD)