C++类似于 XSTREAM 的序列化 Clean XML
我需要编写一个 Linux C++ 应用程序,它将设置保存为 XML 格式(以便于手动编辑),并通过套接字和 HTTP 通过 XML 消息与现有应用程序进行通信。问题是我一直无法找到任何智能库来帮助我,我并不特别喜欢编写 DOM 或 SAX 代码只是为了编写和读取一些非常简单的消息。
Boost Serialization 几乎是匹配的,但它向它生成的 xml 添加了大量特定于 boost 的数据。这显然不适用于交换格式。我想知道是否可以使 Boost Serialization 或其他一些 C++ 序列化库生成干净的 xml。我不介意是否有一些必需的额外属性 - 例如版本属性,但我真的希望能够控制它们的命名并摆脱我不使用的“功能” - Tracking_level 和 class_id实例。
理想情况下,我只想拥有类似于 Java 中的 xstream 的东西。我知道 c++ 缺乏内省,因此有必要进行一些手动编码 - 但如果有一个干净的解决方案来读取和写入简单的 XML 而无需组装,那就太好了!
如果无法做到这一点,我也对 XML 模式作为规范资源(契约优先)的工具感兴趣 - 这是 C++ 的一个很好的 JAXB 替代品。到目前为止,我只找到了像 CodeSynthesis XSD 这样的商业解决方案。我更喜欢开源解决方案。我尝试过 gSoap - 但它生成的代码非常难看,而且它也是特定于 SOAP 的。
无奈之下,我还开始寻找原始缓冲区的替代序列化格式。这是存在的——但仅适用于 Java!令我惊讶的是,protocol buffers 似乎是比 XML 更好支持的数据交换格式。
我只是为这个应用程序寻找库而发疯,我真的需要一些新的想法。有人吗?
I need to write a linux c++ app which saves it settings in XML format (for easy hand editing) and also communicates with existing apps through XML messages over sockets and HTTP. Problem is that I haven't been able to find any intelligent libs to help me, I don't particular feel like writing DOM or SAX code just to write and read some very simple messages.
Boost Serialization was almost a match, but it adds a lot of boost-specific data to the xml it generates. This obviously doesn't work well for interchange formats. I'm wondering if it is possible to make Boost Serialization or some other c++ serialization library generate clean xml. I don't mind if there are some required extra attributes - like a version attribute, but I'd really like to be able to control their naming and also get rid of 'features' that I don't use - tracking_level and class_id for instance.
Ideally I would just like to have something similar to xstream in Java. I am aware of the fact that c++ lacks introspection and that it is therefore necessary to do some manual coding - but it would be nice if there was a clean solution to just read and write simple XML without kludges!
If this cannot be done I am also interested in tools where the XML schema is the canonical resource (contract first) - a good JAXB alternative to C++. So far I have only found commercial solutions like CodeSynthesis XSD. I would prefer open source solutions. I have tried gSoap - but it generates really ugly code and it is also SOAP-specific.
In desperation I also started looking at alternative serialization formats for protobuffers. This exists - but only for Java! It really surprises me that protocol buffers seems to be a better supported data interchange format than XML.
I'm going mad just finding libs for this app and I really need some new ideas. Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定它到底提供了什么,因此如果它是您正在寻找的东西,但 Qt 有一个 xml 模块。它还有一个网络模块,听起来好像对您有用。
I'm not sure exactly what it provides and therefore if it's what you are looking for but Qt has an xml module. It also has a network module which sounds as if it may be of some use to you.
为了进一步扩展 Troubadour 的答案,我使用 Qt 的 XML 库 并取得了巨大成功与你非常接近的东西。就我自己而言,我将配置数据序列化到本地文件,然后偶尔将其流式传输到其他线程。
QXmlStreamWriter 和 QXmlStreamReader 是有问题的两个类。 这里是使用它们的示例。
对于我自己的 XML 序列化,我可以完全控制生成的实际格式,并且只需要几百行代码即可实现。关于使用 Qt 的 XML,我能说的最好的事情是我可以专注于内容并让库完成标记数据的痛苦工作 - 这是理想的。
最后,此处是有关 Qt 许可证(商业、GPL、LGPL)的一些信息。
To further expand on Troubadour's answer, I've used Qt's XML libraries with great success doing something very close to what you are. In my own case, I serialize config data to a local file and then occasionally stream it around to other threads.
QXmlStreamWriter and QXmlStreamReader are the two classes in question. Here is an example using them.
For my own XML serialization, I have complete control over the actual format produced and it only took a few hundred lines of code to achieve. The nicest thing I can say about using Qt's XML is that I could focus on the content and let the libraries do the painful work of marking up the data - which is ideal.
Finally, here is some info on Qt Licenses (Commercial, GPL, LGPL).
引用问题中的话:
“到目前为止,我只找到了像 CodeSynthesis 这样的商业解决方案
XSD。我更喜欢开源解决方案”
CodeSynthesis XSD 可以被开源项目使用。该产品
拥有商业专有许可证,但除此之外
还有一个开源许可证(GPL 版本 2,但带有
给予额外的自由)。您可以与所有人一起使用它
“GPL 兼容的自由软件许可证”和所有“GPL 不兼容的自由”
软件许可证”列出于
http://www.gnu.org/licenses/license-list.html
CodeSynthesis XSD 的第三个许可证选项是免费专有许可证
小词汇量许可证。您可以免费使用该许可证
如果您使用的是小型 XML 模式。
在此处阅读有关不同 CodeSynthesis XSD 许可证的更多信息:
http://www.codesynthesis.com/products/xsd/license.xhtml
在功能方面CodeSynthesis XSD还支持序列化
以及解析生成的二进制格式。你可以使用该格式
以实现高效沟通。
A quote from the question:
"So far I have only found commercial solutions like CodeSynthesis
XSD. I would prefer open source solutions"
CodeSynthesis XSD can be used by open source projects. The product
has a Commercial Proprietary License but in addition to that it
also have an Open source license (GPL version 2 but with
additional freedom given). You are allowed to use it together with all
"GPL-Compatible Free Software Licenses" and all "GPL-Incompatible Free
Software Licenses" listed at
http://www.gnu.org/licenses/license-list.html
The third license option for CodeSynthesis XSD is a Free Proprietary
License for Small Vocabularies. You coud use that license for free
if you are using a small XML schema.
Read more about the different CodeSynthesis XSD licenses here:
http://www.codesynthesis.com/products/xsd/license.xhtml
In terms of functionality CodeSynthesis XSD also supports serializing
and parsing of a generated binary format. You could use that format
for efficient communication.