C ++ to read and write XML file (CRUD)

将数据库表student_table的增删改查用XML来实现:
其中,conditionMap存储条件语句,where "xxx" = "xxx"(where conditionMap.begin()->first == conditionMap.begin()->second)

#include <iostream>
#include "tinyxml.h"
#include <string>
#include <map>
using namespace std;

//判断能否找到指定节点,找不到返回NULL
bool FindNode(const string xmlFileName, const map<string, string> &conditionMap, TiXmlElement *&pNode)
{
	//创建XML文档指针
	TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str());
	if (NULL == pDocument)
	{
		return false;
	}
	pDocument->LoadFile(xmlFileName.c_str());

	TiXmlElement *pRoot = pDocument->RootElement();
	if (NULL == pRoot)
	{
		return false;
	}

	pNode = pRoot->FirstChildElement();
	TiXmlElement *pChildNode = NULL;
	string key = conditionMap.begin()->first;
	while(pNode != NULL)
	{
		pChildNode = pNode->FirstChildElement();
		while (pChildNode != NULL)
		{
			const char *strValue = pChildNode->Value();
			const char *strText  = pChildNode->GetText();
			if (strcmp(key.c_str(), strValue) == 0)
			{
				if (string(strText) == conditionMap.begin()->second)
				{
					return true;
				}
				else
				{
					break;
				}
			}
			pChildNode = pChildNode->NextSiblingElement();
		}
		pNode = pNode->NextSiblingElement();
	}
	return false;
}

void CreateXMLFile(const string &xmlFileName, const string &version, const string &encoding, const string &standalone)
{
	//创建XML文档指针
	TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str());
	if (NULL == pDocument)
	{
		return;
	}

	//声明XML
	TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(version.c_str(), encoding.c_str(), standalone.c_str());
	if (NULL == pDeclaration)
	{
		return;
	}
	pDocument->LinkEndChild(pDeclaration);

	//创建根节点
	string rootName = (xmlFileName.substr(0, xmlFileName.find(".")));//根节点元素名为文件名去掉.xml
	TiXmlElement *pRoot = new TiXmlElement(rootName.c_str());
	if (NULL == pRoot)
	{
		return;
	}
	//关联XML文档,成为XML文档的根节点
	pDocument->LinkEndChild(pRoot);
	//保存文档
	pDocument->SaveFile(xmlFileName.c_str());
}

//insert into student_table (id, name, age, score) values ("1", "小明", "18", ""100) 
void Insert(const string xmlFileName, const map<string, string> &T_Map)
{
	//创建XML文档指针
	TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str());
	if (NULL == pDocument)
	{
		return;
	}
	pDocument->LoadFile(xmlFileName.c_str());

	TiXmlElement *pRoot = pDocument->RootElement();
	if (NULL == pRoot)
	{
		return;
	}

	//创建孩子节点
	string rootName = (xmlFileName.substr(0, xmlFileName.find(".")));//根节点元素名为文件名去掉.xml后+Record
	rootName += "Record";
	TiXmlElement *pNode = new TiXmlElement(rootName.c_str());
	if (NULL == pNode)
	{
		return;
	}

	TiXmlElement *pColumnNode = NULL;
	TiXmlText    *pColumnText = NULL;
	for (auto it = T_Map.begin(); it != T_Map.end(); ++it)
	{
		pColumnNode = new TiXmlElement(it->first.c_str());
		pColumnText = new TiXmlText(it->second.c_str());
		pColumnNode->LinkEndChild(pColumnText);
		pNode->LinkEndChild(pColumnNode);
	}

	pRoot->InsertEndChild(*pNode);
	pDocument->SaveFile(xmlFileName.c_str());
}

//delete from student_table where "xxx" = "xxx"
void Delete(const string xmlFileName, const map<string, string> &conditionMap)
{
	//创建XML文档指针
	TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str());
	if (NULL == pDocument)
	{
		return;
	}
	pDocument->LoadFile(xmlFileName.c_str());

	TiXmlElement *pRoot = pDocument->RootElement();
	if (NULL == pRoot)
	{
		return;
	}

	TiXmlElement *pNode = pRoot->FirstChildElement();
	TiXmlElement *pChildNode = NULL;
	string key = conditionMap.begin()->first;
	while(pNode != NULL)
	{
		pChildNode = pNode->FirstChildElement();
		while (pChildNode != NULL)
		{
			const char *strValue = pChildNode->Value();
			const char *strText  = pChildNode->GetText();
			if (strcmp(key.c_str(), strValue) == 0)
			{
				if (string(strText) == conditionMap.begin()->second)
				{
					pRoot->RemoveChild(pNode);
					pDocument->SaveFile(xmlFileName.c_str());
					return;
				}
				else
				{
					break;
				}

				//pChildNode->Clear();
				//TiXmlText *pText = new TiXmlText(T_Map.begin()->second.c_str());
				//pChildNode->LinkEndChild(pText);
				//pDocument->SaveFile(xmlFileName.c_str());
			}
			pChildNode = pChildNode->NextSiblingElement();
		}
		pNode = pNode->NextSiblingElement();
	}
}

//update student_table set column1 = value1, column2 = value2, ... where "xxx" = "xxx";
void UpDate(const string xmlFileName, const map<string, string> &Set_Map, const map<string, string> &Condition_Map)
{
	//创建XML文档指针
	TiXmlDocument *pDocument = new TiXmlDocument(xmlFileName.c_str());
	if (NULL == pDocument)
	{
		return;
	}
	pDocument->LoadFile(xmlFileName.c_str());

	TiXmlElement *pRoot = pDocument->RootElement();
	if (NULL == pRoot)
	{
		return;
	}

	TiXmlElement *pNode = pRoot->FirstChildElement();
	TiXmlElement *pChildNode = NULL;
	string key = Condition_Map.begin()->first;
	while(pNode != NULL)
	{
		pChildNode = pNode->FirstChildElement();
		while (pChildNode != NULL)
		{
			const char *strValue = pChildNode->Value();
			const char *strText  = pChildNode->GetText();
			if (strcmp(key.c_str(), strValue) == 0)
			{
				if (string(strText) == Condition_Map.begin()->second)
				{
					//用oldMap存储更新前的记录信息,
					map<string, string> oldMap;
					TiXmlElement *pFirstChildNode = pNode->FirstChildElement();
					for (; pFirstChildNode; pFirstChildNode = pFirstChildNode->NextSiblingElement())
					{
						string strValue = pFirstChildNode->Value();
						string strText  = pFirstChildNode->GetText();
						oldMap[strValue] = strText;
					}
					//oldMap保存更新后记录信息
					for (auto it = Set_Map.begin(); it != Set_Map.end(); ++it)
					{
						oldMap[it->first] = it->second;
					}
					//清空XML中的孩子结点(清空所有字段信息)
					pNode->Clear();

					TiXmlElement *pColumnNode = NULL;
					TiXmlText    *pColumnText = NULL;
					for (auto it = oldMap.begin(); it != oldMap.end(); ++it)
					{
						pColumnNode = new TiXmlElement(it->first.c_str());
						pColumnText = new TiXmlText(it->second.c_str());
						pColumnNode->LinkEndChild(pColumnText);
						pNode->LinkEndChild(pColumnNode);
					}

					pDocument->SaveFile(xmlFileName.c_str());
					return;
				}
				else
				{
					break;
				}
			}
			pChildNode = pChildNode->NextSiblingElement();
		}
		pNode = pNode->NextSiblingElement();
	}
}

//select * from student_table where "xxx" = "xxx";
map<string, string> SelectAll(const string xmlFileName, const map<string, string> &conditionMap)
{
	map<string, string> resMap;
	TiXmlElement *pNode;
	if (FindNode(xmlFileName, conditionMap, pNode))
	{
		TiXmlElement *pChildNode = pNode->FirstChildElement();
		while (pChildNode != NULL)
		{
			string strValue = pChildNode->Value();
			string strText  = pChildNode->GetText();
			resMap[strValue] = strText;
			pChildNode = pChildNode->NextSiblingElement();
		}
	}

	return resMap;
}

int main()
{
	string xmlFileName = "Student.xml";
	string version = "1.0";
	string encoding = "UTF-8";
	string standalone = "";

	map<string, string> _Map;
	_Map["a_id"]    = "1";
	_Map["b_name"]  = string("张");
	_Map["c_age"]   = "18";
	_Map["d_score"] = "100";
	map<string, string> conditionMap;
	//创建文件:create student_table
	CreateXMLFile(xmlFileName, version, encoding, standalone);
	//插入记录:insert into student_table
	Insert(xmlFileName, _Map);
	_Map["a_id"] = "2";
	Insert(xmlFileName, _Map);
	_Map["a_id"] = "3";
	Insert(xmlFileName, _Map);

	//删除记录:delete from student_table where "xxx" = "xxx";
	conditionMap["a_id"] = "2";
	Delete(xmlFileName, conditionMap);
	
	//更新记录:
	_Map["a_id"] = "4";
	_Map["c_age"] = "25";
	conditionMap["a_id"] = "3";
	UpDate(xmlFileName, _Map, conditionMap);
	
	//查询记录:
	map<string, string> resMap;
	conditionMap["a_id"] = "4";
	resMap = SelectAll(xmlFileName, conditionMap);
	for (auto it = resMap.begin(); it != resMap.end(); ++it)
	{
		cout << it->first << " : " << it->second << endl;
	}
	system("pause");
}
Published 228 original articles · won praise 44 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_40945965/article/details/86991092