读写xml文档

前端之家收集整理的这篇文章主要介绍了读写xml文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
HrResultType LoadFromFile()
{
using namespace boost::property_tree;
HrResultType res;
std::string file = "config/trolleyconfig.xml";
try
{
ptree pt,pchild;
read_xml(file,pt);
m_robotid = pt.get<int>("trolley.robotid");
m_robotnum = pt.get<int>("trolley.tronum");
{
pchild = pt.get_child("trolley.trolleystate");
BOOST_AUTO( bg,pchild.begin());
for (int i = 0; i<m_robotnum;i++)
{
int tsid = bg->second.get<int>("<xmlattr>.tsid");
TrolleyState state = (TrolleyState)bg->second.get<int>("<xmlattr>.state");
m_trostate.insert(make_pair(tsid,state));
++bg;
}
}

{
pchild = pt.get_child("trolley.posstate");

for (BOOST_AUTO( bg,pchild.begin());bg != pchild.end(); ++bg)
{
if ("<xmlattr>" == bg->first)
{
continue;
}
CTSPos pos;
TSPosState state;
pos.m_tsid = bg->second.get<int>("<xmlattr>.tsid");
pos.m_face = bg->second.get<int>("<xmlattr>.face");
pos.m_row = bg->second.get<int>("<xmlattr>.row");
pos.m_col = bg->second.get<int>("<xmlattr>.col");
state = (TSPosState)bg->second.get<int>("<xmlattr>.state");
m_mapposstate.insert(make_pair(pos,state));
}
}
return true;
}

catch (exception* e)
{
res = false;
res.InsertDescribe(e->what());
}
catch(...)
{


}
return res;
}
HrResultType SaveToFile()
{
using namespace boost::property_tree;
HrResultType res;
std::string file = "config/trolleyconfig.xml";
try
{
ptree pt;
pt.add("trolley.robotid",m_robotid);
pt.add("trolley.tronum",m_trostate.size());




{
ptree pchild;
std::map<int,TrolleyState>::iterator bg = m_trostate.begin();
for (int i=0;i<m_trostate.size();i++)
{
pchild.add("state","");
}

BOOST_AUTO(ptbg,pchild.begin());


while( bg != m_trostate.end())
{
string d = ptbg->first;
ptbg->second.add("<xmlattr>.tsid",bg->first);
ptbg->second.add("<xmlattr>.state",(int)(bg->second));
bg++;
ptbg++;
}
pt.add_child("trolley.trolleystate",pchild);
}
{

ptree pchild1;
for (int i = 0; i < m_mapposstate.size(); i++)
{
pchild1.add("state","");
}


MAP_POS_STATE::iterator bg = m_mapposstate.begin();
BOOST_AUTO(ptbg,pchild1.begin());
while (bg != m_mapposstate.end())
{

ptbg->second.add("<xmlattr>.tsid",bg->first.m_tsid);
ptbg->second.add("<xmlattr>.face",bg->first.m_face);
ptbg->second.add("<xmlattr>.row",bg->first.m_row);
ptbg->second.add("<xmlattr>.col",bg->first.m_col);
ptbg->second.add("<xmlattr>.state",(int)bg->second);
bg++;
ptbg++;
}
pt.add_child("trolley.posstate",pchild1);
}
pt.put("trolley.posstate.<xmlattr>.num",m_mapposstate.size());


boost::property_tree::xml_writer_settings<char> settings('\t',1); //用于格式调整
write_xml(file,pt,std::locale(),settings);


return true;


}catch(exception* e)
{
res = false;
res.InsertDescribe(e->what());
}
return res;

}

附:xml文档

<?xml version="1.0" encoding="utf-8"?> <trolley> <robotid>1</robotid> <tronum>2</tronum> <trolleystate> <state tsid="1" state="0"/> <state tsid="2" state="2"/> </trolleystate> <posstate num="24"> <state tsid="1" face="0" row="1" col="1" state="3"/> <state tsid="1" face="0" row="1" col="2" state="3"/> <state tsid="1" face="0" row="1" col="3" state="3"/> <state tsid="1" face="0" row="1" col="4" state="3"/> <state tsid="1" face="0" row="2" col="1" state="3"/> <state tsid="1" face="0" row="2" col="2" state="3"/> <state tsid="1" face="0" row="2" col="3" state="3"/> <state tsid="1" face="0" row="2" col="4" state="3"/> <state tsid="1" face="0" row="3" col="1" state="3"/> <state tsid="1" face="0" row="3" col="2" state="3"/> <state tsid="1" face="0" row="3" col="3" state="3"/> <state tsid="1" face="0" row="3" col="4" state="3"/> <state tsid="1" face="1" row="1" col="1" state="3"/> <state tsid="1" face="1" row="1" col="2" state="3"/> <state tsid="1" face="1" row="1" col="3" state="3"/> <state tsid="1" face="1" row="1" col="4" state="3"/> <state tsid="1" face="1" row="2" col="1" state="3"/> <state tsid="1" face="1" row="2" col="2" state="3"/> <state tsid="1" face="1" row="2" col="3" state="3"/> <state tsid="1" face="1" row="2" col="4" state="3"/> <state tsid="1" face="1" row="3" col="1" state="3"/> <state tsid="1" face="1" row="3" col="2" state="3"/> <state tsid="1" face="1" row="3" col="3" state="3"/> <state tsid="1" face="1" row="3" col="4" state="3"/> </posstate> </trolley>

猜你在找的XML相关文章