C++使用tinyxml庫處理XML文件
一、下載tinyxml
https://sourceforge.net/projects/tinyxml/
打開網(wǎng)頁,點擊Download下載后,里面包含了:tinyxml.h、tinystr.h、tinyxml.cpp、tinystr.cpp、tinyxmlparser.cpp、tinyxmlerror.cpp等文件。
二、創(chuàng)建一個XML文件
#include<stdio.h> #include "tinyxml.h" using namespace std; int main(){ TiXmlDocument* tinyXmlDoc = new TiXmlDocument();//創(chuàng)建一個XML TiXmlDeclaration* tinyXmlDeclare = new TiXmlDeclaration("1.0", "utf-8", "指定是否在XML中包含獨立性聲明");//創(chuàng)建頭部信息 tinyXmlDoc->LinkEndChild(tinyXmlDeclare);// 插入文檔類中 TiXmlElement* Root = new TiXmlElement("Root");// 創(chuàng)建根節(jié)點的名稱 tinyXmlDoc->LinkEndChild(Root); // 把根節(jié)點插入到XML中 TiXmlElement *Child_one = new TiXmlElement("Child_one");//添加子節(jié)點Child_one Child_one->SetAttribute("Name", "大明"); //設(shè)置節(jié)點的屬性 Child_one->SetAttribute("Gender", "Male"); Child_one->SetAttribute("Age", "40"); TiXmlText *Hobby = new TiXmlText("愛好:游泳"); // 創(chuàng)建文本 Child_one->LinkEndChild(Hobby); // 給Child_one節(jié)點添加文本 TiXmlElement *Sunzi_one = new TiXmlElement("Sunzi_one"); //創(chuàng)建Sunzi_one節(jié)點 Sunzi_one->SetAttribute("Name", "小明"); //設(shè)置節(jié)點的屬性 Sunzi_one->SetAttribute("Gender", "Male"); Sunzi_one->SetAttribute("Age", "16"); TiXmlText *Sunzione_hobby = new TiXmlText("愛好:象棋"); // 創(chuàng)建文本 Sunzi_one->LinkEndChild(Sunzione_hobby); // 給Sunzi_one節(jié)點添加文本 Child_one->LinkEndChild(Sunzi_one); // Sunzi_one節(jié)點插入到Child_one節(jié)點下 TiXmlElement *Sunzi_two = new TiXmlElement("Sunzi_two"); //創(chuàng)建Sunzi_two節(jié)點 Sunzi_two->SetAttribute("Name", "小創(chuàng)"); //設(shè)置節(jié)點的屬性 Sunzi_two->SetAttribute("Gender", "Male"); Sunzi_two->SetAttribute("Age", "14"); TiXmlText *Sunzitwo_hobby = new TiXmlText("愛好:武術(shù)"); // 創(chuàng)建文本 Sunzi_two->LinkEndChild(Sunzitwo_hobby); // 給Sunzi_two節(jié)點添加文本 Child_one->LinkEndChild(Sunzi_two); // Sunzi_two節(jié)點插入到Child_one節(jié)點下 TiXmlElement *Sunzi_three = new TiXmlElement("Sunzi_three"); //創(chuàng)建Sunzi_three節(jié)點 Sunzi_three->SetAttribute("Name", "小花"); //設(shè)置節(jié)點的屬性 Sunzi_three->SetAttribute("Gender", "Female"); Sunzi_three->SetAttribute("Age", "13"); TiXmlText *Sunzithree_hobby = new TiXmlText("愛好:跳舞"); // 創(chuàng)建文本 Sunzi_three->LinkEndChild(Sunzithree_hobby); // 給Sunzi_three節(jié)點添加文本 Child_one->LinkEndChild(Sunzi_three); // Sunzi_three節(jié)點插入到Child_one節(jié)點下 Root->LinkEndChild(Child_one); //子節(jié)點Child_one插入到Root節(jié)點下 //保存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(); //控制臺打印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é)點 Root = tinyXmlDoc->RootElement(); TiXmlElement *Child_two = new TiXmlElement("Child_two"); // 插入屬性 Child_two->SetAttribute("Name", "大華"); //設(shè)置節(jié)點的屬性 Child_two->SetAttribute("Gender", "Male"); Child_two->SetAttribute("Age", "38"); TiXmlText *Hobby = new TiXmlText("愛好圍棋"); // 創(chuàng)建文本 Child_two->LinkEndChild(Hobby); // 給Child_two節(jié)點添加文本 TiXmlElement *Description = new TiXmlElement("Description"); TiXmlText *descriptionText = new TiXmlText("還喜歡學(xué)習(xí)編程"); // 創(chuàng)建文本 Description->LinkEndChild(descriptionText); // 給Description節(jié)點添加文本 Child_two->LinkEndChild(Description); // 插入到Book1節(jié)點下 Root->LinkEndChild(Child_two); // 插入到根節(jié)點下 // 保存到文件 bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml"); if (result == true) printf("XML文件寫入成功!\n"); else printf("XML文件寫入失敗!\n"); tinyXmlDoc->Print(); //控制臺打印XML system("pause"); return 0; }
運行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é)點 Root = tinyXmlDoc->RootElement(); // 循環(huán)查找Child_two節(jié)點,修改屬性值 TiXmlElement *Child_two = new TiXmlElement("Child_two"); TiXmlElement* pItem = Root->FirstChildElement("Child_two"); for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two")) { // 找到屬性Name=大華的節(jié)點 if (strcmp(pItem->Attribute("Name"), "大華") == 0) { pItem->SetAttribute("Age", "39"); // 設(shè)置Child_two的子節(jié)點Description的值 TiXmlElement* Description = pItem->FirstChildElement("Description"); // 獲得<Description>還喜歡學(xué)習(xí)編程</Description> TiXmlNode* des = Description->FirstChild(); // 獲取元素指針 // 獲得存儲 "還喜歡學(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(); //控制臺打印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");// 定義一個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é)點 TiXmlElement* Root = new TiXmlElement("Root"); if (Root){ Root = tinyXmlDoc->RootElement(); } // 解析Child_one節(jié)點 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:在同一級元素中查找下一個指定名字的元素 { // 解析Child_two節(jié)點的屬性 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é)點 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");// 定義一個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é)點 TiXmlElement* Root = new TiXmlElement("Root"); if (Root){ Root = tinyXmlDoc->RootElement(); } // 刪除Sunzi_two節(jié)點的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é)點 if (strcmp(pItem->Attribute("Name"), "小創(chuàng)") == 0) { // 刪除Gender屬性 pItem->RemoveAttribute("Gender"); } } //刪除Child_two節(jié)點中屬性Name="大華"的節(jié)點 pItem = Root->FirstChildElement("Child_two"); for (; pItem != NULL; ) { // 找到屬性Name="大華"的節(jié)點 if (strcmp(pItem->Attribute("Name"), "大華") == 0) { // 提前存儲刪除節(jié)點的下一個節(jié)點 TiXmlElement* temporary = pItem->NextSiblingElement("Child_two"); // 刪除當(dāng)前節(jié)點,刪除后pItem為NULL,如果再繼續(xù)使用它會報錯 Root->RemoveChild(pItem->ToElement()); // 這里要進(jìn)行賦值回來 pItem = temporary; } else { // 尋找下一個Child_two節(jié)點 pItem = pItem->NextSiblingElement("Child_two"); } } // 保存到文件 bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml"); if (result == true) printf("文件寫入成功!\n"); else printf("文件寫入失??!\n"); // 打印出來看看 tinyXmlDoc->Print(); printf("\n"); system("pause"); }
總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了tinyxml的使用,包括創(chuàng)建XML文件及增刪改查的操作演示。
到此這篇關(guān)于C++使用tinyxml庫處理XML文件的文章就介紹到這了,更多相關(guān)C++ tinyxml處理XML文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenGL實現(xiàn)不規(guī)則區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了OpenGL實現(xiàn)不規(guī)則區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02C語言詳細(xì)分析常見字符串函數(shù)與模擬實現(xiàn)
字符串函數(shù)(String?processing?function)也叫字符串處理函數(shù),指的是編程語言中用來進(jìn)行字符串處理的函數(shù),如C,pascal,Visual以及LotusScript中進(jìn)行字符串拷貝,計算長度,字符查找等的函數(shù)2022-03-03Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)通用數(shù)據(jù)庫清理
項目如果需要存儲很多日志記錄比如運行日志,時間長了記錄數(shù)量非常多,數(shù)據(jù)庫體積不斷增大,對應(yīng)數(shù)據(jù)庫表的增刪改查的效率不斷降低,因此需要將早期的數(shù)據(jù)清理。本文將詳細(xì)介紹一下通用數(shù)據(jù)庫清理的實現(xiàn),需要的可以參考一下2022-02-02