Take the 2-minute tour ×

In my project there are situations where we have to send xml messages (as char *) among modules. They are not really large ones, just 10-15 lines. Right now everybody just creates the string themselves. I dont think this is the right approach. We are already using xerces DOM library. So why not create a Dom tree, serialize it, and then send it.

What do you guys suggest?

share|improve this question

10 Answers 10

up vote 4 down vote accepted

If you are really just creating small XML messages, Xerces is an overkill, IMHO. It is a parser library and you are not parsing anything.

share|improve this answer
I strongly disagree. Creating the XML by hand is quite error-prone (encoding issues, escaping of characters) so I do recommend to create a tree (DOM or something else) and then to serialize it. –  bortzmeyer Jan 30 '09 at 9:52
Hey, I just said "Xerces is an overkill". Escaping can (and should) be handled by a single function or a lightweight library such as Genx: tbray.org/ongoing/genx/docs/Guide.html – Nemanja Trifunovic –  Nemanja Trifunovic Jan 30 '09 at 13:49

You could use the DOM or write you own small C++ XML generation framework.

The Xerces API was ported from Java and by using object scope you can make generating XML from C++ code even easier:

XmlWriter w;
Elem book = w.Element("book");
book.addAttrib("title", "Lord of the Rings");
Elem author = e.addChild("author");
author.addAttrib("age", 43);

This would result in the following XML:

<book title="Lord of the Rings">
  <author age="43">Tolkien</author>
share|improve this answer

Fast and easy, try TinyXml API :

share|improve this answer
To corroborate its quality, see who is present in the SDK from Adobe. look in \sdk91_v1_win\Adobe\Acrobat 9 SDK\Version 1\NonAdobeSupport\TinyXML (download.macromedia.com/pub/developer/acrobat/sdk/9/…) –  lsalamon Aug 9 '09 at 18:51

I suggest creating the DOM tree. You won't have to worry about escaping XML characters in your data. If one developer forgets one time, you could have user input that makes your XML output incorrect.

share|improve this answer


compare how long it takes you to manually create the string a dozen times or so, then compare how long it takes with the library.

If your team members are like mine, they'll quickly change their tune.

plus, factor in QA time if they code it wrong, etc...

use the library as much as you can.

share|improve this answer

I had the same problem with wanting to pass xml messages between embedded devices.

I wrote the following (all in one header file) to be a fast api for parsing and generating xml. It doesn't support all of xml features, but most simple xml messages don't need them anyway:


There's example usage shown in the comments.

To write xml you can do something like the following:

int isbn = 2938237;
wstring authorName = "Isaac Asimov";
wstring borrower = "Mr. Blobby";

CWTag bookTag = libraryTag(L"Book")
    (L"author",authorName) (L"isbn",isbn) (L"borrower",borrower);
bookTag(L"Review") (L"comments", L"very good");

and this results in the new xml being added under the tag:

<Book author="Isaac Asimov" isbn="2938237" borrower="Mr. Blobby">
    <Review comments="very good"/>

We did try another library, I can't remember which one, and found it wasn't quick enough. We didn't want to stick with manually formatting strings because we were dealing with quite a lot of messages.

share|improve this answer

Creating XML by hand is one of the surest ways to insanity I know of. Eventually someone will screw up an entity somewhere, or a trailing bracket'll be left off, or something... just use a library. GenX would be my choice.

Edit: Misread your post. If you've already got a generation library, use that. Don't complicate things unnecessarily if you can avoid it.

share|improve this answer

I would just use the DOM tree. That way you don't have to parse the data back and forth when sending and recieving the xml strings.

--- Slightly off topic: For other people just dropping by this post and by chance need an ultra-lightweight DOM xml framework use this one: http://www.applied-mathematics.net/tools/xmlParser.html

It's very easy to use, and consists of 2 files.

share|improve this answer

If you're creating a medium to large XML file, you will need a library to assist you. If you're creating a small to very small XML file, you might as well just stream the text into it.

astream << "<book title=\"Lord of the Rings\"><author age=\"43\">Tolkien</author></book>"

is about as easy as you can get for that size XML fragment, if your XML files are that size, you might as well just construct them in this way - don't try to overcomplicate matters just for some purist ideal that doesn't actually help you.

Like optimisation, you should never change things without measuring what effect it'd have. Here, if your code is maintainable as is, keep with it. Only try to change it when it you've measured its maintainability (ie noticed its awkward to update the XMLs) and found that its taking too long for comfort.

share|improve this answer
as long as the programmer knows how to deal with characters like '<' and '>' and '&' in the xml elements - and unicode too. –  jdkoftinoff Jan 29 '09 at 22:38

I actually use CMarkup from FirstObject.com.

CodeProject has an older distribution and textual tutorial (http://www.codeproject.com/KB/cpp/markupclass.aspx)

Source code distributions are available and the component comes in several flavors (Developer, Advanced Developer) via the FirstObject download page(s).

The FirstObject web site contains many helpful examples and they even distribute a Free XML Editor.

No I'm not an employee but I am a satisfied owner of the component.

share|improve this answer

Your Answer


