读XML中KeyValue

前端之家收集整理的这篇文章主要介绍了读XML中KeyValue前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是一个类,直接调用就可以了。

H

  1. class common_tool
  2. {
  3. public:
  4. common_tool(void);
  5. ~common_tool(void);
  6.  
  7. std::string read_config(const char* filename,const char* parent_start,const char* parent_end,const char* child_start,const char* child_end);
  8. /*
  9. 判断在一个字符串里面是否存在查找的字符串
  10. key 查找字符串
  11. */
  12. const bool exist_parent_key(const char* source,const char* key);
  13.  
  14. /*
  15. 得到2个字符串中间的值
  16. */
  17. std::string get_value_between(const char* source,const char* keyname_start,const char* keyname_end);
  18. };


CPP

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include "common_tool.h"
  6. using namespace std;
  7.  
  8. common_tool::common_tool(void)
  9. {
  10.  
  11. }
  12.  
  13.  
  14. common_tool::~common_tool(void)
  15. {
  16.  
  17. }
  18.  
  19. std::string common_tool::read_config(const char* filename,const char* child_end)
  20. {
  21. string strEmptyString="";
  22. ifstream file(filename,ios::in);
  23. if (!file.is_open())
  24. {
  25. return strEmptyString;
  26. }
  27. string line_string="";
  28. string document_txt="";
  29. int pos=-1;
  30. string keyname_parent_start = parent_start;
  31. string keyname_parent_end =parent_end;
  32. string keyname_child_start = child_start;
  33. string keyname_child_end = child_end;
  34. string keyvalue = "";
  35. bool find_parent_start =false;
  36. bool find_parent_end =false;
  37. bool find_parent =false;
  38. while(getline(file,line_string))
  39. {
  40. //find start of parent keyname
  41. if(false == find_parent)
  42. {
  43. find_parent_start = exist_parent_key(line_string.c_str(),keyname_parent_start.c_str());
  44. if(true == find_parent_start)
  45. {
  46. find_parent = true;
  47. }
  48. }
  49. // find end of parent keyname
  50. find_parent_end = exist_parent_key(line_string.c_str(),keyname_parent_end.c_str());
  51. if(true == find_parent_end)
  52. {
  53. find_parent = false;
  54. break;
  55. }
  56.  
  57. //find keyvalue by keyname what you want in parent keyname.
  58. if(true == find_parent)
  59. {
  60. keyvalue="";
  61. keyvalue.append(get_value_between(line_string.c_str(),keyname_child_start.c_str(),keyname_child_end.c_str()));
  62. if(keyvalue.length()>0)
  63. {
  64. break;
  65. }
  66. }
  67. }
  68. file.close();
  69. return keyvalue;
  70. }
  71.  
  72. const bool common_tool::exist_parent_key(const char* source,const char* key)
  73. {
  74. bool rs = false;
  75. string keyname = key;
  76. string line = source;
  77.  
  78. int pos = 0;
  79. pos=line.find(key,0);
  80. if(pos<0)
  81. {
  82. rs = false;
  83. }
  84. else
  85. {
  86. rs = true;
  87. }
  88. return rs;
  89. }
  90.  
  91. std::string common_tool::get_value_between(const char* source,const char* keyname_end)
  92. {
  93. bool rs = false;
  94. string str_source = source;
  95. string keyvalue = "";
  96. static const char* str_empty_value = "";
  97. int pos_start = 0;
  98. int pos_end = 0;
  99. int len = 0;
  100. len = str_source.length();
  101. if(len<=0)
  102. {
  103. return str_empty_value;
  104. }
  105. pos_start = str_source.find(keyname_start,0);
  106. pos_end = str_source.find(keyname_end,0);
  107. if(pos_start<0 || pos_end <0)
  108. {
  109. return str_empty_value; // can not find!
  110. }
  111. int pos_keyvalue_start = pos_start + strlen(keyname_start);
  112. int len_keyvalue = pos_end - pos_keyvalue_start;//len - pos_keyvalue_start - strlen(keyname_end);
  113. if(len_keyvalue<=0)
  114. {
  115. return str_empty_value;
  116. }
  117. keyvalue = str_source.substr(pos_keyvalue_start,len_keyvalue);
  118. return keyvalue;
  119. }


完。

猜你在找的XML相关文章