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.

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