tags:

views:

998

answers:

10

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?

+1  A: 

metrics

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.

cbrulak
+2  A: 

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.

bobwienholt
+4  A: 

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.

Nemanja Trifunovic
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
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: http://www.tbray.org/ongoing/genx/docs/Guide.html – Nemanja Trifunovic
Nemanja Trifunovic
A: 

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.

Nailer
+1  A: 

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:

http://www.scottlangham.co.uk/2009/01/rapidxml-a-quick-xml-parsergenerator-for-c/

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"/>
</Book>

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.

Scott Langham
+3  A: 

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);
author.addText("Tolkien");

This would result in the following XML:

<book title="Lord of the Rings">
  <author age="43">Tolkien</author>
</book>
James Dean
A: 

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.

gbjbaanb
jdkoftinoff
+1  A: 

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.

A: 

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.

SAMills
+3  A: 

Fast and easy, try TinyXml API :
http://www.grinninglizard.com/tinyxml/

lsalamon
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 (http://download.macromedia.com/pub/developer/acrobat/sdk/9/sdk91_v1_win.zip)
lsalamon