雁山老司机

雁山萝莉保护协会

<老司机原创>C++ xml读取类

只是给C++入门者一个参考,大佬请无视。

  1. //**--------------------------------------------------------------------**
  2. //**						Xml读写类 by 雁山老司机						**
  3. //**--------------------------------------------------------------------**
  4. //调用方法:
  5. //MyXml xml_1;
  6. //
  7. //xml_1.getData("id","int"); //(方法一)
  8. //int ID = xml_1.P_int; //(方法一)
  9.  
  10. //int ID = xml_1.getData_s("int"); //(方法二)
  11.  
  12. //------------------------------2017:12:17------------------------------
  13. //初步完成了对Xml的读取操作,支持int、double、char*、string 。
  14.  
  15. #ifndef HasXml
  16.  
  17. #include<iostream>
  18. #include<Windows.h>
  19. #include<fstream>
  20. #include<string>
  21. #include <sstream>    //使用stringstream需要引入这个头文件 
  22.  
  23. using namespace std;
  24.  
  25. #endif // !HasXml
  26.  
  27. class MyXml {
  28. public:
  29. //不安全
  30. int P_int;
  31. double P_double;
  32. char *P_char;
  33. string P_str;
  34. MyXml(string My_file)
  35. {
  36. File_s = My_file;
  37. File_c = (char*)My_file.c_str();
  38. //调试
  39. //cout << "目标文件:" << File_c << endl;
  40.  
  41. //验证文件是否正常
  42. try
  43. {
  44. FILE *fp;
  45. fopen_s(&fp, File_c, "r+");
  46. fclose(fp);
  47. }
  48. catch (exception)
  49. {
  50. cout << "文件有误,无法打开目标文件!" << endl;
  51. }
  52. }
  53.  
  54. void setFile(string My_file)
  55. {
  56. File_s = My_file;
  57. File_c = (char*)My_file.c_str();
  58. }
  59. //获取xml数据到公有变量
  60. bool getData(string name, string type)
  61. {
  62. bool Success = false;
  63. string Data = getDataStr(name);
  64. if (type == "int")
  65. {
  66. Success = true;
  67. P_int = atoi(Data.c_str());
  68. }
  69. if (type == "char*")
  70. {
  71. Success = true;
  72. P_char = (char*)Data.c_str();
  73. }
  74. if (type == "string")
  75. {
  76. Success = true;
  77. //Data = UTF8ToGBK(Data.c_str());
  78. P_str = Data;
  79. }
  80. if (type == "double")
  81. {
  82. Success = true;
  83. P_double = atof(Data.c_str()); //转换double
  84. }
  85. return Success;
  86. }
  87.  
  88. //直接返回泛型
  89. template
  90. Type getData_s(string name)
  91. {
  92. string Data = getDataStr(name);
  93. //Data = UTF8ToGBK(Data.c_str());
  94. istringstream iss(Data);
  95. Type Result;
  96. iss >> Result;
  97. return Result;
  98. }
  99. void ShowVersion()
  100. {
  101. getInformation();
  102. cout << "Xml版本:" << Version << endl;
  103. cout << "编码方式:" << Encoding << endl; } private: char *File_c; string File_s; double Version; string Encoding; //分析xml的版本信息 void getInformation() { try { Version = atof(getDataStr_s("version=\"", "\" ").c_str()); Encoding = getDataStr_s("encoding=\"", "\"?>");
  104. }
  105. catch (exception)
  106. {
  107. cout << "版本读取发生错误,不能识别的格式" << endl; } } //获取xml全文内容 string getDoc() { string doc = ""; char data[40096]; //开始读文件 ifstream infile; infile.open(File_s); while (!infile.eof()) { infile >> data;
  108. doc += data;
  109. }
  110. infile.close(); //结束读文件
  111. return doc;
  112. }
  113.  
  114. //取得对应键值下的数据
  115. string getDataStr(string name)
  116. {
  117. std::string doc = getDoc();
  118. string a, b; a = "<" + name + ">"; b = "</" + name + ">";
  119. int i, j;
  120.  
  121. //分割字符串
  122. i = doc.find(a) + a.length();
  123. j = doc.find(b);
  124. string result = doc.substr(i, j - i);
  125.  
  126. return result;
  127. }
  128.  
  129. //debug 获得二者之间的数值
  130. string getDataStr_s(string a, string b)
  131. {
  132. std::string doc = getDoc();
  133. int i, j;
  134.  
  135. //分割字符串
  136. i = doc.find(a) + a.length();
  137. j = doc.find(b);
  138. string result = doc.substr(i, j - i);
  139.  
  140. return result;
  141. }
  142. };
点赞

发表评论