C++使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù)
1 TinyXML2介紹
TinyXML2是一個(gè)輕量級(jí)的、開源的C++庫(kù),專門用于解析和生成XML文檔。它是原始TinyXML庫(kù)的一個(gè)升級(jí)版本,設(shè)計(jì)得更為高效和強(qiáng)大,同時(shí)保持了簡(jiǎn)單易用的特點(diǎn)。TinyXML2非常適合那些需要處理XML數(shù)據(jù),而又希望保持代碼簡(jiǎn)潔和執(zhí)行效率的應(yīng)用場(chǎng)景。
2 相關(guān)API
2.1 加載文件
/* * @brief 加載XML文件 * @param [IN] filename 文件名 * @return 返回XML_SUCCESS表示成功,返回其他值表示失敗 */ XMLError LoadFile(const char* filename);
2.2 獲取XML數(shù)據(jù)的根節(jié)點(diǎn)
/* * @brief 獲取XML數(shù)據(jù)的根節(jié)點(diǎn) * @return 成功返回XML數(shù)據(jù)的根節(jié)點(diǎn)對(duì)象,失敗返回NULL */ XMLElement* RootElement();
2.3 獲取XML數(shù)據(jù)的指定節(jié)點(diǎn)
/* * @brief 獲取XML數(shù)據(jù)的指定節(jié)點(diǎn) * @param [IN] name 指定節(jié)點(diǎn)名稱,不傳或傳0時(shí)獲取根節(jié)點(diǎn)對(duì)象 * @return 成功返回XML數(shù)據(jù)的指定節(jié)點(diǎn)對(duì)象,失敗返回NULL */ XMLElement* FirstChildElement(const char* name = 0);
2.4 獲取某個(gè)XML節(jié)點(diǎn)的文本內(nèi)容
/* * @brief 獲取XML元素的文本內(nèi)容 * @return 返回XML元素的文本內(nèi)容 */ const char* GetText() const;
2.5 獲取某個(gè)XMl節(jié)點(diǎn)的屬性
/* * @brief 獲取XML數(shù)據(jù)某個(gè)節(jié)點(diǎn)的屬性 * @param [IN] name 屬性名 * @return 返回某個(gè)節(jié)點(diǎn)屬性值 */ const char* Attribute(const char* name) const;
2.6 將新創(chuàng)建的節(jié)點(diǎn)添加到指定父節(jié)點(diǎn)
/* * @brief 將新創(chuàng)建的節(jié)點(diǎn)添加到指定父節(jié)點(diǎn) * @param [IN] addThis 要添加到父節(jié)點(diǎn)的對(duì)象 * @return c成功返回插入的節(jié)點(diǎn)自身,失敗返回NULL */ XMLNode* InsertEndChild(XMLNode* addThis);
2.7 創(chuàng)建新的XMLElement對(duì)象
/* * @brief 創(chuàng)建新的XMLElement對(duì)象 * @param [IN] name 新元素的標(biāo)簽名稱 * @return 成功返回指向新創(chuàng)建的XMLElement對(duì)象的指針, 失敗返回NULL */ XMLElement* NewElement(const char* name);
2.8 設(shè)置某個(gè)XML節(jié)點(diǎn)的文本內(nèi)容
/* * @brief 設(shè)置某個(gè)XML節(jié)點(diǎn)的文本內(nèi)容 * @param [IN] inText 要設(shè)置的文本內(nèi)容 */ void SetText(const char* inText);
2.9 保存XML數(shù)據(jù)到文件中
/* * @brief 保存XML數(shù)據(jù)到文件中 * @param [IN] filename 文件名 * @return 返回XML_SUCCESS表示成功,返回其他值表示失敗 */ XMLError SaveFile(const char* filename);
3 演示
3.1 解析XML數(shù)據(jù)
XML數(shù)據(jù)內(nèi)容
<?xml version="1.0"?> <msg> <msg_id>1</msg_id> <header hattr="http"> <type>Post</type> <host>127.0.0.1</host> </header> <body battr="base64"> <data>aGVsbG8=</data> </body> </msg>
代碼
#include <stdio.h> #include <iostream> #include <tinyxml2.h> int main(){ tinyxml2::XMLDocument xmlObj; // 解析數(shù)據(jù) //const char* xmlData = "<msg></msg>"; //tinyxml2::XMLError errCode = xmlObj.Parse(xmlData); // 解析文件 tinyxml2::XMLError errCode = xmlObj.LoadFile("xmldata.txt"); if(errCode != tinyxml2::XML_SUCCESS){ printf("LoadFile xml failed, errCode = %d\n", errCode); return -1; } // 獲取根節(jié)點(diǎn) tinyxml2::XMLElement* root = xmlObj.RootElement(); if (!root) { std::cout << "Invalid document structure." << std::endl; return -1; } // msg_id tinyxml2::XMLElement* msgidElem = root->FirstChildElement("msg_id"); if(msgidElem != NULL){ const char* cMsgid = msgidElem->GetText(); printf("cMsgid: %s\n", cMsgid); } // 遍歷header元素 for (tinyxml2::XMLElement* headerElem = root->FirstChildElement("header"); headerElem != nullptr; headerElem = headerElem->NextSiblingElement("header")) { // 獲取屬性 const char* cHattr = headerElem->Attribute("hattr"); printf("cHattr: %s\n", cHattr); // 獲取字段值 tinyxml2::XMLElement* typeElem = headerElem->FirstChildElement("type"); if(typeElem != NULL){ const char* cType = typeElem->GetText(); printf("cType: %s\n", cType); } tinyxml2::XMLElement* hostElem = headerElem->FirstChildElement("host"); if(hostElem != NULL){ const char* cHost = hostElem->GetText(); printf("cHost: %s\n", cHost); } } // 遍歷body元素 for (tinyxml2::XMLElement* bodyElem = root->FirstChildElement("body"); bodyElem != nullptr; bodyElem = bodyElem->NextSiblingElement("body")) { // 獲取屬性 const char* cBattr = bodyElem->Attribute("battr"); printf("cBattr: %s\n", cBattr); // 獲取字段值 tinyxml2::XMLElement* dataElem = bodyElem->FirstChildElement("data"); if(dataElem != NULL){ const char* cData = dataElem->GetText(); printf("cData: %s\n", cData); } } return 0; }
打印
3.2 生成XML數(shù)據(jù)
代碼
#include <stdio.h> #include <iostream> #include <tinyxml2.h> int main(){ // 初始化XML文檔對(duì)象 tinyxml2::XMLDocument doc; doc.InsertEndChild(doc.NewDeclaration()); // 添加XML聲明 // 創(chuàng)建根節(jié)點(diǎn) tinyxml2::XMLElement* root = doc.NewElement("msg"); doc.InsertEndChild(root); // 在根節(jié)點(diǎn)下添加msg_id節(jié)點(diǎn) tinyxml2::XMLElement* msgidElem = doc.NewElement("msg_id"); msgidElem->SetText("1"); root->InsertEndChild(msgidElem); // 在根節(jié)點(diǎn)下添加header節(jié)點(diǎn) tinyxml2::XMLElement* headerElem = doc.NewElement("header"); headerElem->SetAttribute("hattr", "http"); root->InsertEndChild(headerElem); // header節(jié)點(diǎn)下添加type節(jié)點(diǎn) tinyxml2::XMLElement* typeElem = doc.NewElement("type"); typeElem->SetText("Post"); headerElem->InsertEndChild(typeElem); // header節(jié)點(diǎn)下添加host節(jié)點(diǎn) tinyxml2::XMLElement* hostElem = doc.NewElement("host"); hostElem->SetText("127.0.0.1"); headerElem->InsertEndChild(hostElem); // 在根節(jié)點(diǎn)下添加body節(jié)點(diǎn) tinyxml2::XMLElement* bodyElem = doc.NewElement("body"); bodyElem->SetAttribute("battr", "base64"); root->InsertEndChild(bodyElem); // body節(jié)點(diǎn)下添加data節(jié)點(diǎn) tinyxml2::XMLElement* dataElem = doc.NewElement("data"); dataElem->SetText("aGVsbG8="); bodyElem->InsertEndChild(dataElem); // 保存到文件 doc.SaveFile("example.xml"); return 0; }
生成的XML數(shù)據(jù)
<?xml version="1.0" encoding="UTF-8"?> <msg> <msg_id>1</msg_id> <header hattr="http"> <type>Post</type> <host>127.0.0.1</host> </header> <body battr="base64"> <data>aGVsbG8=</data> </body> </msg>
以上就是C++使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于C++ TinyXML2解析和生成XML數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++異步數(shù)據(jù)交換實(shí)現(xiàn)方法介紹
這篇文章主要介紹了C++異步數(shù)據(jù)交換實(shí)現(xiàn)方法,異步數(shù)據(jù)交換,除了阻塞函數(shù) send() 和 recv() 之外,Boost.MPI 還支持與成員函數(shù) isend() 和 irecv() 的異步數(shù)據(jù)交換2022-11-11c++項(xiàng)目構(gòu)成從cmake使用基礎(chǔ)詳解
這篇文章主要為大家介紹了c++項(xiàng)目構(gòu)成,從cmake使用基礎(chǔ)開始為大家講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11c++回溯法解決1到9之間插入加減或空使運(yùn)算結(jié)果為100
編寫一個(gè)在1,2,…,9(順序不能變)數(shù)字之間插入+或-或什么都不插入,使得計(jì)算結(jié)果總是100的程序,并輸出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 1002021-10-10C語(yǔ)言中isdigit()函數(shù)和isxdigit()函數(shù)的用法
這篇文章主要介紹了C語(yǔ)言中isdigit()函數(shù)和isxdigit()函數(shù)的用法,用來(lái)判斷字符師傅為阿拉伯?dāng)?shù)字和16進(jìn)制數(shù)字,需要的朋友可以參考下2015-08-08