使用Tinyxml2进行简单的xml操作

来源:互联网 发布:域名隐藏ip地址 编辑:程序博客网 时间:2024/05/18 00:03
#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;}
由于最近太忙,晚些时间再完善。
原创粉丝点击