C++ tinyxml2的使用说明

官网:http://www.grinninglizard.com/tinyxml2/index.html

SDK获取:https://github.com/leethomason/tinyxml2

相关使用说明:https://blog.csdn.net/K346K346/article/details/48750417

例子程序:https://download.csdn.net/download/qq_24127015/11057850

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "tinyxml2.h"
#include "User.h"
using namespace tinyxml2;

//xml文件:新建,增删改查节点
//变量名	描述	类型	长度(字节)	不为空	主键
//UserName	用户名	Vchar	3 - 20	Y	Y
//Password	密码	Char	32	Y	N
//Gender	性别	Int	1	N	N
//Mobile	电话	Char	11	N	N
//Email	电子邮箱	Varchar	1 - 50	N	N

//< ? xml version = "1.0" encoding = "UTF-8" standalone = "no" ? >
//<DBUSER>
//<User Name = ”lvlv” Password = ”123456”>
//<Gender>< / Gender>
//<Mobile >< / Mobile>
//<Email >< / Email >
//< / User>
//.
//.
//.
//<DBUSER>

//创建xml文件
int createXML(const char* xmlPath)
{
	const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
	XMLDocument doc;
	doc.Parse(declaration);//会覆盖xml所有内容
	//
	XMLElement *root = doc.NewElement("DBUSER");
	doc.InsertEndChild(root);
	return doc.SaveFile(xmlPath);
}

//增加xml节点
int insertXMLNode(const char* xmlPath, const User& user)
{
	XMLDocument doc;
	int res = doc.LoadFile(xmlPath);
	if (res != XML_SUCCESS)
	{
		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");
	gender->InsertEndChild(doc.NewText(to_string(user.gender).c_str()));
	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);
	return doc.SaveFile(xmlPath);
}

//查询xml文件的指定节点
XMLElement* queryUserNodeByName(XMLElement* root, const string& userName)
{
	XMLElement* userNode = root->FirstChildElement("User");
	while (userNode!=NULL)
	{
		if (userNode->Attribute("Name") == userName)
		{
			break;
		}
		userNode = userNode->NextSiblingElement();//下一个兄弟节点
	}
	return userNode;
}

//获取User信息
User* queryUserByName(const char* xmlPath, const string& userName)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != XML_SUCCESS)
	{
		cout << "load xml file failed" << endl;
		return NULL;
	}
	XMLElement* root = doc.RootElement();
	XMLElement* userNode = queryUserNodeByName(root, userName);
	if (userNode != NULL)
	{
		User* user = new User();
		user->userName = userNode->Attribute("Name");
		user->passWord = userNode->Attribute("Password");
		user->gender = std::stoi(userNode->FirstChildElement("gender")->GetText());
		XMLElement* pElement = userNode->FirstChildElement("Mobile");
		if(pElement!=NULL)
			user->mobile = pElement->GetText();
		pElement = userNode->FirstChildElement("Email");
		if (pElement != NULL)
			user->email = pElement->GetText();
		return user;
	}
	return NULL;
}

//修改xml文件的指定节点
bool updateUser(const char* xmlPath, User* user)
{
	XMLDocument doc;
	if (XML_SUCCESS != doc.LoadFile(xmlPath))
	{
		cout << "load xml file failed!" << endl;
		return false;
	}

	XMLElement* root = doc.RootElement();
	XMLElement* userNode = queryUserNodeByName(root,user->userName);
	if (userNode != NULL)
	{
		if (user->passWord != userNode->Attribute("Password"))
		{
			userNode->SetAttribute("Password", user->passWord.c_str());//修改节点属性
		}
		XMLElement* genderNode = userNode->FirstChildElement("gender");
		if (user->gender != stoi(genderNode->GetText()))
		{
			genderNode->SetText(to_string(user->gender).c_str());//修改节点内容
		}
		XMLElement* mobileNode = userNode->FirstChildElement("Mobile");
		if (user->mobile != mobileNode->GetText())
		{
			mobileNode->SetText(user->mobile.c_str());
		}
		XMLElement* emailElement = userNode->FirstChildElement("Email");
		if (user->email != emailElement->GetText())
		{
			emailElement->SetText(user->email.c_str());
		}
		if (doc.SaveFile(xmlPath) == 0)
			return true;
	}
	return false;
}

//删除xml文件的指定节点的信息
bool deleteUserByName(const char* xmlPath,const string& userName)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != XML_SUCCESS)
	{
		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) == XML_SUCCESS)
			return true;
	}
	return false;
}

//获取xml文件申明
bool getXMLDeclaration(const char* xmlPath, string& strDecl)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != XML_SUCCESS)
	{
		cout << "load xml file failed" << endl;
		return false;
	}
	XMLNode* decl = doc.FirstChild();
	if (NULL != decl)
	{
		XMLDeclaration* declaration = decl->ToDeclaration();
		if (NULL != declaration)
		{
			strDecl = declaration->Value();
			return true;
		}
	}
	return false;
}

//打印xml文件至标准输出
void paint(const char* xmlPath)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != XML_SUCCESS)
	{
		cout << "load xml file failed" << endl;
		return;
	}
	doc.Print();
}

//获取xml文件所有内容
void getXMLContent(const char* xmlPath, string& content)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != XML_SUCCESS)
	{
		cout << "load xml file failed" << endl;
		return;
	}
	XMLPrinter printer;
	doc.Print(&printer);
	content=printer.CStr();
}

int main()
{
	//创建xml
	createXML("example.xml");

	/*XMLDocument docXml;
	XMLError errXml = docXml.LoadFile("1.xml");
	if (XML_SUCCESS == errXml)
	{
		XMLElement* elmtRoot = docXml.RootElement();
		XMLElement *elmtUser = elmtRoot->FirstChildElement("User");
		XMLElement *elmtName = elmtUser->FirstChildElement("Name");
		if (elmtName)
		{
			const char* pContent = elmtName->GetText();
			printf("%s", pContent);
		}
		XMLElement *elmtAge = elmtName->NextSiblingElement();
		if (elmtAge)
		{
			const char* pContent = elmtAge->GetText();
			printf("%s", pContent);
		}
	}

	char* pXml = "<Item>Hello World</Item>";
	errXml = docXml.Parse(pXml);
	if (XML_SUCCESS == errXml)
	{
		XMLElement* elmtRoot = docXml.RootElement();
		if (elmtRoot)
		{
			const char* pContent = elmtRoot->GetText();
			printf("%s", pContent);
		}
	}*/
	//插入xml节点
	User user1("jordan", "123456", 1, "1248824924", "[email protected]");
	User user2("kobe", "4325345", 1, "234328947", "[email protected]");
	User user3("iverson", "43257897", 1, "894563245", "[email protected]");
	User user4("o'neal", "43257897", 1, "894563245", "[email protected]");
	User user5("curry", "45634324", 1, "676543534", "[email protected]");
	insertXMLNode("example.xml", user1);
	insertXMLNode("example.xml", user2);
	insertXMLNode("example.xml", user3);
	insertXMLNode("example.xml", user4);
	insertXMLNode("example.xml", user5);

	//查询xml节点
	User* kobeUser = queryUserByName("example.xml", "kobe");
	if (kobeUser != NULL)
	{
		cout << kobeUser->userName << "," << kobeUser->email << endl;
		delete kobeUser;
		kobeUser = NULL;
	}

	//修改xml文件的节点
	User user6("kobe", "45634324", 1, "13661901322", "[email protected]");
	updateUser("example.xml", &user6);

	deleteUserByName("example.xml", "kobe");

	//获取xml文件的声明
	string strDeclaration;
	getXMLDeclaration("example.xml", strDeclaration);
	cout << strDeclaration << endl;
	//获取xml文件的所有信息
	string content;
	getXMLContent("example.xml", content);
	cout << content << endl;
	//获取文件内容之后,解析字符串
	XMLDocument doc;
	doc.Parse(content.c_str());
	XMLElement* rootElement = doc.RootElement();
	const char* rootName = rootElement->Value();
	XMLElement* userNode;
	userNode = queryUserNodeByName(rootElement, "curry");
	if (userNode != NULL)
	{
		string email = userNode->FirstChildElement("Email")->GetText();
		cout << "curry's email is " << email << endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24127015/article/details/88792741