nav*_*tor 12 c++ xml boost-serialization
这是一个新手问题.我试图将一些对象序列化为XML,但生成的XML包含boost序列化签名,版本信息,类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;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
<?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>
Run Code Online (Sandbox Code Playgroud)
不过,我会将这些消息序列化为字符串,并将它们发送到期望消息看起来像这样的系统.
<test>
<a>1</a>
<b>2</b>
<c>3.3</c>
</test>
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有签名的情况下序列化xml?
ste*_*fan 15
标志no_header消除了标题行
unsigned int flags = boost::archive::no_header;
boost::archive::xml_oarchive oa(ofs, flags);
Run Code Online (Sandbox Code Playgroud)
以下宏删除了属性
BOOST_CLASS_IMPLEMENTATION(Test, object_serializable)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13016 次 |
| 最近记录: |