(我在windows下并使用visual studio 2015.)我收到问题末尾的xml,采用单行格式,但我用notepad++
打印了非常简单的XML(带有换行符的XML)为了方便地使用。
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
收到“无错误”,其中
boost::archive::xml_iarchive ia(file);
使用boost::archive::archive_exception
触发{m_buffer=0x00e8f504 "unrecognized XML syntax" code=other_exception }
。 interestRateCurve类定义如下:
#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(使用boost代码段生成)不会触发相同的错误:
<?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与我正在战斗的形式相同的形式......
答案 0 :(得分:1)
问题是Boost没有XML库。
您似乎认为Boost Serialization可以读取XML。实际上,它只能读取XML文档的一个子集,即Boost Serialization Archives,它们是一种特定的格式。如果您没有这种格式,则无法使用Boost序列化来阅读它。
相反,只需使用XML库。我建议使用PugiXML或libxml2:
答案 1 :(得分:0)
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);
触发“”输入流错误“code = input_stream_error”。我不知道是不是因为interestRateCurve类的设计不好因此无法完成这项工作,或者我的答案根本不是正确答案,导致“坏”bst::archive::xml_iarchive
? (bst::archive::xml_iarchive
在调试时看起来很正常。)