I am using boost ( 1.69.0 ) serialization to generate an XML template file. The result I want to achieve is unfortunately bad. This is example code:
struct A
{
struct B
{
int bInt;
bool bBool;
private:
friend class boost::serialization::access;
template < class Archive >
void serialize( Archive& ar, const unsigned int version )
{
ar& BOOST_SERIALIZATION_NVP( bInt );
ar& BOOST_SERIALIZATION_NVP( bBool );
}
};
std::vector< B > aVector;
int aInt;
bool abool;
private:
friend class boost::serialization::access;
template < class Archive >
void serialize( Archive& ar, const unsigned int version )
{
ar& BOOST_SERIALIZATION_NVP( aVector );
ar& BOOST_SERIALIZATION_NVP( aInt );
ar& BOOST_SERIALIZATION_NVP( abool );
}
};
And calling
void createXml( const std::filesystem::path& filePath )
{
if (filePath.empty())
{
return;
}
A data;
wofstream wofFile( filePath.c_str() );
boost::archive::xml_woarchive warOutArchive( wofFile );
warOutArchive << boost::serialization::make_nvp( "_Template", data );
}
Result:
<boost_serialization signature="serialization::archive" version="17">
<_Template class_id="0" tracking_level="0" version="0">
<aVector class_id="1" tracking_level="0" version="0">
<count>0</count>
<item_version>0</item_version>
</aVector>
<aInt>-858993460</aInt>
<abool>204</abool>
</_Template>
</boost_serialization>
As you can see, the template generates incorrectly. Content is missing
Everything works well. when I add an element to an empty vector.
sData.aVector.push_back( { 5, true } );
Result:
<boost_serialization signature="serialization::archive" version="17">
<_Template class_id="0" tracking_level="0" version="0">
<aVector class_id="1" tracking_level="0" version="0">
<count>1</count>
<item_version>0</item_version>
<item class_id="2" tracking_level="0" version="0">
<bint>5</bint>
<bbool>1</bbool>
</item>
</aVector>
<aInt>-858993460</aInt>
<abool>204</abool>
</_Template>
</boost_serialization>
Is there any way to generate an xml file template without adding elements to containers? I only need the document template/tree, no values.
Do you ... just want to infer an XML schema for the file? Boost Serialization doesn't have this. You can however prepopulate all your members and then use an existing tool to infer a schema from the example output:
Any tools to generate an XSD schema from an XML instance document?
To automatically prepopulate any container elements you could write a helper function like:
Note that this runs into trouble when you have recursive data structures, because it will run out of memory infinitely deepening. This, however, is the nature of the requirement that your question poses.
For example, replacing
int bIntwithstd::multiset<int> bIntsjust for better demo:Live On Coliru
Which writes