[Boost-users] Bug in XML serialization!!!

184 views
Skip to first unread message

Piyush Kapadia

unread,
Oct 20, 2005, 10:26:05 PM10/20/05
to boost...@lists.boost.org

Following code results in “unrecognized XML syntax” exception while writing to XML file.

However if I change xml_oarchive to text_oarchive or binary_oarchive, it works fine, so I assume it’s a bug in xml_oarchive ??? Does any one know how to fix this for xml_oarchive ???

 

 

#include <iostream>

#include <fstream>

#include <boost/archive/text_iarchive.hpp>

#include <boost/archive/text_oarchive.hpp>

#include <boost/archive/xml_iarchive.hpp>

#include <boost/archive/xml_oarchive.hpp>

#include <boost/archive/binary_iarchive.hpp>

#include <boost/archive/binary_oarchive.hpp>

#include <boost/serialization/vector.hpp>

#include <boost/serialization/map.hpp>

#include <vector>

#include <map>

#include <boost/archive/archive_exception.hpp>

#include <boost/archive/basic_xml_archive.hpp>

 

 

class base {

 

protected :

   std::vector<int> vec;

 

protected :

   friend class boost::serialization::access;

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

        ar & BOOST_SERIALIZATION_NVP(vec);

    }

public :

      void add(int i) { vec.push_back(i); }

      int get(int idx) { return vec[idx]; }

};

class derived : public base {

   friend class boost::serialization::access;

   std::map<std::string, int> hmap;

 

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

            ar & BOOST_SERIALIZATION_NVP(boost::serialization::base_object<base>(*this));

        ar & BOOST_SERIALIZATION_NVP(hmap);

    }

public :

      void add(std::string str, int i) { hmap[str] = i; base::add(i);   }

      int get(std::string str) { return hmap[str];    }

};

 

int main(int argc, char* argv[])

{

      try {

            derived A;

            A.add("First", 12);

            A.add("Second", 34);

const derived AA = A;

 

            std::ofstream ofs("Test.xml");

            assert(ofs.good());

            boost::archive::xml_oarchive oa(ofs);    

            oa << BOOST_SERIALIZATION_NVP(AA);

            ofs.close();

 

derived B;

            std::ifstream ifs("Test.xml");

            assert(ifs.good());

            boost::archive::xml_iarchive ia(ifs);

            ia >> BOOST_SERIALIZATION_NVP(B);

            ifs.close();

           

      }          

      catch (boost::archive::xml_archive_exception ex)

      {

            std::cerr << " X " << ex.what() << std::endl;

      }

      catch (boost::archive::archive_exception  ex)

      {

            std::cerr << " A " << ex.what() << std::endl;

      }

      catch (std::exception  ex)

      {

            std::cerr << " E " <<  ex.what() << std::endl;

      }

      catch (...)

      {

            std::cerr << "Unknown Exception " << std::endl;

      }

      return 0;

}

 

 

Richard Jennings

unread,
Oct 21, 2005, 4:22:30 AM10/21/05
to boost...@lists.boost.org
I think it might be the function I've cut out below. Try using
boost:serialization::make_nvp
in place of the macro:

ar & boost::serialization::make_nvp("base",
boost::serialization::base_object<base>(*this));

The macro uses the parameter as the name for the XML element. Because
it's a templated type
then the angle brackets are probably included in the name, which would be
invalid XML
syntax.

Richard

On Fri, 21 Oct 2005 03:26:05 +0100, Piyush Kapadia
<piyush....@gmail.com> wrote:

> Following code results in "unrecognized XML syntax" exception while
> writing
> to XML file.
[snip]
> void serialize(Archive & ar, const unsigned int version)
>
> {
>
> ar &
> BOOST_SERIALIZATION_NVP(boost::serialization::base_object<base>(*this));
>
> ar & BOOST_SERIALIZATION_NVP(hmap);
>
> }
>

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

Piyush Kapadia

unread,
Oct 21, 2005, 9:35:16 AM10/21/05
to boost...@lists.boost.org, piyush....@gmail.com
Thanks for very prompt response on the subject.
It worked, and it shows as following tag -
 
<AA class_id="0" tracking_level="0" version="0">
 <base class_id="1" tracking_level="0" version="0">
  <vec>
   <count>2</count>
   <item>12</item>
   <item>34</item>
  </vec>
 </base>
 <hmap class_id="3" tracking_level="0" version="0">
  <count>2</count>
  <item class_id="4" tracking_level="0" version="0">
   <first>First</first>
   <second>12</second>
  </item>
  <item>
   <first>Second</first>
   <second>34</second>
  </item>
 </hmap>
</AA>

However two issues persists related to XML -
1. When xml out put file opened on Internet Explorer it results in following legitimate error -

The following tags were not closed: boost_serialization. Error processing resource 'file:///C:/Documents and Settings/piyus...

Some how it failes to write </boost_serialization> at the end of the file, I don't know why  ???

I have included test code and I am using VC 7.

2. Only Base class is using correct generic name as it is supplied via make_nvp function, however all other tags are the name of variables for example enclosing derived class is AA instead of derived as tag, how can we change that to show as derived(just like base) instead of AA or hmap(should be std::map) ???
SerialTest.cpp

Robert Ramey

unread,
Oct 21, 2005, 11:37:12 AM10/21/05
to boost...@lists.boost.org
 
"Piyush Kapadia" <piyush....@gmail.com> wrote in message
 
two issues persists related to XML -
1. When xml out put file opened on Internet Explorer it results in following legitimate error -

The following tags were not closed: boost_serialization. Error processing resource 'file:///C:/Documents and Settings/piyus...

Some how it failes to write </boost_serialization> at the end of the file, I don't know why  ???

I have included test code and I am using VC 7.

This is usually caused by code like the following:

{

    ..ostream os("file");

    ..oarchive oa(os);

    oa << ....

    os.close();  // close or destroy output stream

   // destructor on oa called after stream closed

}

 

Robert Ramey
Reply all
Reply to author
Forward
0 new messages