Why doesn't boost::serialization check for tag names in XML archives?
I'm starting to use boost::serialization on XML archives. I can produce and read data, but when I hand-modify the XML and interchange two tags, it "fails to fail" (i.e. it proceeds happily).
Here's a small, self-complete example showing what I see:
#include <iostream>
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/ser开发者_高级运维ialization/nvp.hpp>
#include <boost/serialization/split_member.hpp>
using namespace std;
int main (void)
{
boost::archive::xml_oarchive oa (cout);
static const string producer = "XXX", version = "0.0.1";
oa << boost::serialization::make_nvp ("producer", producer);
oa << boost::serialization::make_nvp ("producer_version", version);
}
This writes XML to standard output, which contains:
<producer>XXX</producer>
<producer_version>0.0.1</producer_version>
Now, I replace all the code in the main function with a reader:
boost::archive::xml_iarchive ia (cin);
string producer, version;
ia >> boost::serialization::make_nvp ("producer", producer);
ia >> boost::serialization::make_nvp ("producer_version", version);
cout << producer << " " << version << endl;
which works as expected when fed the previous output (outputs "XXX 0.0.1"). If, however, I feed it XML in which I changed the order of the two lines "producer" and "producer_version", it still runs and outputs "0.0.1 XXX".
Thus, it fails to recognize that the tags don't have the expected names, and just proceed. I would have expected it to thrown a xml_archive_parsing_error
exception, as indicated in the doc.
Does someone here have experience with that? What I am doing wrong?
Just changing the order of the two lines won't cause an xml_archive_parsing_error
exception. The doc you've linked says that itself:
(...)This might be possible if only the data is changed and not the XML attributes and nesting structure is left unaltered.(...)
You haven't changed attributes and the order change has kept the structure (still two fields on the first level of your XML). No exception will ever be thrown this way.
Also, when reading a XML using the make_nvp function
, the name parameter won't put any restriction on what is being read. It will just tell arbitrarily the name to be used with the new name-value pair.
So, you can change the name of your XML tags on your input, as long you don't change your expected order i.e. you could rename producer
and producer_version
on your XML to foo
and bar
and still would read the serialized data correctly i.e.:
<foo>XXX</foo>
<bar>0.0.1</bar>
And your printed answer would still be "XXX 0.0.1".
Since this only formatting your serialized data as a XML, there is no interest in checking the tag names. They are only used for making your serialized output more readable.
精彩评论