c++:boost讀取xml
int load(const std::string &strFilePath, const std::string &strFileName)
{
m_mapParam.clear();
string strConfFullPath;
if (kbdSuccess != makeConfigFileFullPath(strFilePath, strFileName, strConfFullPath))
{
return kbdFailed;
}
boost::property_tree::ptree pt;
namespace xml = boost::property_tree::xml_parser;
try
{
xml::read_xml(strConfFullPath, pt, xml::no_comments);
BOOST_AUTO(module, pt.get_child("root"));
for (BOOST_AUTO(pModuleIter, module.begin()); pModuleIter != module.end(); ++pModuleIter)
{
string strModuleName = pModuleIter->second.get<string>("<xmlattr>.name");
boost::property_tree::ptree ptParam = pModuleIter->second;
for (BOOST_AUTO(pParamIter, ptParam.begin()); pParamIter != ptParam.end(); ++pParamIter)
{
if (pParamIter->first == "param")
{
string strKey = pParamIter->second.get<string>("<xmlattr>.key");
string strValue = pParamIter->second.get<string>("<xmlattr>.value");
m_mapParam[strModuleName + CN_PARAM_SEPARATOR + strKey].push_back(strValue);
}
}
}
}
catch (std::exception &ex)
{
LOGERROR("解析配置檔案[%s]失敗.Msg=[%s]", strConfFullPath.c_str(), ex.what());
return kbdFailed;
}
return kbdSuccess;
}