#include "E:\\tinyxml2\\tinyxml2-master\\tinyxml2.h"
#include "E:\\tinyxml2\\tinyxml2-master\\tinyxml2.cpp"
#include <iostream>
using namespace tinyxml2;
using namespace std;
//创建xml文档
int CreateXML(const char * path)
{
const char * declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
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"<<endl;
return res;
}
XMLElement* root=doc.RootElement();
XMLElement* userNode = doc.NewElement("User");
userNode->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"<<endl;
return NULL;
}
else
{
cout << "打开xml文件成功" << endl;
}
XMLElement* root=doc.RootElement();
XMLElement* userNode=queryUserNodeByName(root,userName);
printf("开始读取节点\n");
if(userNode != NULL) //searched successfully
{
printf("读取到节点\n");
User* user = new User();
user->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"<<endl;
return false;
}
XMLElement* root = doc.RootElement();
XMLElement* userNode = queryUserNodeByName(root,user->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"<<endl;
return false;
}
XMLElement* root=doc.RootElement();
//doc.DeleteNode(root);//删除xml所有节点
XMLElement* userNode=queryUserNodeByName(root,userName);
if(userNode!=NULL)
{
userNode->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","1586666@qq.com");
//updateUser(XMLPath,&user1);
//查看xml中节点
//User* temuser1 = NULL;
//temuser1 = queryUserByName(XMLPath,userName);
//temuser1->show();
//删除节点
//deleteUserByName(XMLPath,userName);
return 0;
}
转载自https://blog.csdn.net/qq_37059136/article/details/81873369