boost::archive::xml_iarchive 失败并出现 xml(反序列化上下文)

boost::archive::xml_iarchive fails with an xml (deserizalisation context)

(我在 windows 下并使用 visual studio 2015。)我收到问题末尾的 xml,单行格式,但我打印得很漂亮( XML 仅带换行符)和 notepad++ 为了方便。

std::ifstream file("D:\FILES\anxmlfile.xml");
char msg[500];
strerror_s(msg,errno);
try
{
    boost::archive::xml_iarchive ia(file);
    interestRateCurve IRCurve;
    ia >> BOOST_SERIALIZATION_NVP(IRCurve);
}
catch (boost::archive::archive_exception ex)
{
    // blah
}

哪里 msg 收到 "No error" 哪里

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

{m_buffer=0x00e8f504 "unrecognized XML syntax" code=other_exception } 触发 boost::archive::archive_exception。 interestRateCurve class 定义如下:

#pragma once

#include <string>

#include "boost/archive/text_oarchive.hpp"
#include "boost/archive/text_iarchive.hpp"

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

class calendars
{
public:
    std::string calendar;
    template<class Archive> void serialize(Archive& archive, const unsigned int version)
    {
        archive & BOOST_SERIALIZATION_NVP(calendar);
    }
};

class curvepoint
{
public:
    std::string tenor;
    std::string maturitydate;
    double parrate;
    template<class Archive> void serialize(Archive& archive, const unsigned int version)
    {
        archive & BOOST_SERIALIZATION_NVP(tenor);
        archive & BOOST_SERIALIZATION_NVP(maturitydate);
        archive & BOOST_SERIALIZATION_NVP(parrate);
    }
};

class interestRateCurveDeposits
{
public:
    std::string daycountconvention;
    std::string snaptime;
    std::string spotdate;
    calendars m_calendars;
    curvepoint curvepoint;
    template<class Archive> void serialize(Archive& archive, const unsigned int version)
    {
        archive & BOOST_SERIALIZATION_NVP(daycountconvention);
        archive & BOOST_SERIALIZATION_NVP(snaptime);
        archive & BOOST_SERIALIZATION_NVP(spotdate);
        archive & BOOST_SERIALIZATION_NVP(m_calendars);
        archive & BOOST_SERIALIZATION_NVP(curvepoint);
    }
};

class interestRateCurveSwaps
{
public:
    std::string fixeddaycountconvention;
    std::string floatingdaycountconvention;
    std::string fixedpaymentfrequency;
    std::string floatingpaymentfrequency;
    std::string snaptime;
    std::string spotdate;
    calendars calendars[1];
    curvepoint curvepoint[1];
    template<class Archive> void serialize(Archive& archive, const unsigned int version)
    {
        archive & BOOST_SERIALIZATION_NVP(fixeddaycountconvention);
        archive & BOOST_SERIALIZATION_NVP(floatingdaycountconvention);
        archive & BOOST_SERIALIZATION_NVP(fixedpaymentfrequency);
        archive & BOOST_SERIALIZATION_NVP(floatingpaymentfrequency);
        archive & BOOST_SERIALIZATION_NVP(snaptime);
        archive & BOOST_SERIALIZATION_NVP(spotdate);
        archive & BOOST_SERIALIZATION_NVP(calendars);
        archive & BOOST_SERIALIZATION_NVP(curvepoint);
    }
};

class interestRateCurve
{
public:
    std::string effectiveasof;
    std::string currency;
    std::string baddayconvention;
    interestRateCurveDeposits deposits[1];
    interestRateCurveSwaps swaps[1];
    template<class Archive> void serialize(Archive& archive, const unsigned int version)
    {
        archive & BOOST_SERIALIZATION_NVP(effectiveasof);
        archive & BOOST_SERIALIZATION_NVP(currency);
        archive & BOOST_SERIALIZATION_NVP(baddayconvention);
        archive & BOOST_SERIALIZATION_NVP(deposits);
        archive & BOOST_SERIALIZATION_NVP(swaps);
    }
};

以下简短 xml(使用增强代码片段生成)不会触发相同的错误:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="15">
<d class_id="0" tracking_level="0" version="0">
    <m_day>15</m_day>
    <m_month>8</m_month>
    <m_year>1947</m_year>
</d>
</boost_serialization>

我的问题 xml 是:

<?xml version="1.0" standalone="yes" ?>
<interestRateCurve>
    <effectiveasof>2009-01-05</effectiveasof>
    <currency>USD</currency>
    <baddayconvention>M</baddayconvention>
    <deposits>
        <daycountconvention>ACT/360</daycountconvention>
        <snaptime>2009-01-02T21:00:00.000Z</snaptime>
        <spotdate>2009-01-07</spotdate>
        <calendars>
            <calendar>none</calendar>
        </calendars>
        <curvepoint>
            <tenor>1M</tenor>
            <maturitydate>2009-02-09</maturitydate>
            <parrate>0.0043</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>2M</tenor>
            <maturitydate>2009-03-09</maturitydate>
            <parrate>0.010988</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>3M</tenor>
            <maturitydate>2009-04-07</maturitydate>
            <parrate>0.014125</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>6M</tenor>
            <maturitydate>2009-07-07</maturitydate>
            <parrate>0.017525</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>9M</tenor>
            <maturitydate>2009-10-07</maturitydate>
            <parrate>0.019063</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>1Y</tenor>
            <maturitydate>2010-01-07</maturitydate>
            <parrate>0.020238</parrate>
        </curvepoint>
    </deposits>
    <swaps>
        <fixeddaycountconvention>30/360</fixeddaycountconvention>
        <floatingdaycountconvention>ACT/360</floatingdaycountconvention>
        <fixedpaymentfrequency>6M</fixedpaymentfrequency>
        <floatingpaymentfrequency>3M</floatingpaymentfrequency>
        <snaptime>2009-01-02T21:00:00.000Z</snaptime>
        <spotdate>2009-01-07</spotdate>
        <calendars>
            <calendar>none</calendar>
        </calendars>
        <curvepoint>
            <tenor>2Y</tenor>
            <maturitydate>2011-01-07</maturitydate>
            <parrate>0.016509</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>3Y</tenor>
            <maturitydate>2012-01-07</maturitydate>
            <parrate>0.019413</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>4Y</tenor>
            <maturitydate>2013-01-07</maturitydate>
            <parrate>0.021521</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>5Y</tenor>
            <maturitydate>2014-01-07</maturitydate>
            <parrate>0.023315</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>6Y</tenor>
            <maturitydate>2015-01-07</maturitydate>
            <parrate>0.024588</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>7Y</tenor>
            <maturitydate>2016-01-07</maturitydate>
            <parrate>0.025719</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>8Y</tenor>
            <maturitydate>2017-01-07</maturitydate>
            <parrate>0.02656</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>9Y</tenor>
            <maturitydate>2018-01-07</maturitydate>
            <parrate>0.02726</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>10Y</tenor>
            <maturitydate>2019-01-07</maturitydate>
            <parrate>0.028</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>12Y</tenor>
            <maturitydate>2021-01-07</maturitydate>
            <parrate>0.02893</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>15Y</tenor>
            <maturitydate>2024-01-07</maturitydate>
            <parrate>0.029989</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>20Y</tenor>
            <maturitydate>2029-01-07</maturitydate>
            <parrate>0.030029</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>25Y</tenor>
            <maturitydate>2034-01-07</maturitydate>
            <parrate>0.029639</parrate>
        </curvepoint>
        <curvepoint>
            <tenor>30Y</tenor>
            <maturitydate>2039-01-07</maturitydate>
            <parrate>0.029505</parrate>
        </curvepoint>
    </swaps>
</interestRateCurve>

我做错了什么?格式真的错了吗?为什么?如果没有,怎么办?

编辑。 附上

<interestRateCurve>
...
</interestRateCurve>

我的 xml 在

<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="15">
...
</boost_serialization>

解决了问题,但对我来说当然不是一个可行的解决方案,因为我有数百万 xml 与我正在战斗的相同形式 ...

std::ifstream file("D:\FILES\anxmlfile.xml");
char msg[500];
strerror_s(msg,errno);
try
{
    unsigned int flags = boost::archive::no_header;
    boost::archive::xml_iarchive ia(file, flags);
    interestRateCurve IRCurve;
    ia >> BOOST_SERIALIZATION_NVP(IRCurve);
}
catch (boost::archive::archive_exception ex)
{
    // blah
}

成功了,然后

ia >> BOOST_SERIALIZATION_NVP(IRCurve);

触发“"input stream error"代码=input_stream_error”。我不知道是不是因为 interestRateCurve class 设计不当导致无法完成这项工作,或者我的答案根本不是正确答案,导致 "bad" bst::archive::xml_iarchive ? (尽管 bst::archive::xml_iarchive 在调试时看起来很正常。)

问题是 Boost 没有 XML 库。

您似乎认为 Boost Serialization 可以读取 XML。事实上,它只能读取 XML 个文档的子集,即 Boost Serialization Archives,这是一种特定格式。如果您没有该格式,则无法使用 Boost 序列化来读取它。

相反,只需使用 XML 库。我建议 PugiXML 或 libxml2:

What XML parser should I use in C++?