为什么 boost::serialization 不检查 XML 档案中的标签名称?

发布于 2024-08-20 15:35:59 字数 1532 浏览 4 评论 0原文

我开始在 XML 档案上使用 boost::serialization。我可以生成和读取数据,但是当我手动修改 XML 并交换两个标签时,它“无法失败”(即它顺利进行)。

这是一个小型的、自我完成的示例,展示了我所看到的内容:

#include <iostream>
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/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);
}

这会将 XML 写入标准输出,其中包含:

<producer>XXX</producer>
<producer_version>0.0.1</producer_version>

现在,我用读取器替换主函数中的所有代码:

  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;

当输入先前的输出(输出“ XXX 0.0.1")。但是,如果我向它提供 XML,其中更改了“生产者”和“生产者_版本”两行的顺序,它仍然运行并输出“0.0.1 XXX”。

因此,它无法识别标签没有预期的名称,然后继续。我预计它会抛出 xml_archive_parsing_error 异常,如 文档

这里有人有这方面的经验吗?我做错了什么?

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/serialization/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-08-27 15:35:59

仅更改两行的顺序不会导致 xml_archive_parsing_error 异常。您链接的 doc 说那本身:

(...)如果仅更改数据而不更改 XML 属性和嵌套结构,则这可能是可能的。(...)

您没有更改属性并且顺序更改保留了结构(仍然是两个字段)在 XML 的第一层)。这样就不会抛出任何异常。

此外,当使用 make_nvp 函数读取 XML 时,name 参数不会对读取的内容施加任何限制。它只会任意告诉新名称-值对要使用的名称。

因此,您可以更改输入中 XML 标记的名称,只要不更改预期顺序即可,即您可以将 XML 上的 Producer 和 Producer_version 重命名为foobar 仍然可以正确读取序列化数据,即:

XXX
0.0.1

并且您打印的答案仍然是“XXX 0.0.1”

由于这仅将序列化数据格式化为 XML,因此没有兴趣检查标签名称。它们仅用于使序列化输出更具可读性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文