XML文件——增删改查
1 将数据库表student_table的增删改查用XML来实现: 2 其中,conditionMap存储条件语句,where "xxx" = "xxx"(where conditionMap.begin()->first == conditionMap.begin()->second) 3 4 #include <iostream> 5 #include "tinyxml.h" 6 #include <string> 7 #include <map> 8 using namespace std; 9 10 //判断能否找到指定节点,找不到返回NULL 11 bool FindNode(const string xmlFileName, const map<string, string> &conditionMap, TiXmlElement *&pNode) 12 { 13 //创建XML文档指针 14 TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str()); 15 if (NULL == pDocument) 16 { 17 return false; 18 } 19 pDocument->LoadFile(xmlFileName.c_str()); 20 21 TiXmlElement *pRoot = pDocument->RootElement(); 22 if (NULL == pRoot) 23 { 24 return false; 25 } 26 27 pNode = pRoot->FirstChildElement(); 28 TiXmlElement *pChildNode = NULL; 29 string key = conditionMap.begin()->first; 30 while(pNode != NULL) 31 { 32 pChildNode = pNode->FirstChildElement(); 33 while (pChildNode != NULL) 34 { 35 const char *strValue = pChildNode->Value(); 36 const char *strText = pChildNode->GetText(); 37 if (strcmp(key.c_str(), strValue) == 0) 38 { 39 if (string(strText) == conditionMap.begin()->second) 40 { 41 return true; 42 } 43 else 44 { 45 break; 46 } 47 } 48 pChildNode = pChildNode->NextSiblingElement(); 49 } 50 pNode = pNode->NextSiblingElement(); 51 } 52 return false; 53 } 54 55 void CreateXMLFile(const string &xmlFileName, const string &version, const string &encoding, const string &standalone) 56 { 57 //创建XML文档指针 58 TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str()); 59 if (NULL == pDocument) 60 { 61 return; 62 } 63 64 //声明XML 65 TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(version.c_str(), encoding.c_str(), standalone.c_str()); 66 if (NULL == pDeclaration) 67 { 68 return; 69 } 70 pDocument->LinkEndChild(pDeclaration); 71 72 //创建根节点 73 string rootName = (xmlFileName.substr(0, xmlFileName.find(".")));//根节点元素名为文件名去掉.xml 74 TiXmlElement *pRoot = new TiXmlElement(rootName.c_str()); 75 if (NULL == pRoot) 76 { 77 return; 78 } 79 //关联XML文档,成为XML文档的根节点 80 pDocument->LinkEndChild(pRoot); 81 //保存文档 82 pDocument->SaveFile(xmlFileName.c_str()); 83 } 84 85 //insert into student_table (id, name, age, score) values ("1", "小明", "18", ""100) 86 void Insert(const string xmlFileName, const map<string, string> &T_Map) 87 { 88 //创建XML文档指针 89 TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str()); 90 if (NULL == pDocument) 91 { 92 return; 93 } 94 pDocument->LoadFile(xmlFileName.c_str()); 95 96 TiXmlElement *pRoot = pDocument->RootElement(); 97 if (NULL == pRoot) 98 { 99 return; 100 } 101 102 //创建孩子节点 103 string rootName = (xmlFileName.substr(0, xmlFileName.find(".")));//根节点元素名为文件名去掉.xml后+Record 104 rootName += "Record"; 105 TiXmlElement *pNode = new TiXmlElement(rootName.c_str()); 106 if (NULL == pNode) 107 { 108 return; 109 } 110 111 TiXmlElement *pColumnNode = NULL; 112 TiXmlText *pColumnText = NULL; 113 for (auto it = T_Map.begin(); it != T_Map.end(); ++it) 114 { 115 pColumnNode = new TiXmlElement(it->first.c_str()); 116 pColumnText = new TiXmlText(it->second.c_str()); 117 pColumnNode->LinkEndChild(pColumnText); 118 pNode->LinkEndChild(pColumnNode); 119 } 120 121 pRoot->InsertEndChild(*pNode); 122 pDocument->SaveFile(xmlFileName.c_str()); 123 } 124 125 //delete from student_table where "xxx" = "xxx" 126 void Delete(const string xmlFileName, const map<string, string> &conditionMap) 127 { 128 //创建XML文档指针 129 TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str()); 130 if (NULL == pDocument) 131 { 132 return; 133 } 134 pDocument->LoadFile(xmlFileName.c_str()); 135 136 TiXmlElement *pRoot = pDocument->RootElement(); 137 if (NULL == pRoot) 138 { 139 return; 140 } 141 142 TiXmlElement *pNode = pRoot->FirstChildElement(); 143 TiXmlElement *pChildNode = NULL; 144 string key = conditionMap.begin()->first; 145 while(pNode != NULL) 146 { 147 pChildNode = pNode->FirstChildElement(); 148 while (pChildNode != NULL) 149 { 150 const char *strValue = pChildNode->Value(); 151 const char *strText = pChildNode->GetText(); 152 if (strcmp(key.c_str(), strValue) == 0) 153 { 154 if (string(strText) == conditionMap.begin()->second) 155 { 156 pRoot->RemoveChild(pNode); 157 pDocument->SaveFile(xmlFileName.c_str()); 158 return; 159 } 160 else 161 { 162 break; 163 } 164 165 //pChildNode->Clear(); 166 //TiXmlText *pText = new TiXmlText(T_Map.begin()->second.c_str()); 167 //pChildNode->LinkEndChild(pText); 168 //pDocument->SaveFile(xmlFileName.c_str()); 169 } 170 pChildNode = pChildNode->NextSiblingElement(); 171 } 172 pNode = pNode->NextSiblingElement(); 173 } 174 } 175 176 //update student_table set column1 = value1, column2 = value2, ... where "xxx" = "xxx"; 177 void UpDate(const string xmlFileName, const map<string, string> &Set_Map, const map<string, string> &Condition_Map) 178 { 179 //创建XML文档指针 180 TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str()); 181 if (NULL == pDocument) 182 { 183 return; 184 } 185 pDocument->LoadFile(xmlFileName.c_str()); 186 187 TiXmlElement *pRoot = pDocument->RootElement(); 188 if (NULL == pRoot) 189 { 190 return; 191 } 192 193 TiXmlElement *pNode = pRoot->FirstChildElement(); 194 TiXmlElement *pChildNode = NULL; 195 string key = Condition_Map.begin()->first; 196 while(pNode != NULL) 197 { 198 pChildNode = pNode->FirstChildElement(); 199 while (pChildNode != NULL) 200 { 201 const char *strValue = pChildNode->Value(); 202 const char *strText = pChildNode->GetText(); 203 if (strcmp(key.c_str(), strValue) == 0) 204 { 205 if (string(strText) == Condition_Map.begin()->second) 206 { 207 //用oldMap存储更新前的记录信息, 208 map<string, string> oldMap; 209 TiXmlElement *pFirstChildNode = pNode->FirstChildElement(); 210 for (; pFirstChildNode; pFirstChildNode = pFirstChildNode->NextSiblingElement()) 211 { 212 string strValue = pFirstChildNode->Value(); 213 string strText = pFirstChildNode->GetText(); 214 oldMap[strValue] = strText; 215 } 216 //oldMap保存更新后记录信息 217 for (auto it = Set_Map.begin(); it != Set_Map.end(); ++it) 218 { 219 oldMap[it->first] = it->second; 220 } 221 //清空XML中的孩子结点(清空所有字段信息) 222 pNode->Clear(); 223 224 TiXmlElement *pColumnNode = NULL; 225 TiXmlText *pColumnText = NULL; 226 for (auto it = oldMap.begin(); it != oldMap.end(); ++it) 227 { 228 pColumnNode = new TiXmlElement(it->first.c_str()); 229 pColumnText = new TiXmlText(it->second.c_str()); 230 pColumnNode->LinkEndChild(pColumnText); 231 pNode->LinkEndChild(pColumnNode); 232 } 233 234 pDocument->SaveFile(xmlFileName.c_str()); 235 return; 236 } 237 else 238 { 239 break; 240 } 241 } 242 pChildNode = pChildNode->NextSiblingElement(); 243 } 244 pNode = pNode->NextSiblingElement(); 245 } 246 } 247 248 //select * from student_table where "xxx" = "xxx"; 249 map<string, string> SelectAll(const string xmlFileName, const map<string, string> &conditionMap) 250 { 251 map<string, string> resMap; 252 TiXmlElement *pNode; 253 if (FindNode(xmlFileName, conditionMap, pNode)) 254 { 255 TiXmlElement *pChildNode = pNode->FirstChildElement(); 256 while (pChildNode != NULL) 257 { 258 string strValue = pChildNode->Value(); 259 string strText = pChildNode->GetText(); 260 resMap[strValue] = strText; 261 pChildNode = pChildNode->NextSiblingElement(); 262 } 263 } 264 265 return resMap; 266 } 267 268 int main() 269 { 270 string xmlFileName = "Student.xml"; 271 string version = "1.0"; 272 string encoding = "UTF-8"; 273 string standalone = ""; 274 275 map<string, string> _Map; 276 _Map["a_id"] = "1"; 277 _Map["b_name"] = string("张"); 278 _Map["c_age"] = "18"; 279 _Map["d_score"] = "100"; 280 map<string, string> conditionMap; 281 //创建文件:create student_table 282 CreateXMLFile(xmlFileName, version, encoding, standalone); 283 //插入记录:insert into student_table 284 Insert(xmlFileName, _Map); 285 _Map["a_id"] = "2"; 286 Insert(xmlFileName, _Map); 287 _Map["a_id"] = "3"; 288 Insert(xmlFileName, _Map); 289 290 //删除记录:delete from student_table where "xxx" = "xxx"; 291 conditionMap["a_id"] = "2"; 292 Delete(xmlFileName, conditionMap); 293 294 //更新记录: 295 _Map["a_id"] = "4"; 296 _Map["c_age"] = "25"; 297 conditionMap["a_id"] = "3"; 298 UpDate(xmlFileName, _Map, conditionMap); 299 300 //查询记录: 301 map<string, string> resMap; 302 conditionMap["a_id"] = "4"; 303 resMap = SelectAll(xmlFileName, conditionMap); 304 for (auto it = resMap.begin(); it != resMap.end(); ++it) 305 { 306 cout << it->first << " : " << it->second << endl; 307 } 308 system("pause"); 309 }
标签:XML,string,改查,pChildNode,xmlFileName,pNode,str,增删,NULL From: https://www.cnblogs.com/Jack-Elvis/p/18530188