2

I am parsing below xml file using BOOST library-

<da>
        <m_day Type="int">15</m_day>
        <m_month Type="int">8</m_month>
        <m_year Type="int">1947</m_year>
</da>

My cpp code is :

    #include <boost/archive/xml_oarchive.hpp> 
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream> 
    #include <fstream> 

    typedef struct date { 
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
        date( int d,  int m,  int y) : m_day(d), m_month(m), m_year(y) 
        {}
        date() : m_day(1), m_month(1), m_year(2000) 
        {}
        friend std::ostream& operator << (std::ostream& out, date& d) 
        {
            out << "day: " << d.m_day 
                  << " month: " << d.m_month
        << " year: " << d.m_year;
            return out;
        }
        template<class Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
        archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    } date;

    BOOST_CLASS_IMPLEMENTATION(date, boost::serialization::object_serializable);//object_serializable);

    unsigned int flags =   boost::archive::no_header;


    int main()
    {

     std::ifstream file1("archive.xml");
     boost::archive::xml_iarchive ia(file1,flags);
     date dr;
     ia >> BOOST_SERIALIZATION_NVP(dr);


     std::ofstream file("archive2.xml");
      boost::archive::xml_oarchive oa(file,flags);
    //  date da(15, 8, 1947);
      oa & BOOST_SERIALIZATION_NVP(dr);


    return 0;
    }

I am getting below error :

terminate called after throwing an instance of 'boost::archive::archive_exception' what(): input stream error Aborted (core dumped)

for Normal xml without attributes(as mentioned below) the above code is working fine

<da>
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
</da>

But for previous xml file ,Is there any problem in code ? could you please let me know if it is possible with boost.i have searched so much to find the answer but failed to get any. Thanks in Advance !!!

1
  • This can't work because your document is not a serialization archive. The format is not free for Boost Serialization Commented Jan 20, 2015 at 9:12

2 Answers 2

3

Boost Serialization is not an XML library.

Boost Archive xml_[io]archive isn't an XML library either.

Heck, not even Boost Property Tree is an XML library.

In short: Boost Does Not Contain An XML Library.


Slightly longer: you can use Boost Property Tree to parse your XML. It's using a derivative of RapidXML under the hood and you can read/write fairly generic XML documents using Boost Property Tree.

In reality, the Property Tree interface is quite specific and often leads to confusion. There's not a lot of control.

If you care about your XML support (namespaces? whitespace? ordering? PCDATA? Processing instructions? XPath? Encodings? ...) please consider using an XML library (What XML parser should I use in C++?)

Bonus: sample with Boost Property Tree

Here's how to do it with Boost Property Tree

Live On Coliru

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

typedef struct date { 
    unsigned int m_day;
    unsigned int m_month;
    unsigned int m_year;
    date( int d=1,  int m=1,  int y=2000) : m_day(d), m_month(m), m_year(y) {}

    friend std::ostream& operator << (std::ostream& out, date& d) {
        return out << "day: " << d.m_day << " month: " << d.m_month << " year: " << d.m_year;
    }

    void load(boost::property_tree::ptree const& pt)
    {
        m_day = pt.get("da.m_day", 1);
        m_month = pt.get("da.m_month", 1);
        m_year = pt.get("da.m_year", 2000);
    }
} date;

#include <sstream>

int main() {
    std::istringstream iss("<da>\n"
                "<m_day Type=\"int\">15</m_day>\n"
                "<m_month Type=\"int\">8</m_month>\n"
                "<m_year Type=\"int\">1947</m_year>\n"
                "</da>\n");

    boost::property_tree::ptree pt;
    read_xml(iss, pt);

    date d;
    d.load(pt);

    std::cout << d << "\n";
}

Prints

day: 15 month: 8 year: 1947
Sign up to request clarification or add additional context in comments.

6 Comments

Added a code sample based on Boost Property Tree (don't want to be only cynical :))
Actually my requirement is deserialize the xml and put it into strruc on on side and send it to other side. On the otherside I will serialize the data from this struct back and form an XML. That is the reason i am using Boost Serialization. Could you please provide solution with Boost Serialization instead of Boost property tree. Thanks in advacne
@harshini You missed the point. You cannot. Not unless you can change the XML (or are willing to implement a whole new xml archive for the purpose). That's a completely different question (it's completely reversed) and you should ask it as a new question, in my opinion.
@harshini Cheers. No problem. The answer sadly doesn't change.
@harshini I'm confused now. Repeating the question doesn't change the answer. It's not that I don't want to. It's just not what the library does.
|
1

Boost Serialization does not support writing XML attributes, and apparently doesn't support reading them either, or even ignoring them. One of the authors wrote about this here: http://boost.2283326.n4.nabble.com/serialization-How-to-serialize-as-an-XML-attribute-td2557918.html

You could pre-process the XML before loading it, using another XML library like Expat or whatever.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.