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!

Leave a Comment