tinyxml2简单的创建、增、删、改、查

最近涉及到配置文件的问题,之前一直使用tinyxml,不过后来再上网看了看,发现tinyxml2有较之于tinyxml的优点,这里简单的介绍下tinyxml2的操作(xml类似于小型数据库,既然是数据库,必然有增删改查)

这里给出我准备齐全的tinyxml2压缩包(里面有很多东西)

tinyxml2压缩包下载

这里是我的代码(我的代码基础源于这篇博客https://blog.csdn.net/K346K346/article/details/48750417,不过这篇博客的代码有好些问题,不能直接运行,我花费了点时间将其修改升级为可直接编译运行的代码)

#include "E:\\tinyxml2\\tinyxml2-master\\tinyxml2.h"
#include "E:\\tinyxml2\\tinyxml2-master\\tinyxml2.cpp"
#include 
using namespace tinyxml2;
using namespace std;

//创建xml文档
int CreateXML(const char * path)
{
    const char * declaration = "";
	XMLDocument Doc;
	Doc.Parse(declaration);

	XMLElement * Root = Doc.NewElement("DBUSER");
	Doc.InsertEndChild(Root);

	return Doc.SaveFile(path);
}
class User
{
public:
	User()
	{
		gender = 0;
		cout << "User的构造函数被调用..." << endl;
	}
	//构造函数可以被重载
	User(const string& userName, const string& password, int gender, const string& mobile, const string& email)
	{
		this->userName=userName;
		this->password=password;
		this->gender=gender;
		this->mobile=mobile;
		this->email=email;
		cout << "User的赋值构造函数被调用..." << endl;
	};
	void show()
	{
		printf("%s\n",userName.c_str());
		printf("%s\n",password.c_str());
		printf("%d\n",gender);
		printf("%s\n",mobile.c_str());
		printf("%s\n",email.c_str());
	}
	string userName;
	string password;
	int gender;
	string mobile;
	string email;

private:
protected:
};

//增加一个节点
int insertXMLNode(const char* xmlPath,const User& user)
{
	XMLDocument doc;
	int res=doc.LoadFile(xmlPath);
	if(res!=0)
	{
		cout<<"load xml file failed"<SetAttribute("Name",user.userName.c_str());
	userNode->SetAttribute("Password ",user.password.c_str());
	root->InsertEndChild(userNode);

	XMLElement* gender = doc.NewElement("Gender");
	char *p = new char[2];
	XMLText* genderText=doc.NewText(itoa(user.gender,p,10));
	gender->InsertEndChild(genderText);
	userNode->InsertEndChild(gender);

	XMLElement* mobile = doc.NewElement("Mobile");
	mobile->InsertEndChild(doc.NewText(user.mobile.c_str()));
	userNode->InsertEndChild(mobile);

	XMLElement* email = doc.NewElement("Email");
	email->InsertEndChild(doc.NewText(user.email.c_str()));
	userNode->InsertEndChild(email);
    
	delete[] p;
	return doc.SaveFile(xmlPath);
}


//查询节点
//function:根据用户名获取用户节点
//param:root:xml文件根节点;userName:用户名
//return:用户节点
XMLElement* queryUserNodeByName(XMLElement* root,const string& userName)
{

	XMLElement* userNode=root->FirstChildElement("User");
	while(userNode != NULL)
	{
		//cout << "开始寻找节点" << endl;
		if(strcmp(userNode->Attribute("Name"), userName.c_str()) == 0)
		{
			//cout << "找到节点..." << endl;
			break;
		}
		userNode=userNode->NextSiblingElement();//下一个兄弟节点
	}
	return userNode;
}


//获取用户信息
User* queryUserByName(const char* xmlPath,const string& userName)
{
	XMLDocument doc = new XMLDocument;
	if(doc.LoadFile(xmlPath)!=0)
	{
		cout<<"load xml file failed"<userName = userName;
		
		user->password = userNode->Attribute("Password");
		
		XMLElement* genderNode = userNode->FirstChildElement("Gender");
		user->gender = atoi(genderNode->GetText());
		XMLElement* mobileNode = userNode->FirstChildElement("Mobile");
		user->mobile = mobileNode->GetText();     
		XMLElement* emailNode = userNode->FirstChildElement("Email");
		user->email = emailNode->GetText();           
		return user;
	}
	else
	{
		printf("没有读取到节点\n");
	}
	
	//return NULL;
}

//修改指定节点内容

//function:修改指定节点内容
//param:xmlPath:xml文件路径;user:用户对象
//return:bool
bool updateUser(const char* xmlPath,User* user)
{
	XMLDocument doc;
	if(doc.LoadFile(xmlPath) != 0)
	{
		cout<<"load xml file failed"<userName);

	if(userNode != NULL)
	{
		if(strcmp(user->password.c_str(),userNode->Attribute("Password")) != 0)
		{
			userNode->SetAttribute("Password",user->password.c_str());  //修改属性
		}
		XMLElement* genderNode=userNode->FirstChildElement("Gender");
		if(user->gender!=atoi(genderNode->GetText()))  
		{
			char *p = new char[2];
			genderNode->SetText(itoa(user->gender,p,10));   //修改节点内容
                delete[] p;
		}
		XMLElement* mobileNode=userNode->FirstChildElement("Mobile");
		if(strcmp(user->mobile.c_str(),mobileNode->GetText()) != 0)
		{
			mobileNode->SetText(user->mobile.c_str());
		}
		XMLElement* emailNode=userNode->FirstChildElement("Email");
		if(strcmp(user->email.c_str(),emailNode->GetText()) != 0)
		{
			emailNode->SetText(user->email.c_str());
		}
		if(doc.SaveFile(xmlPath)==0)
			return true;
	}

	return false;
}

//删除指定节点
//function:删除指定节点内容
//param:xmlPath:xml文件路径;userName:用户名称
//return:bool
bool deleteUserByName(const char* xmlPath,const string& userName)
{
	XMLDocument doc;
	if(doc.LoadFile(xmlPath)!=0)
	{
		cout<<"load xml file failed"<DeleteAttribute("Password");//删除属性
		XMLElement* emailNode=userNode->FirstChildElement("Email");
		userNode->DeleteChild(emailNode); //删除指定节点
		//userNode->DeleteChildren();//删除节点的所有孩子节点
		if(doc.SaveFile(xmlPath)==0)
			return true;
	}
	return false;
}
int main()
{
    int rec = 0;
	const char * XMLPath = "E:\\各种测试时创建文件\\demo.xml";
	rec = CreateXML(XMLPath);
	if(rec == 0)
	{
		cout << "Xml文档创建成功..." << endl;
	}
	string userName = "ljx";
	string password = "wzdxhn";
	int gender = 1;
	string mobile = "wjl";
	string email = "sl";
	//增加节点
    const User user(userName,password,gender,mobile,email);
    insertXMLNode(XMLPath,user);

    //查看xml中节点
	User* temuser = NULL;
	temuser = queryUserByName(XMLPath,userName);
    temuser->show();

    //修改指定名字节点
	//User user1("ljx","00001111",0,"13995648666","[email protected]");
	//updateUser(XMLPath,&user1);
	
	

	//查看xml中节点
	//User* temuser1 = NULL;
	//temuser1 = queryUserByName(XMLPath,userName);
	//temuser1->show();
   
	//删除节点
	//deleteUserByName(XMLPath,userName);

	return 0;
}

代码中注释掉的部分都是可直接实现的功能,你可以自己试着运行一下(注:代码中的path路径为个人路径,不要照搬)

你可能感兴趣的:(tinyxml2)