title: C++ RapidXML - Edit values in the XML file
body: <p>I recently started using RapidXML, and parsing the values is fine (I can get the data from inside the elements), but I want to edit the values inside of the elements.</p>
  
  <p>For the purposes of this program, I want to turn this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
       &lt;data&gt;
            This is data.
       &lt;/data&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Into this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
       &lt;data&gt;
            Edited!
       &lt;/data&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I read somewhere that the <code>rapidxml::xml_node</code> has a <code>value()</code> function to change the value inside of an element, but it doesn't seem to be working. When I write out to the file, I get the exact same thing that I had before. Here is my code:</p>
  
  <pre><code>std::string input_xml = loadFile(filename);
  std::vector&lt;char&gt; xml_copy(input_xml.begin(), input_xml.end());
  xml_copy.push_back('\0');
  
  rapidxml::xml_document&lt;&gt; doc;
  
  doc.parse&lt;rapidxml::parse_declaration_node | rapidxml::parse_non_destructive&gt;(&amp;xml_copy[0]);
  // Also tried with doc.parse&lt;0&gt;(&amp;xml_copy[0]) but no luck
  
  rapidxml::xml_node&lt;&gt;* root_node = doc.first_node("root");
  
  root_node-&gt;first_node("data")-&gt;value(std::string("Edited!").c_str());
  
  std::string data = std::string(xml_copy.begin(), xml_copy.end());
  
  std::ofstream file;
  file.open(filename.c_str());
  file &lt;&lt; data;
  file.close();
  </code></pre>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>In conjunction with the accepted answer, the <code>parse()</code> function needs to also have the <code>rapidxml::parse_no_data_nodes</code> flag:</p>
  
  <pre><code>std::string input_xml = TileManager::getData(filename);
  std::vector&lt;char&gt; xml_copy(input_xml.begin(), input_xml.end());
  xml_copy.push_back('\0');
  
  rapidxml::xml_document&lt;&gt; doc;
  
  doc.parse&lt;rapidxml::parse_no_data_nodes&gt;(&amp;xml_copy[0]); // Notice the flag here
  rapidxml::xml_node&lt;&gt;* root_node = doc.first_node("root");
  
  std::string s = "test";
  const char * text = doc.allocate_string(s.c_str(), strlen(s.c_str()));
  
  root_node-&gt;first_node("data")-&gt;value(text);
  
  std::string data;
  rapidxml::print(std::back_inserter(data), doc);
  
  std::ofstream file;
  file.open(filename.c_str());
  file &lt;&lt; data;
  file.close();
  </code></pre>
  
  <p>Then it will work.</p>