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