title: Build an xml tree from scratch - pugixml C++
body: <p>Firstly I would like to say that I have been using an XML parser written by Frank Vanden Berghen and recently trying to migrate to Pugixml. I am finding the transition bit difficult. Hoping to get some help here.</p>
  
  <p>Question: How can I build a tree from scratch for the small xml below using pugixml APIs? I tried looking into the examples on the pugixml home page, but most of them are hard coded with root node values. what I mean is </p>
  
  <pre><code>if (!doc.load("&lt;node id='123'&gt;text&lt;/node&gt;&lt;!-- comment --&gt;", pugi::parse_default | pugi::parse_comments)) return -1;
  </code></pre>
  
  <p>is hard-coded. Also I tried reading about xml_document and xml_node documentation but could not figure out how to start with if I have to build a tree from scratch.</p>
  
  <pre><code>#include "pugixml.hpp"
  
  #include &lt;string.h&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      pugi::xml_document doc;
      if (!doc.load("&lt;node id='123'&gt;text&lt;/node&gt;&lt;!-- comment --&gt;", pugi::parse_default | pugi::parse_comments)) return -1;
  
      //[code_modify_base_node
      pugi::xml_node node = doc.child("node");
  
      // change node name
      std::cout &lt;&lt; node.set_name("notnode");
      std::cout &lt;&lt; ", new node name: " &lt;&lt; node.name() &lt;&lt; std::endl;
  
      // change comment text
      std::cout &lt;&lt; doc.last_child().set_value("useless comment");
      std::cout &lt;&lt; ", new comment text: " &lt;&lt; doc.last_child().value() &lt;&lt; std::endl;
  
      // we can't change value of the element or name of the comment
      std::cout &lt;&lt; node.set_value("1") &lt;&lt; ", " &lt;&lt; doc.last_child().set_name("2") &lt;&lt; std::endl;
      //]
  
      //[code_modify_base_attr
      pugi::xml_attribute attr = node.attribute("id");
  
      // change attribute name/value
      std::cout &lt;&lt; attr.set_name("key") &lt;&lt; ", " &lt;&lt; attr.set_value("345");
      std::cout &lt;&lt; ", new attribute: " &lt;&lt; attr.name() &lt;&lt; "=" &lt;&lt; attr.value() &lt;&lt; std::endl;
  
      // we can use numbers or booleans
      attr.set_value(1.234);
      std::cout &lt;&lt; "new attribute value: " &lt;&lt; attr.value() &lt;&lt; std::endl;
  
      // we can also use assignment operators for more concise code
      attr = true;
      std::cout &lt;&lt; "final attribute value: " &lt;&lt; attr.value() &lt;&lt; std::endl;
      //]
  }
  
  // vim:et
  </code></pre>
  
  <h2>XML:</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;d:testrequest xmlns:d="DAV:" xmlns:o="urn:example.com:testdrive"&gt;
     &lt;d:basicsearch&gt;
        &lt;d:select&gt;
           &lt;d:prop&gt;
              &lt;o:versionnumber/&gt;
              &lt;d:creationdate /&gt;
           &lt;/d:prop&gt;
        &lt;/d:select&gt;
        &lt;d:from&gt;
           &lt;d:scope&gt;
              &lt;d:href&gt;/&lt;/d:href&gt;
              &lt;d:depth&gt;infinity&lt;/d:depth&gt;
           &lt;/d:scope&gt;
        &lt;/d:from&gt;
        &lt;d:where&gt;
           &lt;d:like&gt;
              &lt;d:prop&gt;
                 &lt;o:name /&gt;
              &lt;/d:prop&gt;
              &lt;d:literal&gt;%img%&lt;/d:literal&gt;
           &lt;/d:like&gt;
        &lt;/d:where&gt;
      &lt;/d:basicsearch&gt;
  &lt;/d:testrequest&gt;
  </code></pre>
  
  <p>I could see most of the examples posted on how to read/parse the xml, but I could not find how to create one from the scratch.</p>