[英]Serialize to XML using boost::serialization
This is a newbie question. 这是一个新手问题。 I am trying to serialize some objects to XML, but the resulting XML contains a boost serialization signature, version information, class id, ...etc. 我试图将一些对象序列化为XML,但生成的XML包含boost序列化签名,版本信息,类ID,等等。 that I do not need. 我不需要。 Is there a way to get rid of them without post-processing the xml message? 有没有办法在没有后处理xml消息的情况下摆脱它们?
#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
using namespace std;
class Test {
private:
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar,
const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(a);
ar & BOOST_SERIALIZATION_NVP(b);
ar & BOOST_SERIALIZATION_NVP(c);
}
int a;
int b;
float c;
public:
inline Test(int a, int b, float c) {
this->a = a;
this->b = b;
this->c = c;
}
};
int main() {
std::ofstream ofs("filename.xml");
Test* test = new Test(1, 2, 3.3);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(test);
return 0;
}
results in: 结果是:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization (View Source for full doctype...)>
<boost_serialization signature="serialization::archive" version="6">
<test class_id="0" tracking_level="1" version="0" object_id="_0">
<a>1</a>
<b>2</b>
<c>3.3</c>
</test>
</boost_serialization>
I'll be serializing these messages to strings, though, and sending them to systems that expect a message to look like this. 不过,我会将这些消息序列化为字符串,并将它们发送到期望消息看起来像这样的系统。
<test>
<a>1</a>
<b>2</b>
<c>3.3</c>
</test>
Is there a way to serialize xml without the signature? 有没有办法在没有签名的情况下序列化xml?
the flag no_header eliminates the heading lines 标志no_header消除了标题行
unsigned int flags = boost::archive::no_header;
boost::archive::xml_oarchive oa(ofs, flags);
the following macro eliminates the attributes 以下宏删除了属性
BOOST_CLASS_IMPLEMENTATION(Test, object_serializable)
That is not what boost::serialization
should be used for. 这不应该是boost::serialization
应该用于什么。 If you're looking to generate a specific type of XML, better use an XML generator like Xerces (yes, it says "parser" everywhere, but it'll also write XML). 如果您希望生成特定类型的XML,最好使用像Xerces这样的XML生成器(是的,它在任何地方都说“解析器”,但它也会编写XML)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.