#include "xmlparamimpl.h" #include xmlparamimpl::xmlparamimpl(std::string filepath) { // QDomDocument doc("mydocument"); mbSuc = false; QFile file(filepath.data()); if (!file.open(QIODevice::ReadOnly)) return; if (!mdoc.setContent(&file)) { file.close(); return; } file.close(); mbSuc = true; } std::string xmlparamimpl::GetParam(std::string paramname, std::string defaultvalue) { std::string strrtn = defaultvalue; if(mbSuc == false) return strrtn; QDomElement docElem = mdoc.documentElement(); QDomNode n = docElem.firstChild(); while(!n.isNull()) { QDomElement e = n.toElement(); // 尝试将节点转换为元素 if(e.nodeName() == "node") { QDomNode nodeparam = n.firstChild(); while(!nodeparam.isNull()) { QDomElement ep = nodeparam.toElement(); if(ep.attribute("name"," ").toStdString() == paramname) { strrtn = ep.attribute("value",defaultvalue.data()).toStdString(); return strrtn; } nodeparam = nodeparam.nextSibling(); } break; } n = n.nextSibling(); } return strrtn; } bool xmlparamimpl::GetParam(std::string paramname, bool bValue) { std::string strdefault = "true"; if(bValue == false)strdefault = "false"; std::string strvalue = GetParam(paramname,strdefault); if(strvalue == "true") { return true; } return false; } float xmlparamimpl::GetParam(std::string paramname, float fvalue) { std::string strdef; strdef = QString::number(fvalue).toStdString(); std::string strvalue = GetParam(paramname,strdef); return float(atof(strvalue.data())); } double xmlparamimpl::GetParam(std::string paramname, double dfvalue) { std::string strdef; strdef = QString::number(dfvalue).toStdString(); std::string strvalue = GetParam(paramname,strdef); return atof(strvalue.data()); } int xmlparamimpl::GetParam(std::string paramname, int ndefaultvalue) { std::string strdef; strdef = QString::number(ndefaultvalue).toStdString(); std::string strvalue = GetParam(paramname,strdef); return atoi(strvalue.data()); }