<p>In my C++ program I want to parse a small piece of XML, insert some nodes, then extract the new XML (preferably as a <code>std::string</code>).<br>
<a href="http://rapidxml.sourceforge.net/" rel="nofollow noreferrer">RapidXml</a> has been recommended to me, but I can't see how to retrieve the XML back as a text string.<br>
(I could iterate over the nodes and attributes and build it myself, but surely there's a build in function that I am missing.)<br>
Thank you.</p>

[{'answer_id': 38127, 'author': 'Adam Tegen', 'author_id': 4066, 'author_profile': 'https://Stackoverflow.com/users/4066', 'pm_score': 0, 'selected': False, 'text': '<p>If you aren\'t yet committed to Rapid XML, I can recommend some alternative libraries:</p>\n\n<ul>\n<li><p>Xerces - This is probably the defacto C++ implementation.</p></li>\n<li><p>XMLite - I\'ve had some luck with this minimal XML implementation.  See the article at <a href="http://www.codeproject.com/KB/recipes/xmlite.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/recipes/xmlite.aspx</a></p></li>\n</ul>\n'}, {'answer_id': 38138, 'author': 'Adam Tegen', 'author_id': 4066, 'author_profile': 'https://Stackoverflow.com/users/4066', 'pm_score': 2, 'selected': False, 'text': "<p>If you do build XML yourself, don't forget to escape the special characters.  This tends to be overlooked, but can cause some serious headaches if it is not implemented:</p>\n\n<ul>\n<li>&lt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;lt;</li>\n<li>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;gt;</li>\n<li>&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;amp;</li>\n<li>&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;quot;</li>\n<li>&apos; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;apos;</li>\n</ul>\n"}, {'answer_id': 38413, 'author': 'Thomas Watnedal', 'author_id': 4059, 'author_profile': 'https://Stackoverflow.com/users/4059', 'pm_score': 4, 'selected': True, 'text': '<p>Althoug the documentation is poor on this topic, I managed to get some working code by looking at the source. Although it is missing the xml header which normally contains important information. Here is a small example program that does what you are looking for using rapidxml:</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;sstream&gt;\n#include "rapidxml/rapidxml.hpp"\n#include "rapidxml/rapidxml_print.hpp"\n\nint main(int argc, char* argv[]) {\n    char xml[] = "&lt;?xml version=\\"1.0\\" encoding=\\"latin-1\\"?&gt;"\n                 "&lt;book&gt;"\n                 "&lt;/book&gt;";\n\n    //Parse the original document\n    rapidxml::xml_document&lt;&gt; doc;\n    doc.parse&lt;0&gt;(xml);\n    std::cout &lt;&lt; "Name of my first node is: " &lt;&lt; doc.first_node()-&gt;name() &lt;&lt; "\\n";\n\n    //Insert something\n    rapidxml::xml_node&lt;&gt; *node = doc.allocate_node(rapidxml::node_element, "author", "John Doe");\n    doc.first_node()-&gt;append_node(node);\n\n    std::stringstream ss;\n    ss &lt;&lt;*doc.first_node();\n    std::string result_xml = ss.str();\n    std::cout &lt;&lt;result_xml&lt;&lt;std::endl;\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 161930, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Use <code>print</code> function (found in <code>rapidxml_print.hpp</code> utility header) to print the XML node contents to a <code>stringstream</code>.</p>\n'}, {'answer_id': 3484223, 'author': 'Petrus Theron', 'author_id': 198927, 'author_profile': 'https://Stackoverflow.com/users/198927', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s how to print a node to a string straight from the <a href="http://rapidxml.sourceforge.net/manual.html" rel="nofollow noreferrer">RapidXML Manual</a>:</p>\n\n<pre><code>xml_document&lt;&gt; doc;    // character type defaults to char\n// ... some code to fill the document\n\n// Print to stream using operator &lt;&lt;\nstd::cout &lt;&lt; doc;   \n\n// Print to stream using print function, specifying printing flags\nprint(std::cout, doc, 0);   // 0 means default printing flags\n\n// Print to string using output iterator\nstd::string s;\nprint(std::back_inserter(s), doc, 0);\n\n// Print to memory buffer using output iterator\nchar buffer[4096];                      // You are responsible for making the buffer large enough!\nchar *end = print(buffer, doc, 0);      // end contains pointer to character after last printed character\n*end = 0;                               // Add string terminator after XML\n</code></pre>\n'}, {'answer_id': 8442106, 'author': 'danath', 'author_id': 1089219, 'author_profile': 'https://Stackoverflow.com/users/1089219', 'pm_score': 2, 'selected': False, 'text': '<p>rapidxml::print reuqires an output iterator to generate the output, so a character string works with it. But this is risky because I can not know whether an array with fixed length (like 2048 bytes) is long enough to hold all the content of the XML.</p>\n\n<p>The right way to do this is to pass in an output iterator of a string stream so allow the buffer to be expanded when the XML is being dumped into it.</p>\n\n<p>My code is like below:</p>\n\n<pre><code>std::stringstream stream;\nstd::ostream_iterator&lt;char&gt; iter(stream);\n\nrapidxml::print(iter, doc, rapidxml::print_no_indenting);\n\nprintf("%s\\n", stream.str().c_str());\nprintf("len = %d\\n", stream.str().size());\n</code></pre>\n'}, {'answer_id': 15023568, 'author': 'uilianries', 'author_id': 2036859, 'author_profile': 'https://Stackoverflow.com/users/2036859', 'pm_score': 0, 'selected': False, 'text': '<p>Use static_cast&lt;></p>\n\n<p>Ex:</p>\n\n<pre><code>rapidxml::xml_document&lt;&gt; doc;\nrapidxml::xml_node &lt;&gt; * root_node = doc.first_node();\nstd::string strBuff;\n\ndoc.parse&lt;0&gt;(xml);\n\n.\n.\n.\nstrBuff = static_cast&lt;std::string&gt;(root_node-&gt;first_attribute("attribute_name")-&gt;value());\n</code></pre>\n'}, {'answer_id': 15086468, 'author': 'duckduckgo', 'author_id': 988930, 'author_profile': 'https://Stackoverflow.com/users/988930', 'pm_score': 0, 'selected': False, 'text': '<p>Following is very easy,</p>\n\n<pre><code>std::string s;\nprint(back_inserter(s), doc, 0);\ncout &lt;&lt; s;\n</code></pre>\n\n<p>You  only need to include "rapidxml_print.hpp" header in your source code.</p>\n'}]
['https://Stackoverflow.com/questions/38037', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3590/']