最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++使用tinyxml庫處理XML文件

 更新時間:2023年07月27日 08:37:35   作者:大白曰夢想家  
TinyXML是一個開源的解析XML的解析庫,能夠用于C++,能夠在Windows或Linux中編譯,這個解析庫的模型通過解析XML文件,然后在內(nèi)存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹,本文為大家介紹的是使用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("還喜歡學習編程");		// 創(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>還喜歡學習編程</Description>
			TiXmlNode* des = Description->FirstChild();	// 獲取元素指針		// 獲得存儲 "還喜歡學習編程" 的指針
			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");
			// 刪除當前節(jié)點,刪除后pItem為NULL,如果再繼續(xù)使用它會報錯
			Root->RemoveChild(pItem->ToElement());
			// 這里要進行賦值回來
			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)文章

  • C語言遞歸思想實現(xiàn)漢諾塔詳解

    C語言遞歸思想實現(xiàn)漢諾塔詳解

    大家好,本篇文章主要講的是C語言遞歸思想實現(xiàn)漢諾塔詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • matlab模擬退火算法單約束車間流水線調(diào)度解決實現(xiàn)及示例

    matlab模擬退火算法單約束車間流水線調(diào)度解決實現(xiàn)及示例

    這篇文章主要為大家介紹了matlab模擬退火算法求解單約束車間流水線調(diào)度的實現(xiàn)及示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • C++Zip壓縮解壓縮示例(支持遞歸壓縮)

    C++Zip壓縮解壓縮示例(支持遞歸壓縮)

    C++Zip壓縮解壓縮示例,用第三方函數(shù)封裝而成,支持 UNCODE, ANSCII、支持壓縮文件夾、支持遞歸壓縮
    2013-11-11
  • C++如何判斷一個數(shù)字是否為質(zhì)數(shù)

    C++如何判斷一個數(shù)字是否為質(zhì)數(shù)

    這篇文章主要為大家詳細介紹了C++如何判斷一個數(shù)字是否為質(zhì)數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 關(guān)于C++使用指針 堆和棧的區(qū)別分析

    關(guān)于C++使用指針 堆和棧的區(qū)別分析

    本篇文章小編為大家介紹,關(guān)于C++使用指針 堆和棧的區(qū)別分析。需要的朋友參考下
    2013-04-04
  • OpenCV圖像處理之直方圖比較方法詳解

    OpenCV圖像處理之直方圖比較方法詳解

    直方圖比較是對輸入的兩張圖像進行計算得到直方圖H1與H2,歸一化到相同的尺度空間,然后可以通過計算H1與H2的之間的距離得到兩個直方圖的相似程度,進而比較圖像本身的相似程度。本文將為大家詳細講講直方圖比較的實現(xiàn)方法,需要的可以參考一下
    2022-09-09
  • c語言定時器示例分享

    c語言定時器示例分享

    在linux下開發(fā),使用的是C語言。適用于需要定時的軟件開發(fā),以系統(tǒng)真實的時間來計算,它送出SIGALRM信號。每隔一秒定時一次
    2014-04-04
  • 二進制、八進制?、十進制、十六進制之間轉(zhuǎn)換的原理詳解

    二進制、八進制?、十進制、十六進制之間轉(zhuǎn)換的原理詳解

    本文介紹了進制的概念及其在C語言編程中的應(yīng)用,進制是進位制的簡稱,描述了數(shù)值在不同進制下的表示方法,常見的進制包括二進制、八進制和十六進制,二進制使用0和1表示,八進制使用0-7數(shù)字表示,十六進制使用0-9和A-F表示,文章還介紹了如何在不同進制之間進行轉(zhuǎn)換
    2024-11-11
  • C語言數(shù)組的各種操作梳理

    C語言數(shù)組的各種操作梳理

    數(shù)組是一組有序的數(shù)據(jù)的集合,數(shù)組中元素類型相同,由數(shù)組名和下標唯一地確定,數(shù)組中數(shù)據(jù)不僅數(shù)據(jù)類型相同,而且在計算機內(nèi)存里連續(xù)存放,地址編號最低的存儲單元存放數(shù)組的起始元素,地址編號最高的存儲單元存放數(shù)組的最后一個元素
    2022-04-04
  • 正確理解C++的構(gòu)造函數(shù)和析構(gòu)函數(shù)

    正確理解C++的構(gòu)造函數(shù)和析構(gòu)函數(shù)

    在C++的學習中,可以把類當作一個模具,類實例化出來的對象就是根據(jù)這個模具所產(chǎn)生的實體,對象看作是自己創(chuàng)建的一個新的數(shù)據(jù)類型。本文主要介紹了類對象通過拷貝函數(shù)進行初始化,分析類對象的內(nèi)存模型,以及通過this指針實現(xiàn)更復(fù)雜的功能。最后介紹了析構(gòu)函數(shù)的基礎(chǔ)知識
    2021-06-06

最新評論

兰州市| 屯门区| 吉林省| 疏附县| 马鞍山市| 珲春市| 清苑县| 聂荣县| 马尔康县| 汝城县| 西安市| 同江市| 宿松县| 尚义县| 甘肃省| 巩义市| 屏山县| 涟源市| 荆门市| 溧水县| 措勤县| 怀化市| 怀柔区| 朝阳县| 张家川| 衡阳县| 镇原县| 江陵县| 金湖县| 夏邑县| 栾城县| 黄冈市| 德昌县| 会泽县| 乌拉特中旗| 渝中区| 开鲁县| 西乌珠穆沁旗| 三原县| 祥云县| 张家口市|