[Boost-users] XML Serialization: Putting Stylesheet tag inside XML file

56 views
Skip to first unread message

girish hilage

unread,
Feb 18, 2010, 10:56:59 AM2/18/10
to boost...@lists.boost.org
Hi,

   Using XML serialization I have created an XML file which I want to see through a browser using a stylesheet.
   So I want to put a statement :
   <?xml-stylesheet type="text/xsl" href="XSLTFile.xsl"?>

   in between below 2 statements :
   <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
   <!DOCTYPE boost_serialization>

   like this :
   <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
   <?xml-stylesheet type="text/xsl" href="XSLTFile.xsl"?>
   <!DOCTYPE boost_serialization>
   <boost_serialization signature="serialization::archive" version="5">

   But the code for a init() function inside file boost/archive/impl/basic_xml_oarchive.ipp is as follows :

   basic_xml_oarchive<Archive>::init(){
       // xml header
       this->This()->put("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n");
       this->This()->put("<!DOCTYPE boost_serialization>\n");
       // xml document wrapper - outer root
       this->This()->put("<boost_serialization");
       write_attribute("signature", BOOST_ARCHIVE_SIGNATURE());
       write_attribute("version", BOOST_ARCHIVE_VERSION());
       this->This()->put(">\n");
   }

   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?

Regards,
Girish

Robert Ramey

unread,
Feb 18, 2010, 12:27:09 PM2/18/10
to boost...@lists.boost.org

> 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

>
> Regards,
> Girish
>
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost...@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ganeshram Iyer

unread,
Feb 18, 2010, 3:07:54 PM2/18/10
to boost-users
Hi Girish,
If you do get this problem solved, could you do me a favor and post the solution, if you can. I was entertaining some similar requirement last year for one of my projects.

Thanks in advance
Ganesh

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users



--
Ganeshram Iyer
Open Source and CAD: http://ossandcad.blogspot.com
ganesh...@yahoo.com

girish hilage

unread,
Feb 19, 2010, 6:19:17 AM2/19/10
to boost...@lists.boost.org, ra...@rrsd.com
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 <ra...@rrsd.com> wrote:

Robert Ramey

unread,
Feb 19, 2010, 11:34:42 AM2/19/10
to boost...@lists.boost.org
girish hilage wrote:
> 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?
>

I can't. The only thing that looks missing to me is:

#include <boost/serialization/string.hpp>

Robert Ramey

girish hilage

unread,
Feb 20, 2010, 12:55:54 PM2/20/10
to boost...@lists.boost.org, ra...@rrsd.com
Hi,

   I tried including <boost/serialization/string.hpp> file but still getting the crash.
   I have tried to resolve the problem by overloading << as follows :
   XMLArchive & operator << (XMLArchive &xa, const struct EMPLOYEE &e)
   {
       xa << BOOST_SERIALIZATION_NVP(e.id);
       xa << BOOST_SERIALIZATION_NVP(e.name);

       return xa;
   }

   and calling it like this :
   xa << e;

   But, though I am getting the expected result, it still gives me a WARNING As follows :

   test.cc: In function ‘int main()’:
   test.cc:94: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
   test.cc:72: note: candidate 1: XMLArchive& operator<<(XMLArchive&, const EMPLOYEE&)
   /usr/include/boost/archive/detail/interface_oarchive.hpp:63: note: candidate 2: Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = EMPLOYEE, Archive = boost::archive::xml_oarchive]

   If I call "<<()" as :
   operator <<(xa, e);
   then it gives me no WARNING.

   Also, "ltrace -C " for the crashed program is as below :

__libc_start_main(0x804a554, 1, 0xbf973d44, 0x804beb0, 0x804bea0 <unfinished ...>
std::ios_base::Init::Init()(0x804f554, 0x8967bc, 0, 0xb7f52438, 1)                               = 9
__cxa_atexit(0x804a2b8, 0x804f554, 0x804bf68, 0xb7f52438, 1)                                     = 0

__cxa_guard_acquire(0x804f558, 0x804f258, 0, 0x48bff4, 0x804beb0)                                = 1
__cxa_guard_acquire(0x804f560, 0x8049d93, 0x23b1f0, 0xb7f532f0, 0xb7f53a8c)                      = 1
boost::serialization::detail::extended_type_info_typeid_0::extended_type_info_typeid_0()(0x804f590, 0xbf973b74, 1, 1, 0) = 0xb6bf88
boost::serialization::detail::extended_type_info_typeid_0::type_register(std::type_info const&)(0x804f590, 0x804c7f4, 1, 1, 0) = 0x99d0008
__cxa_guard_release(0x804f560, 0x8049d93, 0x23b1f0, 0xb7f532f0, 0xb7f53a8c)                      = 0x804f560
__cxa_atexit(0x804b20c, 0x804f590, 0x804bf68, 0xb7f532f0, 0xb7f53a8c)                            = 0
boost::archive::detail::basic_oserializer::basic_oserializer(boost::serialization::extended_type_info const&)(0x804f584, 0x804f590, 0x804f38c, 0, 0x23b1f0) = 0x804f584
__cxa_guard_release(0x804f558, 0x804f258, 0, 0x48bff4, 0x804beb0)                                = 0x804f558
__cxa_atexit(0x804ae62, 0x804f584, 0x804bf68, 0x48bff4, 0x804beb0)                               = 0

std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(char const*, std::_Ios_Openmode)(0xbf973b1c, 0x804c0af, 48, 0x896818, 0xb7f52428) = 0xbf973ba8
boost::archive::xml_oarchive_impl<boost::archive::xml_oarchive>::xml_oarchive_impl(std::ostream&, unsigned int)(0xbf973c30, 0xbf973b1c, 0, 0x48bff4, 0) = 0

std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()(0xbf973c64, 0xbf973b1c, 0, 0x48bff4, 0) = 0xbf973c64
std::string::operator=(char const*)(0xbf973c64, 0x804c0bd, 48, 0x896818, 0xb7f52428)             = 0xbf973c64

boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_start(char const*)(0xbf973c30, 0x804c0c8, 0xff636d6e, 0xbf973c60, 0x804c0bd) = 0xbf973c30
boost::archive::detail::basic_oarchive::save_object(void const*, boost::archive::detail::basic_oserializer const&)(0xbf973c30, 0xbf973c60, 0x804f584, 0x1010101, 0x1010101 <unfinished ...>
boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::class_id_optional_type const&, int)(0xbf973c30, 0xbf973a0c, 0, 0, 0xaffd10) = 0xbf973b1c
boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::tracking_type const&, int)(0xbf973c30, 0xbf973a1f, 0, 0, 0xaffd10) = 0xbf973b1c
boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::version_type const&, int)(0xbf973c30, 0xbf973a10, 0, 0, 0xaffd10) = 0xbf973b1c

__dynamic_cast(0xbf973c30, 0x804f4f0, 0x804c9ec, 0, 0xafd170)                                    = 0
__cxa_bad_cast(0xbf973c30, 0x804f4f0, 0x804c9ec, 0, 0xafd170 <unfinished ...>
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8

terminate called after throwing an instance of 'std::bad_cast'
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 1, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 6
__gxx_personality_v0(1, 2, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 8
__gxx_personality_v0(1, 6, 0x432b2b00, 0x474e5543, 0x99d2480)                                    = 7
  what():  std::bad_cast
--- SIGABRT (Aborted) ---
+++ killed by SIGABRT +++
   So, does anyone know about any solution on,
   * either, how to remove the WARNING
   * or, how to avoid the crash?
 
   I am using boost-1.37.0-6.fc11.i586 on FC11.
Regards,
Girish

--- On Fri, 2/19/10, Robert Ramey <ra...@rrsd.com> wrote:

From: Robert Ramey <ra...@rrsd.com>
Subject: Re: [Boost-users] XML Serialization: Putting Stylesheet tag insideXMLfile
To: boost...@lists.boost.org
Reply all
Reply to author
Forward
0 new messages