Boost logo

Boost Users :

Subject: Re: [Boost-users] XML Serialization: Putting Stylesheet tag inside XMLfile
From: girish hilage (girish_hilage_at_[hidden])
Date: 2010-02-19 06:19:17


Hi,

   I am able to insert the stylesheet tag from inside a constructor of my new derived class.
   Please find below the whole program.

   But, I am still getting an error :
   terminate called after throwing an instance of 'std::bad_cast'
     what():  std::bad_cast
   Aborted (core dumped)

   on the statement :
   xa << BOOST_SERIALIZATION_NVP(e);
   in the code below.

   The statements :
   xa.putTag ("TESTING_XMLCLASS");
   xa << BOOST_SERIALIZATION_NVP(tmp);
   oa << BOOST_SERIALIZATION_NVP(e);
   work as expected.

   Can you please let me know what could be the reason for this?

Regards,
Girish

# include <iostream>
#include <fstream>
#include <string>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/version.hpp>
#include <boost/archive/xml_oarchive.hpp>

using namespace std;
using namespace boost::archive;

struct EMPLOYEE
{
    int id;
    string name;

    friend class boost::serialization::access;
    template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */)
    {
        ar << BOOST_SERIALIZATION_NVP(id);
        ar << BOOST_SERIALIZATION_NVP(name);
    }
};

class XMLArchive : public xml_oarchive_impl<xml_oarchive>
{
    public :
    XMLArchive (ostream &os) : xml_oarchive_impl<xml_oarchive>(os, 0)
    {
        os.seekp (58);

        string styletag = "?xml-stylesheet type=\"text/xsl\" href=\"XSLTFile.xsl\"?>\n";
        string line1 = "<!DOCTYPE boost_serialization>\n";
        string line2 = "<boost_serialization signature=\"serialization::archive\" version=\"5\">\n";
        string buffer = styletag + line1 + line2;
        os << buffer;
        os.flush ();
    }

    void putTag (string tagname)
    {
        string tag_str = "<";
        tag_str = tag_str + tagname + ">\n";
        os << tag_str;
    }

    ~XMLArchive () {}
};

int main (void)
{
    ofstream ofs ("XMLresult.xml");
    XMLArchive xa(ofs);
//  boost::archive::xml_oarchive oa(ofs);

    xa.putTag ("TESTING_XMLCLASS");

    string tmp = "TMPTMP";
    xa << BOOST_SERIALIZATION_NVP(tmp);

    struct EMPLOYEE e;
    e.id = 101;
    e.name = "James Bond";

    xa << BOOST_SERIALIZATION_NVP(e);
//  oa << BOOST_SERIALIZATION_NVP(e);

    return 0;
}

--- On Thu, 2/18/10, Robert Ramey <ramey_at_[hidden]> wrote:

From: Robert Ramey <ramey_at_[hidden]>
Subject: Re: [Boost-users] XML Serialization: Putting Stylesheet tag inside XMLfile
To: boost-users_at_[hidden]
Date: Thursday, February 18, 2010, 5:27 PM

>    File XSLTFile.xml contain stylesheet info.
>    * So, how is it possible to put a stylesheet related statement
> just after the 1st statement?
>    * Another query is, how to change the the tag name
> "boost_serialization" to something else?

You should be able to derive your new class from xml_archive_impl...

There you can the initialization / windup functions.  There is/was
and outdated example of how to do this in the documentation
using portable binary archive as an example.  I don't know if
it's still in there.

Robert Ramey



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net