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.

97 lines
3.0 KiB

  1. /**
  2. * Copyright (c) 2012 ooxi/xml.c
  3. * https://github.com/ooxi/xml.c
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from the
  7. * use of this software.
  8. *
  9. * Permission is granted to anyone to use this software for any purpose,
  10. * including commercial applications, and to alter it and redistribute it
  11. * freely, subject to the following restrictions:
  12. *
  13. * 1. The origin of this software must not be misrepresented; you must not
  14. * claim that you wrote the original software. If you use this software in a
  15. * product, an acknowledgment in the product documentation would be
  16. * appreciated but is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <xml.h>
  27. int main(int argc, char** argv) {
  28. /* XML source, could be read from disk
  29. */
  30. uint8_t* source = ""
  31. "<Root>"
  32. "<Hello>World</Hello>"
  33. "<This>"
  34. "<Is>:-)</Is>"
  35. "<An>:-O</An>"
  36. "<Example>:-D</Example>"
  37. "</This>"
  38. "</Root>"
  39. ;
  40. /* Parse the document
  41. *
  42. * Watch out: Remember not to free the source until you have freed the
  43. * document itself. If you have to free the source before, supply a
  44. * copy to xml_parse_document which can be freed together with the
  45. * document (`free_buffer' argument to `xml_document_free')
  46. */
  47. struct xml_document* document = xml_parse_document(source, strlen(source));
  48. /* You _have_ to check the result of `xml_parse_document', if it's 0
  49. * then the source could not be parsed. If you think this is a bug in
  50. * xml.c, than use a debug build (cmake -DCMAKE_BUILD_TYPE=Debug) which
  51. * will verbosely tell you about the parsing process
  52. */
  53. if (!document) {
  54. printf("Could parse document\n");
  55. exit(EXIT_FAILURE);
  56. }
  57. struct xml_node* root = xml_document_root(document);
  58. /* Say Hello World :-)
  59. */
  60. struct xml_node* root_hello = xml_node_child(root, 0);
  61. struct xml_string* hello = xml_node_name(root_hello);
  62. struct xml_string* world = xml_node_content(root_hello);
  63. /* Watch out: `xml_string_copy' will not 0-terminate your buffers! (but
  64. * `calloc' will :-)
  65. */
  66. uint8_t* hello_0 = calloc(xml_string_length(hello) + 1, sizeof(uint8_t));
  67. uint8_t* world_0 = calloc(xml_string_length(world) + 1, sizeof(uint8_t));
  68. xml_string_copy(hello, hello_0, xml_string_length(hello));
  69. xml_string_copy(world, world_0, xml_string_length(world));
  70. printf("%s %s\n", hello_0, world_0);
  71. free(hello_0);
  72. free(world_0);
  73. /* Extract amount of Root/This children
  74. */
  75. struct xml_node* root_this = xml_node_child(root, 1);
  76. printf("Root/This has %lu children\n", (unsigned long)xml_node_children(root_this));
  77. /* Remember to free the document or you'll risk a memory leak
  78. */
  79. xml_document_free(document, false);
  80. }