I want to (de)serialize C++ object into XML files. To do so, I use Cereal
library which is lighter than Boost.
So using the Cereal documentation, I created a very simple MWE.
Thus, using Cereal serialize
function inside the object definition, it is possible to export the object into an XML archive.
The MWE:
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <cereal/archives/xml.hpp>
#include <cereal/types/vector.hpp>
using namespace std;
class ClassRectangle
{
private:
/* data */
public:
std::string nameClass="Rectangle";
double length=0.;
double width=0.;
vector<double> center={0., 2.};
template <class Archive>
void serialize( Archive & ar ) const
{
ar( CEREAL_NVP( length ) );
ar( CEREAL_NVP( width ) );
ar( CEREAL_NVP( center ) );
}
};
int main(void)
{
// Beginning of main.
cout << "(Start)" << endl;
// Save Part.
ClassRectangle Shape;
cereal::XMLOutputArchive archive( std::cout );
archive( cereal::make_nvp(Shape.nameClass, Shape) );
// End of the main.
cout << "(End) " << endl;
return 0;
}
// EoF
This example yields:
(Start)
(End)
<?xml version="1.0" encoding="utf-8"?>
<cereal>
<Rectangle>
<length>0</length>
<width>0</width>
<center size="dynamic">
<value0>0</value0>
<value1>1</value1>
</center>
</Rectangle>
</cereal>
Up to this point, everything is fine. However, in this example the Rectangle
object/XML node is whitin an cereal
node.
My question is: How can I remove the <cereal>
XML node ? This would give the following output:
<?xml version="1.0" encoding="utf-8"?>
<Rectangle>
<length>0</length>
<width>0</width>
<center size="dynamic">
<value0>0</value0>
<value1>1</value1>
</center>
</Rectangle>