title: Failure to create two sibling nodes in the root node using libxml2 writer handler
body: <p>I am new to libxml2 trying to generate XML files for the first time. But unable to create sibling node in the root node when try to create an XML file via <a href="http://www.xmlsoft.org/examples/index.html#testWriter.c" rel="nofollow noreferrer">testWriter.c</a> link,The libxml2 will print an error message.</p>
  <p><strong>I want to create this example xml file. The point that there are sibling nodes in the root node:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root&gt;
      &lt;!--child1 comment--&gt;
      &lt;child1&gt;
          &lt;element11&gt;value11&lt;/element11&gt;
          &lt;element11&gt;12&lt;/element11&gt;
      &lt;/child1&gt;
  
      &lt;!--child2 comment--&gt;
      &lt;child2&gt;
          &lt;element21&gt;value21&lt;/element21&gt;
          &lt;element22&gt;22&lt;/element22&gt;
      &lt;/child2&gt;
  &lt;/root&gt;
  </code></pre>
  <p><strong>My writer handler code:</strong></p>
  <pre class="lang-c prettyprint-override"><code>static void
  gen_child1(xmlTextWriterPtr writer) {
      char buf[64];
  
      // comment
      xmlTextWriterWriteComment(writer, BAD_CAST &quot;child1 comment&quot;);
  
      // start
      if( xmlTextWriterStartElement(writer, BAD_CAST &quot;child1&quot;) &lt; 0 )
          return;
  
      // add a child element
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element11&quot;, BAD_CAST &quot;value11&quot;);
  
      // add a child element
      (void) snprintf(buf, sizeof buf, &quot;%d&quot;, 12);
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element12&quot;, BAD_CAST buf);
  
      // start new node
      if( xmlTextWriterStartElement(writer, BAD_CAST &quot;element13&quot;) &lt; 0 )
          return;
  
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element131&quot;, BAD_CAST &quot;value131&quot;);
  
      // end new node
      xmlTextWriterEndElement(writer);
  
      // end
      xmlTextWriterEndElement(writer);
  }
  
  
  static void
  gen_child2(xmlTextWriterPtr writer) {
      char buf[64];
  
      // comment
      xmlTextWriterWriteComment(writer, BAD_CAST &quot;child2 comment&quot;);
  
      // start
      if( xmlTextWriterStartElement(writer, BAD_CAST &quot;child2&quot;) &lt; 0 )
          return;
  
      // add a child element
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element21&quot;, BAD_CAST &quot;value21&quot;);
  
      // add a child element
      (void) snprintf(buf, sizeof buf, &quot;%d&quot;, 22);
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element22&quot;, BAD_CAST buf);
  
      // start new node
      if( xmlTextWriterStartElement(writer, BAD_CAST &quot;element23&quot;) &lt; 0 )
          return;
  
      xmlTextWriterWriteElement(writer, BAD_CAST &quot;element231&quot;, BAD_CAST &quot;value231&quot;);
  
      // end new node
      xmlTextWriterEndElement(writer);
  
      // end
      xmlTextWriterEndElement(writer);
  }
  
  
  static void
  gen_doc(void) {
      xmlTextWriterPtr writer = NULL;
      xmlDocPtr doc;
      xmlNodePtr root;
  
      if( !(doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION)) )
          puts(&quot;xmlNewDoc() failure&quot;);
  
      if( !(root = xmlNewDocNode(doc, NULL, BAD_CAST &quot;root&quot;, NULL)) )
          puts(&quot;xmlNewDocNode() failure&quot;);
  
      xmlDocSetRootElement(doc, root);
  
      // create writer
      if( !(writer = xmlNewTextWriterTree(doc, root, 0)) )
          puts(&quot;xmlNewTextWriterTree() failure&quot;);
  
      // start doc
      if( xmlTextWriterStartDocument(writer, NULL, ENCODING, NULL) &lt; 0 )
          puts(&quot;xmlTextWriterStartDocument() failure&quot;);
  
      gen_child1(writer);
      gen_child2(writer);
  
      // end doc
      xmlTextWriterEndDocument(writer);
  
      // free writer
      xmlFreeTextWriter(writer);
  
      print_doc(doc);
  }
  </code></pre>
  <p><strong>Libxml2 print this error message:</strong></p>
  <pre class="lang-sh prettyprint-override"><code># This is Libxml2 output:
  Entity: line 2: parser error : Extra content at the end of the document
  nt13&gt;&lt;element131&gt;value131&lt;/element131&gt;&lt;/element13&gt;&lt;/child1&gt;&lt;!--child2 comment--&gt;
                                                                                 ^
  error : xmlTextWriterWriteDocCallback : XML error 5 !
  I/O error : flush error
  error : xmlTextWriterCloseDocCallback : XML error 5 !
  
  # This is my print_doc(doc) output:
  Length: 224
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root&gt;&lt;!--child1 comment--&gt;&lt;child1&gt;&lt;element11&gt;value11&lt;/element11&gt;&lt;element12&gt;12&lt;/element12&gt;&lt;element13&gt;&lt;element131&gt;value131&lt;/element131&gt;&lt;/element13&gt;&lt;/child1&gt;&lt;/root&gt;
  &lt;!--child2 comment--&gt;
  </code></pre>
  <p><strong>I will succeed without using the writer handler, this the code:</strong></p>
  <pre class="lang-c prettyprint-override"><code>
  static xmlNodePtr
  gen_child1(xmlNsPtr _ns, const xmlChar *_name) {
      xmlNodePtr retval = xmlNewNode(_ns, _name);
      char buf[64];
  
      if( retval ) {
          xmlNewTextChild(retval, _ns, BAD_CAST &quot;element11&quot;, BAD_CAST &quot;value11&quot;);
  
          (void) snprintf(buf, sizeof buf, &quot;%d&quot;, 12);
          xmlNewTextChild(retval, _ns, BAD_CAST &quot;element11&quot;, BAD_CAST buf);
      }
  
      return retval;
  }
  
  
  static xmlNodePtr
  gen_child2(xmlNsPtr _ns, const xmlChar *_name) {
      xmlNodePtr retval = xmlNewNode(_ns, _name);
      char buf[64];
  
      if( retval ) {
          xmlNewTextChild(retval, _ns, BAD_CAST &quot;element21&quot;, BAD_CAST &quot;value21&quot;);
  
          (void) snprintf(buf, sizeof buf, &quot;%d&quot;, 22);
          xmlNewTextChild(retval, _ns, BAD_CAST &quot;element22&quot;, BAD_CAST buf);
      }
  
      return retval;
  }
  
  
  static void
  gen_doc(void) {
      xmlTextWriterPtr writer = NULL;
      xmlDocPtr doc;
      xmlNodePtr root;
  
      if( !(doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION)) )
          puts(&quot;xmlNewDoc() failure&quot;);
  
      if( !(root = xmlNewDocNode(doc, NULL, BAD_CAST &quot;root&quot;, NULL)) )
          puts(&quot;xmlNewDocNode() failure&quot;);
  
      xmlDocSetRootElement(doc, root);
  
      xmlAddChild(root, xmlNewComment(BAD_CAST &quot;child1 comment&quot;));
      xmlAddChild(root, gen_child1(NULL, BAD_CAST &quot;child1&quot;));
  
      xmlAddChild(root, xmlNewComment(BAD_CAST &quot;child2 comment&quot;));
      xmlAddChild(root, gen_child2(NULL, BAD_CAST &quot;child2&quot;));
  
      print_doc(doc);
  
      xmlFreeDoc(doc);
  }
  </code></pre>
  <p><strong>I want to know what i did wrong when using the writer handler?</strong></p>