读写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>

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念