xmlparamimpl.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "xmlparamimpl.h"
  2. #include <QFile>
  3. xmlparamimpl::xmlparamimpl(std::string filepath)
  4. {
  5. // QDomDocument doc("mydocument");
  6. mbSuc = false;
  7. QFile file(filepath.data());
  8. if (!file.open(QIODevice::ReadOnly))
  9. return;
  10. if (!mdoc.setContent(&file)) {
  11. file.close();
  12. return;
  13. }
  14. file.close();
  15. mbSuc = true;
  16. }
  17. std::string xmlparamimpl::GetParam(std::string paramname, std::string defaultvalue)
  18. {
  19. std::string strrtn = defaultvalue;
  20. if(mbSuc == false) return strrtn;
  21. QDomElement docElem = mdoc.documentElement();
  22. QDomNode n = docElem.firstChild();
  23. while(!n.isNull())
  24. {
  25. QDomElement e = n.toElement(); // 尝试将节点转换为元素
  26. if(e.nodeName() == "node")
  27. {
  28. QDomNode nodeparam = n.firstChild();
  29. while(!nodeparam.isNull())
  30. {
  31. QDomElement ep = nodeparam.toElement();
  32. if(ep.attribute("name"," ").toStdString() == paramname)
  33. {
  34. strrtn = ep.attribute("value",defaultvalue.data()).toStdString();
  35. return strrtn;
  36. }
  37. nodeparam = nodeparam.nextSibling();
  38. }
  39. break;
  40. }
  41. n = n.nextSibling();
  42. }
  43. return strrtn;
  44. }
  45. bool xmlparamimpl::GetParam(std::string paramname, bool bValue)
  46. {
  47. std::string strdefault = "true";
  48. if(bValue == false)strdefault = "false";
  49. std::string strvalue = GetParam(paramname,strdefault);
  50. if(strvalue == "true")
  51. {
  52. return true;
  53. }
  54. return false;
  55. }
  56. float xmlparamimpl::GetParam(std::string paramname, float fvalue)
  57. {
  58. std::string strdef;
  59. strdef = QString::number(fvalue).toStdString();
  60. std::string strvalue = GetParam(paramname,strdef);
  61. return float(atof(strvalue.data()));
  62. }
  63. double xmlparamimpl::GetParam(std::string paramname, double dfvalue)
  64. {
  65. std::string strdef;
  66. strdef = QString::number(dfvalue).toStdString();
  67. std::string strvalue = GetParam(paramname,strdef);
  68. return atof(strvalue.data());
  69. }
  70. int xmlparamimpl::GetParam(std::string paramname, int ndefaultvalue)
  71. {
  72. std::string strdef;
  73. strdef = QString::number(ndefaultvalue).toStdString();
  74. std::string strvalue = GetParam(paramname,strdef);
  75. return atoi(strvalue.data());
  76. }