使用Tinyxml2进行简单的xml操作

#include <iostream>
#include <cstring>

#include "tinyxml2.h"

using namespace tinyxml2;
using namespace std;

#define ParameterPath "test.xml"
#define PayRecordPath "payrecord.xml"

class User
{
public:
	User() 
	{
	}
	
	User(char *UserID, char *StartWay, char *StartTime, char *EndTime, char *PayMoney, char *Electricity)
	{
		this->UserID = UserID;
		this->StartWay = StartWay;
		this->StartTime = StartTime;
		this->EndTime = EndTime;
		this->PayMoney = PayMoney;
		this->Electricity = Electricity;
	}

	~User()
	{
	};

	char *UserID;
	char *StartWay;
	char *StartTime;
	char *EndTime;
	char *PayMoney;
	char *Electricity;

private:
	
};
// Function: Create a xml file
// Parameter:
//    xmlPath:xml文件路径
//    xmlName:xml文件名称
// Return:0,成功,非0,失败

int createXML(const char *xmlPath, const char *RootName)
{
	const char *declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
	XMLDocument doc;
	doc.Parse(declaration);//会覆盖xml所有内容

	XMLElement *root = doc.NewElement(RootName);
	doc.InsertEndChild(root);

	return doc.SaveFile(xmlPath);
}

// 增加新节点,用于增加充电记录
// Function: Insert a xml node
// Parameter:
//    xmlPath: xml文件路径
//    ParameterName: 参数名称
//    ParameterValue:参数值

int InsertPayRecord(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 *NewUser = doc.NewElement("UserID");
	NewUser->SetText(user.UserID);
	root->InsertEndChild(NewUser);

	XMLElement *UserAttribute = doc.NewElement("Attribute");
	UserAttribute->SetAttribute("StartWay", user.StartWay);
	UserAttribute->SetAttribute("StartTime", user.StartTime);
	UserAttribute->SetAttribute("EndTime", user.EndTime);
	UserAttribute->SetAttribute("PayMoney", user.PayMoney);
	UserAttribute->SetAttribute("Electricity", user.Electricity);
	NewUser->InsertEndChild(UserAttribute);
	
	return doc.SaveFile(xmlPath);
}

// Function: Edit a node value
// Parameter:
//    xmlPath:xml文件路径
//    ParameterName:参数名称
//    ParameterValue:参数值

int xmlEditNodeValue(const char *xmlPath, const char *ParameterName, const char *ParameterValue)
{
	XMLDocument doc;
	int res = doc.LoadFile(xmlPath);
	if (res != 0)
	{
		cout << "load xml file failed" << endl;
		return res;
	}
	XMLElement *root = doc.RootElement();

	if (XMLElement *node = root->FirstChildElement(ParameterName))
	{
		node->SetText(ParameterValue);
	}
	else 
	{
		XMLElement *newparameter = doc.NewElement(ParameterName);
		newparameter->SetText(ParameterValue);
		root->InsertEndChild(newparameter);
	}
	return doc.SaveFile(xmlPath);
}

// Function: Delete a node 
// Parameter:
//    xmlPath:xml文件路径
//    ParameterName:参数名称

bool xmlDeleteNode(const char *xmlPath, const char *ParameterName)
{
	XMLDocument doc;
	if (doc.LoadFile(xmlPath) != 0)
	{
		cout << "load xml file failed" << endl;
		return false;
	}
	XMLElement *root = doc.RootElement();
	if (XMLElement *node = root->FirstChildElement(ParameterName))
	{
		root->DeleteChild(node);
	}
	return doc.SaveFile(xmlPath);
}

// Function: Get a node value
// Parameter:
//    xmlPath:xml文件路径
//    ParameterName:参数名称

int xmlGetNodeValue(const char *xmlPath, const char *ParameterName)
{
	XMLDocument doc;
	int res = doc.LoadFile(xmlPath);
	if (res != 0)
	{
		cout << "load xml file failed" << endl;
		return -1;
	}
	XMLElement *root = doc.RootElement();

	XMLElement *node = root->FirstChildElement(ParameterName);

	return atoi(node->GetText());
}

// Function: Get a node value
// Parameter:
//    xmlPath:xml文件路径
//    ParameterName:参数名称

char *xmlGetNodeValue(const char *xmlPath, const char *ParameterName, int type)
{
	XMLDocument doc;
	int res = doc.LoadFile(xmlPath);
	if (res != 0)
	{
		cout << "load xml file failed" << endl;
		return NULL;
	}
	XMLElement *root = doc.RootElement();

	XMLElement *node = root->FirstChildElement(ParameterName);

	char temp[255];
	strcpy(temp, node->GetText());
	return temp;
}
由于最近太忙,晚些时间再完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值