io2.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * section: InputOutput
  3. * synopsis: Output to char buffer
  4. * purpose: Demonstrate the use of xmlDocDumpMemory
  5. * to output document to a character buffer
  6. * usage: io2
  7. * test: io2 > io2.tmp ; diff io2.tmp io2.res ; rm -f io2.tmp
  8. * author: John Fleck
  9. * copy: see Copyright for the status of this software.
  10. */
  11. #include <libxml/parser.h>
  12. #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
  13. int
  14. main(void)
  15. {
  16. xmlNodePtr n;
  17. xmlDocPtr doc;
  18. xmlChar *xmlbuff;
  19. int buffersize;
  20. /*
  21. * Create the document.
  22. */
  23. doc = xmlNewDoc(BAD_CAST "1.0");
  24. n = xmlNewNode(NULL, BAD_CAST "root");
  25. xmlNodeSetContent(n, BAD_CAST "content");
  26. xmlDocSetRootElement(doc, n);
  27. /*
  28. * Dump the document to a buffer and print it
  29. * for demonstration purposes.
  30. */
  31. xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
  32. printf("%s", (char *) xmlbuff);
  33. /*
  34. * Free associated memory.
  35. */
  36. xmlFree(xmlbuff);
  37. xmlFreeDoc(doc);
  38. return (0);
  39. }
  40. #else
  41. #include <stdio.h>
  42. int
  43. main(void)
  44. {
  45. fprintf(stderr,
  46. "library not configured with tree and output support\n");
  47. return (1);
  48. }
  49. #endif