title: Get more control over formatting while using libxml2
body: <p>I have this code (Copied+ a bit minified from <a href="http://xmlsoft.org/examples/tree2.c" rel="nofollow noreferrer">here</a>):</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;libxml/parser.h&gt;
  #include &lt;libxml/tree.h&gt;
  /*
   *To compile this file using gcc you can type
   *gcc `xml2-config --cflags --libs` -o tree2 tree2.c
   */
  
  /* A simple example how to create DOM. Libxml2 automagically 
   * allocates the necessary amount of memory to it.
  */
  int
  main(int argc, char **argv)
  {
      xmlDocPtr doc = NULL;       /* document pointer */
      xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
      char buff[256];
      int i, j;
  
      LIBXML_TEST_VERSION;
      doc = xmlNewDoc(BAD_CAST &quot;1.0&quot;);
      root_node = xmlNewNode(NULL, BAD_CAST &quot;root&quot;);
      xmlDocSetRootElement(doc, root_node);
      xmlCreateIntSubset(doc, BAD_CAST &quot;root&quot;, NULL, BAD_CAST &quot;tree2.dtd&quot;);
      xmlNewChild(root_node, NULL, BAD_CAST &quot;node1&quot;,
                  BAD_CAST &quot;content of node 1&quot;);
      xmlNewChild(root_node, NULL, BAD_CAST &quot;node2&quot;, NULL);
      node =
          xmlNewChild(root_node, NULL, BAD_CAST &quot;node3&quot;,
                      BAD_CAST &quot;this node has attributes&quot;);
      xmlNewProp(node, BAD_CAST &quot;attribute&quot;, BAD_CAST &quot;yes&quot;);
      xmlNewProp(node, BAD_CAST &quot;foo&quot;, BAD_CAST &quot;bar&quot;);
      node = xmlNewNode(NULL, BAD_CAST &quot;node4&quot;);
      node1 = xmlNewText(BAD_CAST
                     &quot;other way to create content (which is also a node)&quot;);
      xmlAddChild(node, node1);
      xmlAddChild(root_node, node);
      for (i = 5; i &lt; 7; i++) {
          sprintf(buff, &quot;node%d&quot;, i);
          node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
          for (j = 1; j &lt; 4; j++) {
              sprintf(buff, &quot;node%d%d&quot;, i, j);
              node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
              xmlNewProp(node1, BAD_CAST &quot;odd&quot;, BAD_CAST((j % 2) ? &quot;no&quot; : &quot;yes&quot;));
          }
      }
      xmlSaveFormatFileEnc(argc &gt; 1 ? argv[1] : &quot;-&quot;, doc, &quot;UTF-8&quot;, 1);
      xmlFreeDoc(doc);
      xmlCleanupParser();
      xmlMemoryDump();
      return(0);
  }
  </code></pre>
  <p>It gives me the output, but only indented by two spaces, I need to control how many spaces are used or if tabs are used for indenting.</p>
  <p>I found <a href="https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/libxml2-xmlsave.html" rel="nofollow noreferrer">this page</a> for saving, but was not able to find any function allowing me to control the indentation.</p>