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@rrsd.com> wrote:

From: Robert Ramey <ramey@rrsd.com>
Subject: Re: [Boost-users] XML Serialization: Putting Stylesheet tag inside XMLfile
To: boost-users@lists.boost.org
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