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.

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