parse3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * section: Parsing
  3. * synopsis: Parse an XML document in memory to a tree and free it
  4. * purpose: Demonstrate the use of xmlReadMemory() to read an XML file
  5. * into a tree and and xmlFreeDoc() to free the resulting tree
  6. * usage: parse3
  7. * test: parse3
  8. * author: Daniel Veillard
  9. * copy: see Copyright for the status of this software.
  10. */
  11. #include <stdio.h>
  12. #include <libxml/parser.h>
  13. #include <libxml/tree.h>
  14. static const char *document = "<doc/>";
  15. /**
  16. * example3Func:
  17. * @content: the content of the document
  18. * @length: the length in bytes
  19. *
  20. * Parse the in memory document and free the resulting tree
  21. */
  22. static void
  23. example3Func(const char *content, int length) {
  24. xmlDocPtr doc; /* the resulting document tree */
  25. /*
  26. * The document being in memory, it have no base per RFC 2396,
  27. * and the "noname.xml" argument will serve as its base.
  28. */
  29. doc = xmlReadMemory(content, length, "noname.xml", NULL, 0);
  30. if (doc == NULL) {
  31. fprintf(stderr, "Failed to parse document\n");
  32. return;
  33. }
  34. xmlFreeDoc(doc);
  35. }
  36. int main(void) {
  37. /*
  38. * this initialize the library and check potential ABI mismatches
  39. * between the version it was compiled for and the actual shared
  40. * library used.
  41. */
  42. LIBXML_TEST_VERSION
  43. example3Func(document, 6);
  44. /*
  45. * Cleanup function for the XML library.
  46. */
  47. xmlCleanupParser();
  48. /*
  49. * this is to debug memory for regression tests
  50. */
  51. xmlMemoryDump();
  52. return(0);
  53. }