C++使用tinyxml庫(kù)處理XML文件
一、下載tinyxml
https://sourceforge.net/projects/tinyxml/
打開(kāi)網(wǎng)頁(yè),點(diǎn)擊Download下載后,里面包含了:tinyxml.h、tinystr.h、tinyxml.cpp、tinystr.cpp、tinyxmlparser.cpp、tinyxmlerror.cpp等文件。
二、創(chuàng)建一個(gè)XML文件
#include<stdio.h>
#include "tinyxml.h"
using namespace std;
int main(){
TiXmlDocument* tinyXmlDoc = new TiXmlDocument();//創(chuàng)建一個(gè)XML
TiXmlDeclaration* tinyXmlDeclare = new TiXmlDeclaration("1.0", "utf-8", "指定是否在XML中包含獨(dú)立性聲明");//創(chuàng)建頭部信息
tinyXmlDoc->LinkEndChild(tinyXmlDeclare);// 插入文檔類中
TiXmlElement* Root = new TiXmlElement("Root");// 創(chuàng)建根節(jié)點(diǎn)的名稱
tinyXmlDoc->LinkEndChild(Root); // 把根節(jié)點(diǎn)插入到XML中
TiXmlElement *Child_one = new TiXmlElement("Child_one");//添加子節(jié)點(diǎn)Child_one
Child_one->SetAttribute("Name", "大明"); //設(shè)置節(jié)點(diǎn)的屬性
Child_one->SetAttribute("Gender", "Male");
Child_one->SetAttribute("Age", "40");
TiXmlText *Hobby = new TiXmlText("愛(ài)好:游泳"); // 創(chuàng)建文本
Child_one->LinkEndChild(Hobby); // 給Child_one節(jié)點(diǎn)添加文本
TiXmlElement *Sunzi_one = new TiXmlElement("Sunzi_one"); //創(chuàng)建Sunzi_one節(jié)點(diǎn)
Sunzi_one->SetAttribute("Name", "小明"); //設(shè)置節(jié)點(diǎn)的屬性
Sunzi_one->SetAttribute("Gender", "Male");
Sunzi_one->SetAttribute("Age", "16");
TiXmlText *Sunzione_hobby = new TiXmlText("愛(ài)好:象棋"); // 創(chuàng)建文本
Sunzi_one->LinkEndChild(Sunzione_hobby); // 給Sunzi_one節(jié)點(diǎn)添加文本
Child_one->LinkEndChild(Sunzi_one); // Sunzi_one節(jié)點(diǎn)插入到Child_one節(jié)點(diǎn)下
TiXmlElement *Sunzi_two = new TiXmlElement("Sunzi_two"); //創(chuàng)建Sunzi_two節(jié)點(diǎn)
Sunzi_two->SetAttribute("Name", "小創(chuàng)"); //設(shè)置節(jié)點(diǎn)的屬性
Sunzi_two->SetAttribute("Gender", "Male");
Sunzi_two->SetAttribute("Age", "14");
TiXmlText *Sunzitwo_hobby = new TiXmlText("愛(ài)好:武術(shù)"); // 創(chuàng)建文本
Sunzi_two->LinkEndChild(Sunzitwo_hobby); // 給Sunzi_two節(jié)點(diǎn)添加文本
Child_one->LinkEndChild(Sunzi_two); // Sunzi_two節(jié)點(diǎn)插入到Child_one節(jié)點(diǎn)下
TiXmlElement *Sunzi_three = new TiXmlElement("Sunzi_three"); //創(chuàng)建Sunzi_three節(jié)點(diǎn)
Sunzi_three->SetAttribute("Name", "小花"); //設(shè)置節(jié)點(diǎn)的屬性
Sunzi_three->SetAttribute("Gender", "Female");
Sunzi_three->SetAttribute("Age", "13");
TiXmlText *Sunzithree_hobby = new TiXmlText("愛(ài)好:跳舞"); // 創(chuàng)建文本
Sunzi_three->LinkEndChild(Sunzithree_hobby); // 給Sunzi_three節(jié)點(diǎn)添加文本
Child_one->LinkEndChild(Sunzi_three); // Sunzi_three節(jié)點(diǎn)插入到Child_one節(jié)點(diǎn)下
Root->LinkEndChild(Child_one); //子節(jié)點(diǎn)Child_one插入到Root節(jié)點(diǎn)下
//保存xml文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");//將tinyXmlDoc寫入xml文件
if (result == true) printf("XML文件寫入成功!\n");
else printf("XML文件寫入失敗!\n");
tinyXmlDoc->Print(); //控制臺(tái)打印XML
system("pause");
return 0;
} 
三、XML文件增加數(shù)據(jù)
#include<stdio.h>
#include "tinyxml.h"
using namespace std;
int main(){
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");// 讀取xml文件
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
TiXmlDeclaration *pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration(); // 讀取xml的頭部信息
if (pDeclar != NULL) {
printf("頭部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
}
TiXmlElement *Root = new TiXmlElement("Root");//獲取根節(jié)點(diǎn)
Root = tinyXmlDoc->RootElement();
TiXmlElement *Child_two = new TiXmlElement("Child_two");
// 插入屬性
Child_two->SetAttribute("Name", "大華"); //設(shè)置節(jié)點(diǎn)的屬性
Child_two->SetAttribute("Gender", "Male");
Child_two->SetAttribute("Age", "38");
TiXmlText *Hobby = new TiXmlText("愛(ài)好圍棋"); // 創(chuàng)建文本
Child_two->LinkEndChild(Hobby); // 給Child_two節(jié)點(diǎn)添加文本
TiXmlElement *Description = new TiXmlElement("Description");
TiXmlText *descriptionText = new TiXmlText("還喜歡學(xué)習(xí)編程"); // 創(chuàng)建文本
Description->LinkEndChild(descriptionText); // 給Description節(jié)點(diǎn)添加文本
Child_two->LinkEndChild(Description); // 插入到Book1節(jié)點(diǎn)下
Root->LinkEndChild(Child_two); // 插入到根節(jié)點(diǎn)下
// 保存到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("XML文件寫入成功!\n");
else printf("XML文件寫入失敗!\n");
tinyXmlDoc->Print(); //控制臺(tái)打印XML
system("pause");
return 0;
} 運(yùn)行3次后,結(jié)果如下圖:

四、修改XML文件
#include<stdio.h>
#include "tinyxml.h"
using namespace std;
int main(){
// 讀取xml文件
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
TiXmlDeclaration *pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();// 讀取xml的頭部信息
if (pDeclar != NULL) {
printf("頭部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
}
TiXmlElement *Root = new TiXmlElement("Root"); //獲取根節(jié)點(diǎn)
Root = tinyXmlDoc->RootElement();
// 循環(huán)查找Child_two節(jié)點(diǎn),修改屬性值
TiXmlElement *Child_two = new TiXmlElement("Child_two");
TiXmlElement* pItem = Root->FirstChildElement("Child_two");
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two")) {
// 找到屬性Name=大華的節(jié)點(diǎn)
if (strcmp(pItem->Attribute("Name"), "大華") == 0) {
pItem->SetAttribute("Age", "39");
// 設(shè)置Child_two的子節(jié)點(diǎn)Description的值
TiXmlElement* Description = pItem->FirstChildElement("Description"); // 獲得<Description>還喜歡學(xué)習(xí)編程</Description>
TiXmlNode* des = Description->FirstChild(); // 獲取元素指針 // 獲得存儲(chǔ) "還喜歡學(xué)習(xí)編程" 的指針
des->SetValue("最討厭編程"); // 重新為其設(shè)置值
}
}
// 保存xml到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("XML文件寫入成功!\n");
else printf("XML文件寫入失?。n");
tinyXmlDoc->Print(); //控制臺(tái)打印XML
system("pause");
return 0;
} 
五、解析XML文件
#include<stdio.h>
#include "tinyxml.h"
using namespace std;
int main()
{
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");// 定義一個(gè)TiXmlDocument類指針
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
//讀取xml的頭部信息
TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("Header info,version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
printf("\n");
}
//獲取文件根節(jié)點(diǎn)
TiXmlElement* Root = new TiXmlElement("Root");
if (Root){
Root = tinyXmlDoc->RootElement();
}
// 解析Child_one節(jié)點(diǎn)
TiXmlElement* Child_one = Root->FirstChildElement("Child_one");
if (Child_one){
printf("Child_one : %s\n", Child_one->GetText());
printf("\n");
}
TiXmlElement* pItem = Root->FirstChildElement("Child_two"); // 函數(shù)FirstChildElement():找到指定名字的元素
if (pItem){
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two"))// 函數(shù)NextSiblingElement:在同一級(jí)元素中查找下一個(gè)指定名字的元素
{
// 解析Child_two節(jié)點(diǎn)的屬性
printf("Child_two: \n");
printf("Name = %s\n", pItem->Attribute("Name"));
printf("Gender = %s\n", pItem->Attribute("Gender"));
printf("Age = %s\n", pItem->Attribute("Age"));
// 解析Child_two的子節(jié)點(diǎn)
TiXmlElement* Description = pItem->FirstChildElement("Description");
if(Description){
printf("Description = %s\n", Description->GetText());
}
printf("\n");
}
}
printf("\n");
system("pause");
}
六、XML文件刪除數(shù)據(jù)
#include<stdio.h>
#include "tinyxml.h"
using namespace std;
int main()
{
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");// 定義一個(gè)TiXmlDocument類指針
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
//讀取xml的頭部信息
TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("Header info,version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
printf("\n");
}
//獲取文件根節(jié)點(diǎn)
TiXmlElement* Root = new TiXmlElement("Root");
if (Root){
Root = tinyXmlDoc->RootElement();
}
// 刪除Sunzi_two節(jié)點(diǎn)的Gender屬性
TiXmlElement* Child_one = Root->FirstChildElement("Child_one");
TiXmlElement* pItem = Child_one->FirstChildElement("Sunzi_two");
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Sunzi_two")) {
// 找到屬性Name=小創(chuàng)的節(jié)點(diǎn)
if (strcmp(pItem->Attribute("Name"), "小創(chuàng)") == 0) {
// 刪除Gender屬性
pItem->RemoveAttribute("Gender");
}
}
//刪除Child_two節(jié)點(diǎn)中屬性Name="大華"的節(jié)點(diǎn)
pItem = Root->FirstChildElement("Child_two");
for (; pItem != NULL; ) {
// 找到屬性Name="大華"的節(jié)點(diǎn)
if (strcmp(pItem->Attribute("Name"), "大華") == 0) {
// 提前存儲(chǔ)刪除節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
TiXmlElement* temporary = pItem->NextSiblingElement("Child_two");
// 刪除當(dāng)前節(jié)點(diǎn),刪除后pItem為NULL,如果再繼續(xù)使用它會(huì)報(bào)錯(cuò)
Root->RemoveChild(pItem->ToElement());
// 這里要進(jìn)行賦值回來(lái)
pItem = temporary;
} else {
// 尋找下一個(gè)Child_two節(jié)點(diǎn)
pItem = pItem->NextSiblingElement("Child_two");
}
}
// 保存到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("文件寫入成功!\n");
else printf("文件寫入失敗!\n");
// 打印出來(lái)看看
tinyXmlDoc->Print();
printf("\n");
system("pause");
}
總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了tinyxml的使用,包括創(chuàng)建XML文件及增刪改查的操作演示。
到此這篇關(guān)于C++使用tinyxml庫(kù)處理XML文件的文章就介紹到這了,更多相關(guān)C++ tinyxml處理XML文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
C語(yǔ)言實(shí)現(xiàn)學(xué)生籍貫信息記錄簿
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生籍貫信息記錄簿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C語(yǔ)言詳細(xì)分析常見(jiàn)字符串函數(shù)與模擬實(shí)現(xiàn)
字符串函數(shù)(String?processing?function)也叫字符串處理函數(shù),指的是編程語(yǔ)言中用來(lái)進(jìn)行字符串處理的函數(shù),如C,pascal,Visual以及LotusScript中進(jìn)行字符串拷貝,計(jì)算長(zhǎng)度,字符查找等的函數(shù)2022-03-03
Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)清理
項(xiàng)目如果需要存儲(chǔ)很多日志記錄比如運(yùn)行日志,時(shí)間長(zhǎng)了記錄數(shù)量非常多,數(shù)據(jù)庫(kù)體積不斷增大,對(duì)應(yīng)數(shù)據(jù)庫(kù)表的增刪改查的效率不斷降低,因此需要將早期的數(shù)據(jù)清理。本文將詳細(xì)介紹一下通用數(shù)據(jù)庫(kù)清理的實(shí)現(xiàn),需要的可以參考一下2022-02-02

