C++ 中的 XML 序列化/反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1380192/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:46:34  来源:igfitidea点击:

XML Serialization/Deserialization in C++

c++xml-serializationserializationtinyxml

提问by Lily

I am using C++ from Mingw, which is the windows version of GNC C++.

我使用的是 Mingw 的 C++,它是 GNC C++ 的 Windows 版本。

What I want to do is: serialize C++ object into an XML file and deserialize object from XML file on the fly. I check TinyXML. It's pretty useful, and (please correct me if I misunderstand it) it basically add all the nodes during processing, and finally put them into a file in one chunk using TixmlDocument::saveToFile(filename) function.

我想要做的是:将 C++ 对象序列化为 XML 文件,并动态地从 XML 文件中反序列化对象。我检查 TinyXML。它非常有用,并且(如果我误解了请纠正我)它基本上在处理过程中添加了所有节点,最后使用 TixmlDocument::saveToFile(filename) 函数将它们放入一个文件中。

I am working on real-time processing, and how can I write to a file on the fly and append the following result to the file?

我正在进行实时处理,如何即时写入文件并将以下结果附加到文件中?

Thanks.

谢谢。

采纳答案by AnthonyLambert

I notice that each TiXmlBase Class has a Print method and also supports streaming to strings and streams.

我注意到每个 TiXmlBase 类都有一个 Print 方法,并且还支持流式传输到字符串和流。

You could walk the new parts of the document in sequence and output those parts as they are added, maybe?

您可以按顺序遍历文档的新部分并在添加这些部分时输出这些部分,也许吧?

Give it a try.....

试一试.....

Tony

托尼

回答by TimW

BOOST has a very nice Serialization/Deserialization lib BOOST.Serialization.
If you stream your objects to a boost xml archiveit will stream them in xml format. If xml is to big or to slow you only need to change the archive in a text or binary archive to change the streaming format.

BOOST 有一个非常好的序列化/反序列化库BOOST.Serialization
如果您将对象流式传输到 boost xml存档,它将以 xml 格式流式传输它们。如果 xml 太大或太慢,您只需更改文本或二进制存档中的存档即可更改流格式。

回答by user272479

Here is a better example of C++ object serialization:

这是一个更好的 C++ 对象序列化示例:

http://www.codeproject.com/KB/XML/XMLFoundation.aspx

http://www.codeproject.com/KB/XML/XMLFoundation.aspx

回答by Dr. Alex RE

I've been using gSOAPfor this purpose. It is probably too powerful for just XML serialization, but knowing it can do much more means I do not have to consider other solutions for more advanced projects since it also supports WSDL, SOAP, XML-RPC, and JSON. Also suitable for embedded and small devices, since XML is simply a transient wire format and not kept in a DOM or something memory intensive.

为此,我一直在使用gSOAP。对于仅用于 XML 序列化来说它可能太强大了,但是知道它可以做更多的事情意味着我不必为更高级的项目考虑其他解决方案,因为它也支持 WSDL、SOAP、XML-RPC 和 JSON。也适用于嵌入式和小型设备,因为 XML 只是一种暂时的有线格式,而不是保存在 DOM 或内存密集型的东西中。