Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...