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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

78 Zeilen
2.3 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. * @warning 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. /* Extract amount of Root/This children
  59. */
  60. struct xml_node* root_this = xml_node_child(root, 1);
  61. printf("Root/This has %lu children\n", (unsigned long)xml_node_children(root_this));
  62. /* Remember to free the document or you'll risk a memory leak
  63. */
  64. xml_document_free(document, false);
  65. }