热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【求助】使用Boost::Serialization将对象序列化到xml中,如何输出“class_name”字段?大侠们帮帮忙,在线等,谢谢!

当前我的代码输出的xml是这样的:<sclass_tracking_level1version2object_>而我希望输出
当前我的代码输出的xml是这样的:



而我希望输出的是这样的:



就是需要把class_name="bus_schedule"字段写入到xml中去,方便其他程序(非C++ Boost)能够知道该还原成什么类型

感觉应该是serialization的用法不对,但是一直没找到办法,还请大家指教,谢谢!

源码如下:
#include "stdafx.h"
#include 
#include 
#include 

#include 
#include 

#include "demo_gps.hpp"

BOOST_CLASS_IMPLEMENTATION(bus_schedule, boost::serialization::object_class_info);
BOOST_CLASS_TRACKING(bus_schedule, boost::serialization::track_always)

void restore_schedule(bus_schedule &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(s);
}

void save_schedule(const bus_schedule &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);

  ////////////////////////////////////////////////////////Does this cause the problem?
  oa << BOOST_SERIALIZATION_NVP(s);

// oa << boost::serialization::make_nvp(BOOST_PP_STRINGIZE(bus_schedule), s);
}

int _tmain(int argc, _TCHAR* argv[])
{
// make the schedule
bus_schedule original_schedule;

// fill in the data
// make a few stops
bus_stop *bs0 = new bus_stop_corner(
gps_position(34, 135, 52.560f),
gps_position(134, 22, 78.30f),
"24th Street", "10th Avenue"
);

// make a  routes
bus_route route0;
  route0.append(bs0);

// add trips to schedule
  original_schedule.append("bob", 6, 24, &route0);

// display the complete schedule
std::cout << "original schedule";
std::cout << original_schedule;

std::string filename(boost::archive::tmpdir());
filename += "/demo_save.xml";

// save the schedule
save_schedule(original_schedule, filename.c_str());

delete bs0;

// make  a new schedule
bus_schedule new_schedule;

restore_schedule(new_schedule, filename.c_str());

// and display
std::cout << "\nrestored schedule";
std::cout << new_schedule;

return 0;
}

2 个解决方案

#1


demo_gps.hpp
#ifndef BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP
#define BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
//
// demo_gps.hpp
//
// (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

// This illustration models the bus system of a small city.
// This includes, multiple bus stops,  bus routes and schedules.
// There are different kinds of stops.  Bus stops in general will
// will appear on multiple routes.  A schedule will include
// muliple trips on the same route.

/////////////////////////////////////////////////////////////
// gps coordinate
//
// llustrates serialization for a simple type
//
class gps_position
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);

    int degrees;
    int minutes;
    float seconds;

    template
    void serialize(Archive & ar, const unsigned int /* file_version */){
        ar  & BOOST_SERIALIZATION_NVP(degrees)
            & BOOST_SERIALIZATION_NVP(minutes)
            & BOOST_SERIALIZATION_NVP(seconds);
    }

public:
    // every serializable class needs a constructor
    gps_position(){};
    gps_position(int _d, int _m, float _s) : 
        degrees(_d), minutes(_m), seconds(_s)
    {}
};

std::ostream & operator<<(std::ostream &os, const gps_position &gp)
{
    return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
}

/////////////////////////////////////////////////////////////
// One bus stop
//
// illustrates serialization of serializable members
//

class bus_stop
{
    friend class boost::serialization::access;
    virtual std::string description() const = 0;
    gps_position latitude;
    gps_position longitude;

    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(latitude);
        ar & BOOST_SERIALIZATION_NVP(longitude);
    }

protected:
    bus_stop(const gps_position & _lat, const gps_position & _long) :
        latitude(_lat), longitude(_long)
    {}
public:
    bus_stop(){}
    friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
    virtual ~bus_stop(){}
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(bus_stop)

std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
{
    return os << bs.latitude << bs.longitude << ' ' << bs.description();
}

/////////////////////////////////////////////////////////////
// Several kinds of bus stops
//
// illustrates serialization of derived types
//
class bus_stop_corner : public bus_stop
{
    friend class boost::serialization::access;
    std::string street1;
    std::string street2;
    virtual std::string description() const
    {
        return street1 + " and " + street2;
    }
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        // save/load base class information
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop);
        ar & BOOST_SERIALIZATION_NVP(street1);
        ar & BOOST_SERIALIZATION_NVP(street2);
    }
public:
    bus_stop_corner(){}
    bus_stop_corner(const gps_position & _lat, const gps_position & _long,
        const std::string & _s1, const std::string & _s2
    ) :
        bus_stop(_lat, _long), street1(_s1), street2(_s2)
    {
    }
};

class bus_stop_destination : public bus_stop
{
    friend class boost::serialization::access;
    std::string name;
    virtual std::string description() const
    {
        return name;
    }
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop)
            & BOOST_SERIALIZATION_NVP(name);
    }
public:
    bus_stop_destination(){}
    bus_stop_destination(
        const gps_position & _lat, const gps_position & _long, const std::string & _name
    ) :
        bus_stop(_lat, _long), name(_name)
    {
    }
};

/////////////////////////////////////////////////////////////
// a bus route is a collection of bus stops
//
// illustrates serialization of STL collection templates.
//
// illustrates serialzation of polymorphic pointer (bus stop *);
//
// illustrates storage and recovery of shared pointers is correct
// and efficient.  That is objects pointed to by more than one
// pointer are stored only once.  In such cases only one such
// object is restored and pointers are restored to point to it
//
class bus_route
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
    typedef bus_stop * bus_stop_pointer;
    std::list stops;
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        // in this program, these classes are never serialized directly but rather
        // through a pointer to the base class bus_stop. So we need a way to be
        // sure that the archive contains information about these derived classes.
        //ar.template register_type();
        ar.register_type(static_cast(NULL));
        //ar.template register_type();
        ar.register_type(static_cast(NULL));
        // serialization of stl collections is already defined
        // in the header
        ar & BOOST_SERIALIZATION_NVP(stops);
    }
public:
    bus_route(){}
    void append(bus_stop *_bs)
    {
        stops.insert(stops.end(), _bs);
    }
};
std::ostream & operator<<(std::ostream &os, const bus_route &br)
{
    std::list::const_iterator it;
    // note: we're displaying the pointer to permit verification
    // that duplicated pointers are properly restored.
    for(it = br.stops.begin(); it != br.stops.end(); it++){
        os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
    }
    return os;
}

/////////////////////////////////////////////////////////////
// a bus schedule is a collection of routes each with a starting time
//
// Illustrates serialization of STL objects(pair) in a non-intrusive way.
// See definition of operator<<  >(ar, pair)
// 
// illustrates nesting of serializable classes
//
// illustrates use of version number to automatically grandfather older
// versions of the same class.

class bus_schedule
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_schedule &bs);
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(schedule);
    }
    // note: this structure was made public. because the friend declarations
    // didn't seem to work as expected.
public:
    struct trip_info
    {
        template
        void serialize(Archive &ar, const unsigned int file_version)
        {
            // in versions 2 or later
            if(file_version >= 2)
                // read the drivers name
                ar & BOOST_SERIALIZATION_NVP(driver);
            // all versions have the follwing info
            ar  & BOOST_SERIALIZATION_NVP(hour)
                & BOOST_SERIALIZATION_NVP(minute);
        }

        // starting time
        int hour;
        int minute;
        // only after system shipped was the driver's name added to the class
        std::string driver;

        trip_info(){}
        trip_info(int _h, int _m, const std::string &_d) :
            hour(_h), minute(_m), driver(_d)
        {}
        ~trip_info(){
        }
    };
//  friend std::ostream & operator<<(std::ostream &os, const trip_info &ti);
private:
    std::list > schedule;
public:
    void append(const std::string &_d, int _h, int _m, bus_route *_br)
    {
        schedule.insert(schedule.end(), std::make_pair(trip_info(_h, _m, _d), _br));
    }
    bus_schedule(){}
};

BOOST_CLASS_VERSION(bus_schedule::trip_info, 3)
BOOST_CLASS_VERSION(bus_schedule, 2)

std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti)
{
    return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
}
std::ostream & operator<<(std::ostream &os, const bus_schedule &bs)
{
    std::list >::const_iterator it;
    for(it = bs.schedule.begin(); it != bs.schedule.end(); it++){
        os << it->first << *(it->second);
    }
    return os;
}

#endif // BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP

#2


demo_gps.hpp
#ifndef BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP
#define BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
//
// demo_gps.hpp
//
// (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

// This illustration models the bus system of a small city.
// This includes, multiple bus stops,  bus routes and schedules.
// There are different kinds of stops.  Bus stops in general will
// will appear on multiple routes.  A schedule will include
// muliple trips on the same route.

/////////////////////////////////////////////////////////////
// gps coordinate
//
// llustrates serialization for a simple type
//
class gps_position
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);

    int degrees;
    int minutes;
    float seconds;

    template
    void serialize(Archive & ar, const unsigned int /* file_version */){
        ar  & BOOST_SERIALIZATION_NVP(degrees)
            & BOOST_SERIALIZATION_NVP(minutes)
            & BOOST_SERIALIZATION_NVP(seconds);
    }

public:
    // every serializable class needs a constructor
    gps_position(){};
    gps_position(int _d, int _m, float _s) : 
        degrees(_d), minutes(_m), seconds(_s)
    {}
};

std::ostream & operator<<(std::ostream &os, const gps_position &gp)
{
    return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
}

/////////////////////////////////////////////////////////////
// One bus stop
//
// illustrates serialization of serializable members
//

class bus_stop
{
    friend class boost::serialization::access;
    virtual std::string description() const = 0;
    gps_position latitude;
    gps_position longitude;

    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(latitude);
        ar & BOOST_SERIALIZATION_NVP(longitude);
    }

protected:
    bus_stop(const gps_position & _lat, const gps_position & _long) :
        latitude(_lat), longitude(_long)
    {}
public:
    bus_stop(){}
    friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
    virtual ~bus_stop(){}
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(bus_stop)

std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
{
    return os << bs.latitude << bs.longitude << ' ' << bs.description();
}

/////////////////////////////////////////////////////////////
// Several kinds of bus stops
//
// illustrates serialization of derived types
//
class bus_stop_corner : public bus_stop
{
    friend class boost::serialization::access;
    std::string street1;
    std::string street2;
    virtual std::string description() const
    {
        return street1 + " and " + street2;
    }
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        // save/load base class information
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop);
        ar & BOOST_SERIALIZATION_NVP(street1);
        ar & BOOST_SERIALIZATION_NVP(street2);
    }
public:
    bus_stop_corner(){}
    bus_stop_corner(const gps_position & _lat, const gps_position & _long,
        const std::string & _s1, const std::string & _s2
    ) :
        bus_stop(_lat, _long), street1(_s1), street2(_s2)
    {
    }
};

class bus_stop_destination : public bus_stop
{
    friend class boost::serialization::access;
    std::string name;
    virtual std::string description() const
    {
        return name;
    }
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop)
            & BOOST_SERIALIZATION_NVP(name);
    }
public:
    bus_stop_destination(){}
    bus_stop_destination(
        const gps_position & _lat, const gps_position & _long, const std::string & _name
    ) :
        bus_stop(_lat, _long), name(_name)
    {
    }
};

/////////////////////////////////////////////////////////////
// a bus route is a collection of bus stops
//
// illustrates serialization of STL collection templates.
//
// illustrates serialzation of polymorphic pointer (bus stop *);
//
// illustrates storage and recovery of shared pointers is correct
// and efficient.  That is objects pointed to by more than one
// pointer are stored only once.  In such cases only one such
// object is restored and pointers are restored to point to it
//
class bus_route
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
    typedef bus_stop * bus_stop_pointer;
    std::list stops;
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        // in this program, these classes are never serialized directly but rather
        // through a pointer to the base class bus_stop. So we need a way to be
        // sure that the archive contains information about these derived classes.
        //ar.template register_type();
        ar.register_type(static_cast(NULL));
        //ar.template register_type();
        ar.register_type(static_cast(NULL));
        // serialization of stl collections is already defined
        // in the header
        ar & BOOST_SERIALIZATION_NVP(stops);
    }
public:
    bus_route(){}
    void append(bus_stop *_bs)
    {
        stops.insert(stops.end(), _bs);
    }
};
std::ostream & operator<<(std::ostream &os, const bus_route &br)
{
    std::list::const_iterator it;
    // note: we're displaying the pointer to permit verification
    // that duplicated pointers are properly restored.
    for(it = br.stops.begin(); it != br.stops.end(); it++){
        os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
    }
    return os;
}

/////////////////////////////////////////////////////////////
// a bus schedule is a collection of routes each with a starting time
//
// Illustrates serialization of STL objects(pair) in a non-intrusive way.
// See definition of operator<<  >(ar, pair)
// 
// illustrates nesting of serializable classes
//
// illustrates use of version number to automatically grandfather older
// versions of the same class.

class bus_schedule
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_schedule &bs);
    template
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(schedule);
    }
    // note: this structure was made public. because the friend declarations
    // didn't seem to work as expected.
public:
    struct trip_info
    {
        template
        void serialize(Archive &ar, const unsigned int file_version)
        {
            // in versions 2 or later
            if(file_version >= 2)
                // read the drivers name
                ar & BOOST_SERIALIZATION_NVP(driver);
            // all versions have the follwing info
            ar  & BOOST_SERIALIZATION_NVP(hour)
                & BOOST_SERIALIZATION_NVP(minute);
        }

        // starting time
        int hour;
        int minute;
        // only after system shipped was the driver's name added to the class
        std::string driver;

        trip_info(){}
        trip_info(int _h, int _m, const std::string &_d) :
            hour(_h), minute(_m), driver(_d)
        {}
        ~trip_info(){
        }
    };
//  friend std::ostream & operator<<(std::ostream &os, const trip_info &ti);
private:
    std::list > schedule;
public:
    void append(const std::string &_d, int _h, int _m, bus_route *_br)
    {
        schedule.insert(schedule.end(), std::make_pair(trip_info(_h, _m, _d), _br));
    }
    bus_schedule(){}
};

BOOST_CLASS_VERSION(bus_schedule::trip_info, 3)
BOOST_CLASS_VERSION(bus_schedule, 2)

std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti)
{
    return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
}
std::ostream & operator<<(std::ostream &os, const bus_schedule &bs)
{
    std::list >::const_iterator it;
    for(it = bs.schedule.begin(); it != bs.schedule.end(); it++){
        os << it->first << *(it->second);
    }
    return os;
}

#endif // BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP

推荐阅读
  • 在ROS系统中,参数读写一般通过xml或者yaml格式的文件,其中yaml用得比较多。这是一种可读性高,轻量级的标记语言,简单好用。对于yaml文件,ros中用的较早版本的yaml- ... [详细]
  • 下载器,就是一种网络工具,从网络中接收自己想要的数据。下载器是一个网络客户端。它的下载流程无非就是客户端连接服务器端,然后发送资源下载请求 ... [详细]
  • 水题。。main.cppPATA1121CreatedbyPhoenixon2018224.Copyright©2018年Phoenix.Allrightsreserve ... [详细]
  • *Copyright(c)2016,烟台大学计算机与控制工程学院Allrightsreserved.文件名称:字符串加密.cpp作者:彭友程完成日期&# ... [详细]
  • 1、概念共享内存:共享内存是进程间通信中最简单的方式之一。共享内存允许两个或更多进程访问同一块内存,就如同malloc()函数向不同进程返回了指向同一个 ... [详细]
  • 对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。Input输入数据包括多个测试实例,每个实例由一行长度 ... [详细]
  • [二分图]JZOJ 4612 游戏
    DescriptionInputOutputSampleInput44#****#****#*xxx#SampleOutput5DataConstraint分析非常眼熟࿰ ... [详细]
  • [字符编码]Numeric Character Reference和HTML Entities(一)
    你是否在dreamweaver里编辑网页的时候看到&#x3A3;这样的东西,你曾使用过&nbsp;这样的玩意吧,或者你在调试webservice的时候看到返回xml字符串中现 ... [详细]
  • 这篇文章将为大家详细讲解有关如何使用C语言strcmp函数,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一 ... [详细]
  • 使用临时文件tmpnam该函数的功能是产生一个唯一的文件名系统回味该文件分配一块内存来保存临时变量例如下面的代码#includeintmain(){charnam ... [详细]
  • 这篇文章主要讲解了“GradeBook类怎么定义”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Grad ... [详细]
  • 接上文http:blog.itpub.net29254281viewspace-1318239领导让开发同学鼓捣一个可配置化的后台.又回到了原来的问题如果要灵活,很多参数要 ... [详细]
  • socket8 [命名管道]
    ::命名管道不但能实现同一台机器上两个进程通信,还能在网络中不同机器上的两个进程之间的通信机制。与邮槽不同,命名管道是采用基于连接并且可靠的传输方式,所以命名管道传输数据只能一对一 ... [详细]
  • wyh2000andastringproblemTimeLimit:20001000MS(JavaOthers)MemoryLimit:13107265 ... [详细]
  • 实验七、绕过ASLR 第二部分
    7.1实验环境VM配置:Ubuntu12.04(x86)7.2实验原理什么是爆破?使用爆破技巧,来绕过共享库地址随机化。7.3实验过程7. ... [详细]
author-avatar
蕊蕊宝宝妈妈_534
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有