xml 使用 boost 解析
xml parsing using boost
我正在使用 BOOST 库解析以下 xml 文件-
<da>
<m_day Type="int">15</m_day>
<m_month Type="int">8</m_month>
<m_year Type="int">1947</m_year>
</da>
我的 cpp 代码是:
#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;
}
我遇到以下错误:
抛出 'boost::archive::archive_exception' 的实例后调用终止
what(): 输入流错误
已中止(核心已转储)
对于没有属性的普通 xml(如下所述)上面的代码工作正常
<da>
<m_day>15</m_day>
<m_month>8</m_month>
<m_year>1947</m_year>
</da>
但是对于之前的xml文件,代码有什么问题吗?能否请您告诉我是否有可能 boost.i 已经搜索了这么多以找到答案但没有得到任何答案。
在此先感谢!!!
Boost Serialization 不支持写入 XML 属性,显然也不支持读取它们,甚至忽略它们。其中一位作者在这里写道:http://boost.2283326.n4.nabble.com/serialization-How-to-serialize-as-an-XML-attribute-td2557918.html
您可以在加载之前预处理 XML,使用另一个 XML 库,如 Expat 或其他。
Boost 序列化 不是 一个 XML 库。
Boost Archive xml_[io]archive 也不是 XML 库。
见鬼,连 Boost 都没有 属性 Tree 是一个 XML 库。
简而言之:Boost 不包含 XML 库。
稍微长一些:您可以使用Boost 属性树来解析您的XML。它在引擎盖下使用 RapidXML 的衍生物,您可以 read/write 公平地 通用 XML 使用 Boost 属性 树的文档。
实际上,属性 Tree 界面非常具体,经常会导致混淆。没有太多的控制权。
如果您关心 XML 支持(命名空间?空格?排序?PCDATA?处理指令?XPath?编码?...)请考虑使用 XML 库(What XML parser should I use in C++?)
奖励:使用 Boost 属性 Tree
的示例
这是使用 Boost 属性 Tree
的方法
#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";
}
版画
day: 15 month: 8 year: 1947
我正在使用 BOOST 库解析以下 xml 文件-
<da>
<m_day Type="int">15</m_day>
<m_month Type="int">8</m_month>
<m_year Type="int">1947</m_year>
</da>
我的 cpp 代码是:
#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;
}
我遇到以下错误:
抛出 'boost::archive::archive_exception' 的实例后调用终止 what(): 输入流错误 已中止(核心已转储)
对于没有属性的普通 xml(如下所述)上面的代码工作正常
<da>
<m_day>15</m_day>
<m_month>8</m_month>
<m_year>1947</m_year>
</da>
但是对于之前的xml文件,代码有什么问题吗?能否请您告诉我是否有可能 boost.i 已经搜索了这么多以找到答案但没有得到任何答案。 在此先感谢!!!
Boost Serialization 不支持写入 XML 属性,显然也不支持读取它们,甚至忽略它们。其中一位作者在这里写道:http://boost.2283326.n4.nabble.com/serialization-How-to-serialize-as-an-XML-attribute-td2557918.html
您可以在加载之前预处理 XML,使用另一个 XML 库,如 Expat 或其他。
Boost 序列化 不是 一个 XML 库。
Boost Archive xml_[io]archive 也不是 XML 库。
见鬼,连 Boost 都没有 属性 Tree 是一个 XML 库。
简而言之:Boost 不包含 XML 库。
稍微长一些:您可以使用Boost 属性树来解析您的XML。它在引擎盖下使用 RapidXML 的衍生物,您可以 read/write 公平地 通用 XML 使用 Boost 属性 树的文档。
实际上,属性 Tree 界面非常具体,经常会导致混淆。没有太多的控制权。
如果您关心 XML 支持(命名空间?空格?排序?PCDATA?处理指令?XPath?编码?...)请考虑使用 XML 库(What XML parser should I use in C++?)
奖励:使用 Boost 属性 Tree
的示例这是使用 Boost 属性 Tree
的方法#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";
}
版画
day: 15 month: 8 year: 1947