views:

1626

answers:

7

I've used boost serialization but this doesn't appear to allow me to generate xml that conforms to a particular schema -- it seems it's purpose was to just to persist a class's state.

Platform: linux

What do you guys use to generate NOT parse xml?

So far I'm going down Foredecker's route of just generating it myself -- it's not a large document but I really shouldn't be having this much trouble finding a decent library to generate it correctly.

As for boost, the things that I would like to be able to do is set the node names, set attributes in my nodes, and get rid of all the extra crap that comes with it as I don't really care about having to put my document back into that class.

+1  A: 

Xalan-C++ seems quite reasonable to use. :-)

Chris Jester-Young
A: 

I have used libxml++ to geneate XML, but I can't guarantee it's the easiest way! :-)

divideandconquer.se
+4  A: 

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.

Foredecker
thanks a lot! I spent the last 2-3 hours scouring the web, newsgroups, irc, etc... and in the past 20 minutes I built a generator that does exactly what I need to do using this method -- it's not beautiful; needs work but it DOES WORK -- thnx!
feydr
Yep, Heretic! ;) Seriously though, I'd advise against going this route - there are many libraries out there that will help and prevent you from making mistakes in your XML.
MattyT
Well, I have been generating my own XML for years without issue. Like Foredecker, our XML is relatively simple but, perhaps, more complicated than what he states.
Rob
Edmund
A: 

Which platform? MSXML is an option on Windows. CMarkup is another good choice. If .NET is an option, it has excellent XML support.

Serge - appTranslator
CMarkup also works without .Net You can build it to use std::string or MFC CString. It does build the entire doc in memory if that is an issue
Martin Beckett
A: 

I haven't actually tried boost serialization to do this, but as I understand it, if you very carefully pick your options, you can set lots of different options in the schema (such as preventing pointer flyweighting and setting the element names for certain types)

coppro
+12  A: 

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("BytesReceived",  14);
measurements.InsertEndChild(tbr);
root->InsertEndChild(measurements);

Which produces:

<RootNode>
    <measurements>
        <TotalBytesReceived displayName="Total Bytes Received">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!

MattyT
A: 

i use tinyXml++ and it makes it very easy to create xml as posted above. It will also save it to a file in one command but i cant find out how to save it to a buffer.

Lodle