tree2.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * section: Tree
  3. * synopsis: Creates a tree
  4. * purpose: Shows how to create document, nodes and dump it to stdout or file.
  5. * usage: tree2 <filename> -Default output: stdout
  6. * test: tree2 > tree2.tmp ; diff tree2.tmp tree2.res ; rm tree2.tmp
  7. * author: Lucas Brasilino <brasilino@recife.pe.gov.br>
  8. * copy: see Copyright for the status of this software
  9. */
  10. #include <stdio.h>
  11. #include <libxml/parser.h>
  12. #include <libxml/tree.h>
  13. #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
  14. /*
  15. *To compile this file using gcc you can type
  16. *gcc `xml2-config --cflags --libs` -o tree2 tree2.c
  17. */
  18. /* A simple example how to create DOM. Libxml2 automagically
  19. * allocates the necessary amount of memory to it.
  20. */
  21. int
  22. main(int argc, char **argv)
  23. {
  24. xmlDocPtr doc = NULL; /* document pointer */
  25. xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
  26. xmlDtdPtr dtd = NULL; /* DTD pointer */
  27. char buff[256];
  28. int i, j;
  29. LIBXML_TEST_VERSION;
  30. /*
  31. * Creates a new document, a node and set it as a root node
  32. */
  33. doc = xmlNewDoc(BAD_CAST "1.0");
  34. root_node = xmlNewNode(NULL, BAD_CAST "root");
  35. xmlDocSetRootElement(doc, root_node);
  36. /*
  37. * Creates a DTD declaration. Isn't mandatory.
  38. */
  39. dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
  40. /*
  41. * xmlNewChild() creates a new node, which is "attached" as child node
  42. * of root_node node.
  43. */
  44. xmlNewChild(root_node, NULL, BAD_CAST "node1",
  45. BAD_CAST "content of node 1");
  46. /*
  47. * The same as above, but the new child node doesn't have a content
  48. */
  49. xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);
  50. /*
  51. * xmlNewProp() creates attributes, which is "attached" to an node.
  52. * It returns xmlAttrPtr, which isn't used here.
  53. */
  54. node =
  55. xmlNewChild(root_node, NULL, BAD_CAST "node3",
  56. BAD_CAST "this node has attributes");
  57. xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
  58. xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");
  59. /*
  60. * Here goes another way to create nodes. xmlNewNode() and xmlNewText
  61. * creates a node and a text node separately. They are "attached"
  62. * by xmlAddChild()
  63. */
  64. node = xmlNewNode(NULL, BAD_CAST "node4");
  65. node1 = xmlNewText(BAD_CAST
  66. "other way to create content (which is also a node)");
  67. xmlAddChild(node, node1);
  68. xmlAddChild(root_node, node);
  69. /*
  70. * A simple loop that "automates" nodes creation
  71. */
  72. for (i = 5; i < 7; i++) {
  73. sprintf(buff, "node%d", i);
  74. node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
  75. for (j = 1; j < 4; j++) {
  76. sprintf(buff, "node%d%d", i, j);
  77. node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
  78. xmlNewProp(node1, BAD_CAST "odd", BAD_CAST((j % 2) ? "no" : "yes"));
  79. }
  80. }
  81. /*
  82. * Dumping document to stdio or file
  83. */
  84. xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
  85. /*free the document */
  86. xmlFreeDoc(doc);
  87. /*
  88. *Free the global variables that may
  89. *have been allocated by the parser.
  90. */
  91. xmlCleanupParser();
  92. /*
  93. * this is to debug memory for regression tests
  94. */
  95. xmlMemoryDump();
  96. return(0);
  97. }
  98. #else
  99. int main(void) {
  100. fprintf(stderr, "tree support not compiled in\n");
  101. exit(1);
  102. }
  103. #endif