#include "xmlconfig.h" #include #include namespace settings { XmlConfig::XmlConfig() {} XmlConfig::~XmlConfig() {} bool XmlConfig::Load(const std::string &path) { tinyxml2::XMLError err = xmlDocument_.LoadFile(path.c_str()); if (err != tinyxml2::XML_SUCCESS) { HTELINK_LOG_ERR("load %s failed, %d", path.c_str(), err); return false; } return true; } void XmlConfig::GetElemValue(const std::string &father, std::function &)> func) { xmlRoot_ = xmlDocument_.RootElement(); if (!xmlRoot_) { HTELINK_LOG_ERR("XML ROOT is nullptr, %s", father.c_str()); return; } tinyxml2::XMLElement *fatherEle = xmlRoot_; if (father == "") { fatherEle = xmlRoot_->FirstChildElement(father.c_str()); if (!fatherEle) { HTELINK_LOG_ERR("XML ROOT no father element %s", father.c_str()); return; } } for (tinyxml2::XMLElement *book = xmlRoot_->FirstChildElement(father.c_str()); book != nullptr; book = book->NextSiblingElement(father.c_str())) { std::map kv; for (tinyxml2::XMLElement *elem = book->FirstChildElement(); elem != nullptr; elem = elem->NextSiblingElement()) { kv[elem->Name()] = elem->GetText(); } func(kv); } } const std::string XmlConfig::GetElemValueString(const std::string &father, const std::string elem) { xmlRoot_ = xmlDocument_.RootElement(); if (!xmlRoot_) { HTELINK_LOG_ERR("XML ROOT is nullptr, %s", elem.c_str()); return ""; } tinyxml2::XMLElement *fatherEle = xmlRoot_; if (father != "") { fatherEle = xmlRoot_->FirstChildElement(father.c_str()); if (!fatherEle) { HTELINK_LOG_ERR("XML ROOT no father element %s", father.c_str()); return ""; } } tinyxml2::XMLElement *xmlEle = fatherEle->FirstChildElement(elem.c_str()); if (!xmlEle) { HTELINK_LOG_ERR("XML ROOT no child element %s", elem.c_str()); return ""; } return xmlEle->GetText(); } const int32_t XmlConfig::GetElemValueInt(const std::string &father, const std::string elem) { std::string result = GetElemValueString(father, elem); return atoi(result.c_str()); } const bool XmlConfig::GetElemValueBool(const std::string &father, const std::string elem) { std::string result = GetElemValueString(father, elem); return atoi(result.c_str()) != 0; } } // namespace settings