这是一个新手问题。我正在尝试将一些对象序列化为 XML,但生成的 XML 包含增强序列化签名、版本信息、类 id 等。我不需要。有没有办法在不对 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;
}
结果是:
<?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>
不过,我会将这些消息序列化为字符串,并将它们发送到期望消息看起来像这样的系统。
<test>
<a>1</a>
<b>2</b>
<c>3.3</c>
</test>
有没有办法在没有签名的情况下序列化 xml?