在 C++ 中生成 xml 的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/303371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the easiest way to generate xml in c++?
提问by eyberg
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.
我使用了 boost 序列化,但这似乎不允许我生成符合特定模式的 xml——它的目的似乎只是为了保持类的状态。
Platform: linux
平台:linux
What do you guys use to generate NOT parse xml?
你们用什么来生成 NOT 解析 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.
到目前为止,我正在沿着 Foredecker 自己生成它的路线前进——它不是一个大文档,但我真的不应该在找到一个像样的库来正确生成它时遇到这么多麻烦。
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.
至于提升,我希望能够做的事情是设置节点名称,在我的节点中设置属性,并摆脱它带来的所有额外的废话,因为我真的不在乎必须把我的文件回到那个班级。
采纳答案by Foredecker
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.
有些人可能会宣称我是 XML 异端——但一种有效的方法是使用你最喜欢的字符串输出工具(打印、输出流等)生成它——这可以转到缓冲区或文件。
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.
对于我们的一个项目,我们有一组非常简单的模板来管理开始/结束标签和属性。这些每个都有一个流输出操作符。这使得生成源 XML 和调试变得非常容易。这使得 XML 生成代码的结构看起来非常像 XML 本身。
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).
这样做的一个优点是,如果流式传输到文件,您可以高效地生成大量 XML。您将在稍后支付验证费用(大概是在进行昂贵操作的更好时间)。
The downside of this technique is that it is essentially output only. It is not suitable for creating then consuming XML dynamically.
这种技术的缺点是它本质上只是输出。它不适合动态创建然后使用 XML。
回答by MattyT
I recently reviewed a bunch of XML libraries specifically for generating XML code.
我最近回顾了一些专门用于生成 XML 代码的 XML 库。
Executive summary: I chose to go with TinyXML++.
执行摘要:我选择使用TinyXML++。
TinyXML++ has decent C++ syntax, is built on the mature TinyXMLC libraries, is free & open source (MIT license) and small. In short, it helps get the job done quickly. Here's a quick snippet:
TinyXML++ 具有不错的 C++ 语法,建立在成熟的TinyXMLC 库之上,免费且开源(MIT 许可)且体积小。简而言之,它有助于快速完成工作。这是一个快速片段:
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.
Xerces:国王爸爸。难道一切(尤其是当与结合的Xalan),但权重股和力量的内存管理到用户。
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.
RapidXML:非常适合解析(它是一个原位解析器并且速度很快)但不适合生成,因为将节点添加到 DOM 需要内存管理。
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.
Boost.XML(提案):看起来很棒- 强大、优秀的 C++ 语法。然而,它还没有通过过程,不受支持,界面可能会发生变化。反正都差不多用了。期待它被 Boost 接受。
Libxml(++): Very good; powerful, decent syntax. But it's large-ish if all you're doing is generatingXML and is tied to the glibmm library (for ustring). If we were onlyon Linux (like yourself?) I would seriously consider.
Libxml( ++):非常好;强大,体面的语法。但是,如果您所做的只是生成XML 并且与 glibmm 库(用于 ustring)相关联,那么这就是大问题。如果我们只在 Linux 上(像你一样?)我会认真考虑。
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.
XiMOL:独特的基于流的库。这对于我们的需求来说有点过于简单,但是对于基本的 XML 生成,您可能会发现它非常有用。流语法非常简洁。
Hopefully there's something in there of some use!
希望里面有一些有用的东西!
回答by Daniel Wolf
Boost.PropertyTreeis a nice and straightforward way of generating XML - especially if you are already using Boost.
Boost.PropertyTree是一种很好且直接的生成 XML 的方法 - 特别是如果您已经在使用 Boost。
The following is a complete example program:
下面是一个完整的示例程序:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::write_xml;
using boost::property_tree::xml_writer_settings;
int wmain(int argc, wchar_t* argv[]) {
char* titles[] = {"And Then There Were None", "Android Games", "The Lord of the Rings"};
ptree tree;
tree.add("library.<xmlattr>.version", "1.0");
for (int i = 0; i < 3; i++) {
ptree& book = tree.add("library.books.book", "");
book.add("title", titles[i]);
book.add("<xmlattr>.id", i);
book.add("pageCount", (i+1) * 234);
}
// Note that starting with Boost 1.56, the template argument must be std::string
// instead of char
write_xml("C:\Users\Daniel\Desktop\test.xml", tree,
std::locale(),
xml_writer_settings<char>(' ', 4));
return 0;
}
The resulting XML looks like this:
生成的 XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<library version="1.0">
<books>
<book id="0">
<title>And Then There Were None</title>
<pageCount>234</pageCount>
</book>
<book id="1">
<title>Android Games</title>
<pageCount>468</pageCount>
</book>
<book id="2">
<title>The Lord of the Rings</title>
<pageCount>702</pageCount>
</book>
</books>
</library>
One thing that's particularly nice are the dot-separated paths that allow you to implicitly create all the nodes along the way. The documentationis rather meager, but together with ptree.hpp
should give you an idea of how it works.
特别好的一件事是点分隔路径,它允许您沿途隐式创建所有节点。该文档是相当微薄,但用起来ptree.hpp
应该给你它是如何工作的想法。
回答by Lodle
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.
我使用 tinyXml++,它可以很容易地创建上面发布的 xml。它还将在一个命令中将其保存到一个文件中,但我无法找到如何将其保存到缓冲区中。
回答by Serge Wautier
回答by coppro
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)
我实际上并没有尝试使用 boost 序列化来做到这一点,但据我所知,如果你非常仔细地选择你的选项,你可以在模式中设置许多不同的选项(例如防止指针飞行加权和设置某些类型的元素名称)