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
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 3.7 KiB

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