Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize to XML using boost::serialization

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?

like image 861
navigator Avatar asked Aug 11 '10 10:08

navigator


People also ask

What is Boost serialization?

The library Boost. Serialization makes it possible to convert objects in a C++ program to a sequence of bytes that can be saved and loaded to restore the objects. There are different data formats available to define the rules for generating sequences of bytes. All of the formats supported by Boost.

What is the correct way of using XML serialization?

As with the CreatePo method, you must first construct an XmlSerializer, passing the type of class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.

What does it mean to serialize XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

How do you serialize and deserialize an object in C# using XML?

Just mark up what you want to serialize with [XmlElement(name)] (or XmlAttribute, XmlRoot, etc) and then use the XmlSerializer. If you need really custom formating, implement IXmlSerializable.


2 Answers

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)
like image 54
stefan Avatar answered Sep 20 '22 14:09

stefan


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).

like image 29
Ivo Avatar answered Sep 21 '22 14:09

Ivo