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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.md 2.9 KiB

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