what's the easiest way to generate xml in c++? what's the easiest way to generate xml in c++? xml xml

what's the easiest way to generate xml in c++?


I recently reviewed a bunch of XML libraries specifically for generating XML code.

Executive summary: I chose to go with TinyXML++.

TinyXML++ has decent C++ syntax, is built on the mature TinyXML C libraries, is free & open source (MIT license) and small. In short, it helps get the job done quickly. Here's a quick snippet:

Document doc;Node* root(doc.InsertEndChild(Element("RootNode")));Element measurements("measurements");Element tbr("TotalBytesReceived",  12);measurements.InsertEndChild(tbr);root->InsertEndChild(measurements);

Which produces:

<RootNode>    <measurements>        <TotalBytesReceived>12</TotalBytesReceived>    </measurements></RootNode>

I've been quite happy with it.

I reviewed many others; here's some of the better contenders:

Xerces: The king-daddy. Does everything (especially when combined with Xalan) but is heavyweight and forces memory management onto the user.

RapidXML: Great for parsing (it's an in-situ parser and is fast) but not good for generation since adding nodes to the DOM requires memory management.

Boost.XML (proposal): Looks great - powerful, excellent C++ syntax. However it hasn't yet gone through the review process, is unsupported and the interface may well change. Almost used it anyway. Looking forward to it's acceptance into Boost.

Libxml(++): Very good; powerful, decent syntax. But it's large-ish if all you're doing is generating XML and is tied to the glibmm library (for ustring). If we were only on Linux (like yourself?) I would seriously consider.

XiMOL: Unique stream-based library. This was a little too simplistic for our needs but for basic XML generation you may find it quite useful. The stream syntax is quite neat.

Hopefully there's something in there of some use!


Some may declare me an XML heretic - but one effective way is to just generate it with your favorite string output tools (print, output streams, etc) - this can go to a buffer or a file.

Once saved - you really should then validate with a schema before committing it our shipping it off.

For one of our projects we have a very simple set of templates for managing begin/end tags and attributes. These each have a stream output operator. This makes it very easy to generate the source XML and debug. This makes the structure of the XML generation code look very much like the XML itself.

One advantage of this is that you can generate large amounts of XML efficiently if streaming to a file. You will pay the validation costs later (presumably at a better time for an expensive operation).

The downside of this technique is that it is essentially output only. It is not suitable for creating then consuming XML dynamically.


Boost.PropertyTree is a nice and straightforward way of generating XML - especially if you are already using Boost.

The following is a complete example program:

#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/xml_parser.hpp>using boost::property_tree::ptree;using boost::property_tree::write_xml;using boost::property_tree::xml_writer_settings;int wmain(int argc, wchar_t* argv[]) {    char* titles[] = {"And Then There Were None", "Android Games", "The Lord of the Rings"};    ptree tree;    tree.add("library.<xmlattr>.version", "1.0");    for (int i = 0; i < 3; i++) {        ptree& book = tree.add("library.books.book", "");        book.add("title", titles[i]);        book.add("<xmlattr>.id", i);        book.add("pageCount", (i+1) * 234);    }    // Note that starting with Boost 1.56, the template argument must be std::string    // instead of char    write_xml("C:\\Users\\Daniel\\Desktop\\test.xml", tree,        std::locale(),        xml_writer_settings<char>(' ', 4));    return 0;}

The resulting XML looks like this:

<?xml version="1.0" encoding="utf-8"?><library version="1.0">    <books>        <book id="0">            <title>And Then There Were None</title>            <pageCount>234</pageCount>        </book>        <book id="1">            <title>Android Games</title>            <pageCount>468</pageCount>        </book>        <book id="2">            <title>The Lord of the Rings</title>            <pageCount>702</pageCount>        </book>    </books></library>

One thing that's particularly nice are the dot-separated paths that allow you to implicitly create all the nodes along the way. The documentation is rather meager, but together with ptree.hpp should give you an idea of how it works.