TinyXML2(学习记录)

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;
}

可以尝试学习~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值