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.0 KiB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Usage
  9. -----
  10. This example is also [included in the repository](https://github.com/ooxi/xml.c/blob/master/test/example.c)
  11. and will be build by default. Most of the code is C boilerplate, the important
  12. functions are `xml_parse_document`, `xml_document_root`, `xml_node_name`,
  13. `xml_node_content` and `xml_node_child` / `xml_node_children`.
  14. ```c
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <xml.h>
  19. int main(int argc, char** argv) {
  20. /* XML source, could be read from disk
  21. */
  22. uint8_t* source = ""
  23. "<Root>"
  24. "<Hello>World</Hello>"
  25. "<This>"
  26. "<Is>:-)</Is>"
  27. "<An>:-O</An>"
  28. "<Example>:-D</Example>"
  29. "</This>"
  30. "</Root>"
  31. ;
  32. /* Parse the document
  33. *
  34. * Watch out: Remember not to free the source until you have freed the
  35. * document itself. If you have to free the source before, supply a
  36. * copy to xml_parse_document which can be freed together with the
  37. * document (`free_buffer' argument to `xml_document_free')
  38. */
  39. struct xml_document* document = xml_parse_document(source, strlen(source));
  40. /* You _have_ to check the result of `xml_parse_document', if it's 0
  41. * then the source could not be parsed. If you think this is a bug in
  42. * xml.c, than use a debug build (cmake -DCMAKE_BUILD_TYPE=Debug) which
  43. * will verbosely tell you about the parsing process
  44. */
  45. if (!document) {
  46. printf("Could parse document\n");
  47. exit(EXIT_FAILURE);
  48. }
  49. struct xml_node* root = xml_document_root(document);
  50. /* Say Hello World :-)
  51. */
  52. struct xml_node* root_hello = xml_node_child(root, 0);
  53. struct xml_string* hello = xml_node_name(root_hello);
  54. struct xml_string* world = xml_node_content(root_hello);
  55. /* Watch out: `xml_string_copy' will not 0-terminate your buffers! (but
  56. * `calloc' will :-)
  57. */
  58. uint8_t* hello_0 = calloc(xml_string_length(hello) + 1, sizeof(uint8_t));
  59. uint8_t* world_0 = calloc(xml_string_length(world) + 1, sizeof(uint8_t));
  60. xml_string_copy(hello, hello_0, xml_string_length(hello));
  61. xml_string_copy(world, world_0, xml_string_length(world));
  62. printf("%s %s\n", hello_0, world_0);
  63. free(hello_0);
  64. free(world_0);
  65. /* Extract amount of Root/This children
  66. */
  67. struct xml_node* root_this = xml_node_child(root, 1);
  68. printf("Root/This has %lu children\n", (unsigned long)xml_node_children(root_this));
  69. /* Remember to free the document or you'll risk a memory leak
  70. */
  71. xml_document_free(document, false);
  72. }
  73. ```
  74. Another usage example can be found in the [unit case](https://github.com/ooxi/xml.c/blob/master/test/test-xml.c).
  75. License
  76. -------
  77. [libpng/zlib](https://github.com/ooxi/xml.c/blob/master/LICENSE) (BSD)