1.CMakeList.txt:
cmake_minimum_required(VERSION 2.8.0)
project(info)
add_executable(${PROJECT_NAME}
test.cpp
)
target_link_libraries(${PROJECT_NAME}
pthread
tinyxml2
-lm
)
2.整段代码:
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <thread>
#include <unistd.h>
#include <memory>
#include <stdio.h>
#include <tinyxml2.h>
#include <cstdlib>
#include <math.h>
using namespace std;
const char* xmlpath = "/home/li/Desktop/test/1.xml";//本地路径
//如何创建xml文件看此函数
int createXML(const char* xmlPath)
{
const char* declaration ="<?xml version=\"2.0\" encoding=\"UTF-8\" use=\"no\"?>";
tinyxml2::XMLDocument doc;
doc.Parse(declaration);
tinyxml2::XMLElement* root=doc.NewElement("BUS");
doc.InsertEndChild(root);
return doc.SaveFile(xmlPath);
}
//自定义的类
class User
{
public:
User()
{
gender=0;
};
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;
};
string userName;
string password;
int gender;
string mobile;
string email;
};
//往xml中插入node看此函数
int insertXMLNode(const char* xmlPath,const User& user)
{
tinyxml2::XMLDocument doc;
int res=doc.LoadFile(xmlPath);
if(res!=0)
{
cout<<"load xml file failed"<<endl;
return res;
}
tinyxml2::XMLElement* root=doc.RootElement();
tinyxml2::XMLElement* userNode = doc.NewElement("User");
userNode->SetAttribute("Name", user.userName.c_str());
userNode->SetAttribute("Password", user.password.c_str());
userNode->SetAttribute("Gender", "1");
root->InsertEndChild(userNode);
tinyxml2::XMLElement* mobile = doc.NewElement("Mobile");
mobile->InsertEndChild(doc.NewText(user.mobile.c_str()));
userNode->InsertEndChild(mobile);
tinyxml2::XMLElement* email = doc.NewElement("Email");
email->InsertEndChild(doc.NewText(user.email.c_str()));
userNode->InsertEndChild(email);
tinyxml2::XMLElement* age = doc.NewElement("Age");
age->InsertEndChild(doc.NewText("30"));
userNode->InsertEndChild(age);
return doc.SaveFile(xmlPath);
}
//xml中的信息打印看此函数
void print(const char* xmlPath)
{
tinyxml2::XMLDocument doc;
if(doc.LoadFile(xmlPath)!=0)
{
cout<<"load xml file failed"<<endl;
return;
}
doc.Print();
}
//解析子节点看此函数
void ParseSubNode(tinyxml2::XMLElement* xml_node)
{
auto node = xml_node->FirstChildElement();
if(node)
{
std::string node_value = node->Value();
std::cout << "node_value" << node_value << std::endl;
if(node_value == "User")
{
std::string name = node->Attribute("Name");
std::cout << "name" << name << std::endl;
std::string password = node->Attribute("Password");
std::cout << "password" << password << std::endl;
}
ParseSubNode(node);
}
node = xml_node->NextSiblingElement();
if(node)
{
std::string node_value_next = node->Value();
std::cout << "node_value_next" << node_value_next << std::endl;
ParseSubNode(node);
}
}
bool getXMLFirst(const char* xmlPath)
{
tinyxml2::XMLDocument doc;
if(doc.LoadFile(xmlPath)!=0)
{
cout<<"load xml file failed"<<endl;
return false;
}
tinyxml2::XMLElement* root = doc.RootElement();
ParseSubNode(root);
}
int main(void)
{
User user("li", "123456", 1, "120528", "521@qq.com");
std::cout << createXML(xmlpath) << std::endl;
std::cout << insertXMLNode(xmlpath, user) << std::endl;
print(xmlpath);
getXMLFirst(xmlpath);
return 0;
}
可以尝试学习~