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++中vector的模擬實(shí)現(xiàn)實(shí)例詳解
vector是表示可變大小數(shù)組的序列容器,它也采用連續(xù)存儲(chǔ)空間來(lái)存儲(chǔ)元素,因此可以采用下標(biāo)對(duì)vector的元素進(jìn)行訪問,這篇文章主要給大家介紹了關(guān)于C++中vector模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-11-11
win32下進(jìn)程間通信(共享內(nèi)存)實(shí)例分析
這篇文章主要介紹了win32下進(jìn)程間通信(共享內(nèi)存)實(shí)例分析,對(duì)win32應(yīng)用程序及進(jìn)程的原理做了較為深入的剖析,需要的朋友可以參考下2014-07-07
C++ opencv ffmpeg圖片序列化實(shí)現(xiàn)代碼解析
這篇文章主要介紹了C++ opencv ffmpeg圖片序列化實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
C語(yǔ)言的變量與常量 字符字符串與轉(zhuǎn)義字符詳解
這篇文章主要介紹了詳解C語(yǔ)言的變量與常量 字符字符串與轉(zhuǎn)義字符,包括其之間的區(qū)別是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2021-10-10

