tags:

views:

2444

answers:

6

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

A: 

If you aren't yet committed to Rapid XML, I can recommend some alternative libraries:

Adam Tegen
Just don't forget the few hundred percent speed penalty for these libraries -- though they both have a lot more features than RapidXML.
Billy ONeal
+1  A: 

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:

  • <        &lt;
  • >        &gt;
  • &        &amp;
  • "        &quot;
  • '        &apos;
Adam Tegen
+4  A: 

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:

#include <iostream>
#include <sstream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_print.hpp"

int main(int argc, char* argv[]) {
    char xml[] = "<?xml version=\"1.0\" encoding=\"latin-1\"?>"
                 "<book>"
                 "</book>";

    //Parse the original document
    rapidxml::xml_document<> doc;
    doc.parse<0>(xml);
    std::cout << "Name of my first node is: " << doc.first_node()->name() << "\n";

    //Insert something
    rapidxml::xml_node<> *node = doc.allocate_node(rapidxml::node_element, "author", "John Doe");
    doc.first_node()->append_node(node);

    std::stringstream ss;
    ss <<*doc.first_node();
    std::string result_xml = ss.str();
    std::cout <<result_xml<<std::endl;
    return 0;
}
Thomas Watnedal
+4  A: 

Use print function (found in rapidxml_print.hpp utility header) to print the XML node contents to a stringstream.

Excellent, that allows me to suppress node indentation
hamishmcn
A: 

Hi all,

could you please give me some advice why my simple RapidXmL project does not want even to compile ? I reported problem on address: http://stackoverflow.com/questions/2575376/rapidxml-does-not-compile

Br, Milan

milan
+1  A: 

Here's how to print a node to a string straight from the RapidXML Manual:

xml_document<> doc;    // character type defaults to char
// ... some code to fill the document

// Print to stream using operator <<
std::cout << doc;   

// Print to stream using print function, specifying printing flags
print(std::cout, doc, 0);   // 0 means default printing flags

// Print to string using output iterator
std::string s;
print(std::back_inserter(s), doc, 0);

// Print to memory buffer using output iterator
char buffer[4096];                      // You are responsible for making the buffer large enough!
char *end = print(buffer, doc, 0);      // end contains pointer to character after last printed character
*end = 0;                               // Add string terminator after XML
FreshCode