读写xml文档

前端之家收集整理的这篇文章主要介绍了读写xml文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
HrResultType LoadFromFile()
@H_404_1@{
@H_404_1@using namespace boost::property_tree;
@H_404_1@HrResultType res;
@H_404_1@std::string file = "config/trolleyconfig.xml";
@H_404_1@try
@H_404_1@{
@H_404_1@ptree pt,pchild;
@H_404_1@read_xml(file,pt);
@H_404_1@m_robotid = pt.get<int>("trolley.robotid");
@H_404_1@m_robotnum = pt.get<int>("trolley.tronum");
@H_404_1@{
@H_404_1@pchild = pt.get_child("trolley.trolleystate");
@H_404_1@BOOST_AUTO( bg,pchild.begin());
@H_404_1@for (int i = 0; i<m_robotnum;i++)
@H_404_1@{ @H_404_1@
@H_404_1@int tsid = bg->second.get<int>("<xmlattr>.tsid");
@H_404_1@TrolleyState state = (TrolleyState)bg->second.get<int>("<xmlattr>.state");
@H_404_1@m_trostate.insert(make_pair(tsid,state));
@H_404_1@++bg;
@H_404_1@}
@H_404_1@}
@H_404_1@
@H_404_1@{ @H_404_1@
@H_404_1@pchild = pt.get_child("trolley.posstate"); @H_404_1@
@H_404_1@
@H_404_1@for (BOOST_AUTO( bg,pchild.begin());bg != pchild.end(); ++bg)
@H_404_1@{
@H_404_1@if ("<xmlattr>" == bg->first)
@H_404_1@{
@H_404_1@continue;
@H_404_1@}
@H_404_1@CTSPos pos;
@H_404_1@TSPosState state; @H_404_1@
@H_404_1@pos.m_tsid = bg->second.get<int>("<xmlattr>.tsid");
@H_404_1@pos.m_face = bg->second.get<int>("<xmlattr>.face");
@H_404_1@pos.m_row = bg->second.get<int>("<xmlattr>.row");
@H_404_1@pos.m_col = bg->second.get<int>("<xmlattr>.col");
@H_404_1@state = (TSPosState)bg->second.get<int>("<xmlattr>.state");
@H_404_1@m_mapposstate.insert(make_pair(pos,state)); @H_404_1@
@H_404_1@}
@H_404_1@}
@H_404_1@return true;
@H_404_1@}
@H_404_1@
@H_404_1@catch (exception* e)
@H_404_1@{
@H_404_1@res = false;
@H_404_1@res.InsertDescribe(e->what());
@H_404_1@}
@H_404_1@catch(...)
@H_404_1@{


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




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


@H_404_1@while( bg != m_trostate.end())
@H_404_1@{ @H_404_1@
@H_404_1@string d = ptbg->first;
@H_404_1@ptbg->second.add("<xmlattr>.tsid",bg->first);
@H_404_1@ptbg->second.add("<xmlattr>.state",(int)(bg->second));
@H_404_1@bg++;
@H_404_1@ptbg++;
@H_404_1@} @H_404_1@
@H_404_1@pt.add_child("trolley.trolleystate",pchild);
@H_404_1@} @H_404_1@
@H_404_1@{
@H_404_1@
@H_404_1@ptree pchild1;
@H_404_1@for (int i = 0; i < m_mapposstate.size(); i++)
@H_404_1@{
@H_404_1@pchild1.add("state","");
@H_404_1@}


@H_404_1@MAP_POS_STATE::iterator bg = m_mapposstate.begin();
@H_404_1@BOOST_AUTO(ptbg,pchild1.begin());
@H_404_1@while (bg != m_mapposstate.end())
@H_404_1@{
@H_404_1@
@H_404_1@ptbg->second.add("<xmlattr>.tsid",bg->first.m_tsid);
@H_404_1@ptbg->second.add("<xmlattr>.face",bg->first.m_face);
@H_404_1@ptbg->second.add("<xmlattr>.row",bg->first.m_row);
@H_404_1@ptbg->second.add("<xmlattr>.col",bg->first.m_col);
@H_404_1@ptbg->second.add("<xmlattr>.state",(int)bg->second);
@H_404_1@bg++;
@H_404_1@ptbg++;
@H_404_1@}
@H_404_1@pt.add_child("trolley.posstate",pchild1);
@H_404_1@}
@H_404_1@pt.put("trolley.posstate.<xmlattr>.num",m_mapposstate.size());


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


@H_404_1@return true;


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

@H_404_1@}

附: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相关文章