我编写了一个XML解析器,用于读取XML文件并将其转换为我的" Position"的对象。类。它工作正常。请看下面的代码。
XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="15">
<position class_id="0" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3</Seconds>
<data class_id="1" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3</Seconds>
</data>
</position>
</boost_serialization>
职位分类:
#ifndef XMLMANAGER_POSITION_H
#define XMLMANAGER_POSITION_H
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
class Data
{
public:
int Degrees;
int Minutes;
float Seconds;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
}
};
class Position
{
public:
// every serializable class needs a constructor
Position() {
Degrees = 0;
Minutes = 0;
Seconds = 0;
};
Position(int degrees, int minutes, float seconds){
Degrees = degrees;
Minutes = minutes;
Seconds = seconds;
};
int Degrees;
int Minutes;
float Seconds;
Data data;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
ar & BOOST_SERIALIZATION_NVP(data);
}
};
#endif //XMLMANAGER_POSITION_H
主要功能:
// Load an Object from XML
std::ifstream ifs("/tests/r.karimi/XmlManager/data/Output.xml");
boost::archive::xml_iarchive ixa(ifs);
Position newPosition;
try
{
ixa >> BOOST_SERIALIZATION_NVP(newPosition);
}
catch (std::exception const &ex)
{
std::cerr << ex.what() << std::endl;
}
std::cout << "Degree " << newPosition.Degrees <<
" Minutes " << newPosition.Minutes <<
" Seconds " << newPosition.Seconds << std::endl;
std::cout << "Degree " << newPosition.data.Degrees <<
" Minutes " << newPosition.data.Minutes <<
" Seconds " << newPosition.data.Seconds << std::endl;
现在,我想传递以下XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="15">
<position class_id="0" tracking_level="0" version="0">
<Degrees>0</Degrees>
<Minutes>0</Minutes>
<Seconds>0.000000000e+00</Seconds>
<data class_id="1" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3</Seconds>
</data>
<data class_id="1" tracking_level="0" version="0">
<Degrees>4</Degrees>
<Minutes>5</Minutes>
<Seconds>6</Seconds>
</data>
</position>
</boost_serialization>
我该怎么办?我尝试了不同的解决方案,例如获取数据&#34;数据&#34;课程&#34;职位&#34;类,但它不起作用。我更喜欢将Boost库用于我的项目。 (除非不可能!!!)
答案 0 :(得分:1)
Boost 不拥有XML库。
Boost Serialization不会读取一般的XML(类似)文档。它只读取XML档案 。这意味着,您无法控制XML。
所有人都说,你可以简单地用另一个Data成员扩展该类,并且意外地导致一个非常相似的XML存档。也许这对你来说已经足够了:
<强> Live On Coliru 强>
#include <boost/archive/xml_oarchive.hpp>
class Data
{
public:
int Degrees;
int Minutes;
float Seconds;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
}
};
class Position
{
public:
// every serializable class needs a constructor
Position() {
Degrees = 0;
Minutes = 0;
Seconds = 0;
};
Position(int degrees, int minutes, float seconds){
Degrees = degrees;
Minutes = minutes;
Seconds = seconds;
};
int Degrees;
int Minutes;
float Seconds;
Data data;
Data data2;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
ar & boost::serialization::make_nvp("data", data);
ar & boost::serialization::make_nvp("data", data2);
}
};
#include <fstream>
int main() {
Position position;
position.Degrees = 1;
position.Minutes = 2;
position.Seconds = 3;
position.data = {1,2,3};
position.data2 = {4,5,6};
{
std::ofstream ofs("output.xml");
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(position);
}
}
写入
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<position class_id="0" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3.000000000e+00</Seconds>
<data class_id="1" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3.000000000e+00</Seconds>
</data>
<data>
<Degrees>4</Degrees>
<Minutes>5</Minutes>
<Seconds>6.000000000e+00</Seconds>
</data>
</position>
</boost_serialization>
如果你想要控制XML,那就更好了。让图书馆按照您的意愿行事:
<强> Live On Coliru 强>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/vector.hpp>
class Data
{
public:
int Degrees;
int Minutes;
float Seconds;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
}
};
class Position
{
public:
// every serializable class needs a constructor
Position() {
Degrees = 0;
Minutes = 0;
Seconds = 0;
};
Position(int degrees, int minutes, float seconds){
Degrees = degrees;
Minutes = minutes;
Seconds = seconds;
};
int Degrees;
int Minutes;
float Seconds;
std::vector<Data> data;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/){
ar & BOOST_SERIALIZATION_NVP(Degrees);
ar & BOOST_SERIALIZATION_NVP(Minutes);
ar & BOOST_SERIALIZATION_NVP(Seconds);
ar & boost::serialization::make_nvp("data", data);
}
};
#include <fstream>
int main() {
{
Position position;
position.Degrees = 1;
position.Minutes = 2;
position.Seconds = 3;
position.data = { {1,2,3}, {4,5,6 } };
std::ofstream ofs("output.xml");
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(position);
}
{
std::ifstream ifs("output.xml");
boost::archive::xml_iarchive ia(ifs);
Position position;
ia >> BOOST_SERIALIZATION_NVP(position);
}
}
在这种情况下,XML 发生看起来像:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<position class_id="0" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3.000000000e+00</Seconds>
<data class_id="1" tracking_level="0" version="0">
<count>2</count>
<item_version>0</item_version>
<item class_id="2" tracking_level="0" version="0">
<Degrees>1</Degrees>
<Minutes>2</Minutes>
<Seconds>3.000000000e+00</Seconds>
</item>
<item>
<Degrees>4</Degrees>
<Minutes>5</Minutes>
<Seconds>6.000000000e+00</Seconds>
</item>
</data>
</position>
</boost_serialization>