使用 boost::serialization 序列化为 XML

发布于 2024-09-13 10:55:53 字数 1653 浏览 12 评论 0原文

这是一个新手问题。我试图将一些对象序列化为 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;
}

结果是:

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

不过,我会将这些消息序列化为字符串,并将它们发送到期望消息如下所示的系统。

  <test>
    <a>1</a>
    <b>2</b> 
    <c>3.3</c> 
  </test>

有没有办法在没有签名的情况下序列化xml?

This is a newbie question. I am trying to serialize some objects to XML, but the resulting XML contains a boost serialization signature, version information, class id, ...etc. that I do not need. Is there a way to get rid of them without post-processing the xml message?

#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;
}

results in:

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

I'll be serializing these messages to strings, though, and sending them to systems that expect a message to look like this.

  <test>
    <a>1</a>
    <b>2</b> 
    <c>3.3</c> 
  </test>

Is there a way to serialize xml without the signature?

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

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

发布评论

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

评论(2

摘星┃星的人 2024-09-20 10:55:53

标志 no_header 消除标题行

unsigned int flags = boost::archive::no_header;
boost::archive::xml_oarchive oa(ofs, flags);

以下宏消除属性

BOOST_CLASS_IMPLEMENTATION(Test, object_serializable)

the flag no_header eliminates the heading lines

unsigned int flags = boost::archive::no_header;
boost::archive::xml_oarchive oa(ofs, flags);

the following macro eliminates the attributes

BOOST_CLASS_IMPLEMENTATION(Test, object_serializable)
爱情眠于流年 2024-09-20 10:55:53

这不是 boost::serialization 应该使用的用途。如果您希望生成特定类型的 XML,最好使用 XML 生成器,例如 Xerces (是的,它到处都写着“解析器”,但它也会写入 XML)。

That is not what boost::serialization should be used for. If you're looking to generate a specific type of XML, better use an XML generator like Xerces (yes, it says "parser" everywhere, but it'll also write XML).

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