C++ uses the tinyxml library to process XML files


foreword

TinyXML is an open source parsing library for parsing XML, which can be used in C++ and can be compiled in Windows or Linux. The model of this parsing library parses the XML file and then generates the DOM model in memory, so that we can easily traverse the XML tree.


1. Download tinyxml

https://sourceforge.net/projects/tinyxml/
Open the web page, click Download to download, which contains: tinyxml.h, tinystr.h, tinyxml.cpp, tinystr.cpp, tinyxmlparser.cpp, tinyxmlerror.cpp and other files.

2. Create an XML file

#include<stdio.h>
#include "tinyxml.h"
using namespace std;

int main(){
    
    
	
	TiXmlDocument* tinyXmlDoc = new TiXmlDocument();//创建一个XML	
	TiXmlDeclaration* tinyXmlDeclare = new TiXmlDeclaration("1.0", "utf-8", "指定是否在XML中包含独立性声明");//创建头部信息	
	tinyXmlDoc->LinkEndChild(tinyXmlDeclare);// 插入文档类中	
	TiXmlElement* Root = new TiXmlElement("Root");// 创建根节点的名称
	tinyXmlDoc->LinkEndChild(Root);	// 把根节点插入到XML中

	TiXmlElement *Child_one = new TiXmlElement("Child_one");//添加子节点Child_one
	Child_one->SetAttribute("Name", "大明");  //设置节点的属性
	Child_one->SetAttribute("Gender", "Male");
	Child_one->SetAttribute("Age", "40");
	TiXmlText *Hobby = new TiXmlText("爱好:游泳");	// 创建文本
	Child_one->LinkEndChild(Hobby);	// 给Child_one节点添加文本

	TiXmlElement *Sunzi_one = new TiXmlElement("Sunzi_one"); //创建Sunzi_one节点
	Sunzi_one->SetAttribute("Name", "小明");  //设置节点的属性
	Sunzi_one->SetAttribute("Gender", "Male");
	Sunzi_one->SetAttribute("Age", "16");
	TiXmlText *Sunzione_hobby = new TiXmlText("爱好:象棋");	// 创建文本
	Sunzi_one->LinkEndChild(Sunzione_hobby);	// 给Sunzi_one节点添加文本
	Child_one->LinkEndChild(Sunzi_one);		// Sunzi_one节点插入到Child_one节点下

	TiXmlElement *Sunzi_two = new TiXmlElement("Sunzi_two"); //创建Sunzi_two节点
	Sunzi_two->SetAttribute("Name", "小创");  //设置节点的属性
	Sunzi_two->SetAttribute("Gender", "Male");
	Sunzi_two->SetAttribute("Age", "14");
	TiXmlText *Sunzitwo_hobby = new TiXmlText("爱好:武术");	// 创建文本
	Sunzi_two->LinkEndChild(Sunzitwo_hobby);	// 给Sunzi_two节点添加文本
	Child_one->LinkEndChild(Sunzi_two);		// Sunzi_two节点插入到Child_one节点下

	TiXmlElement *Sunzi_three = new TiXmlElement("Sunzi_three"); //创建Sunzi_three节点
	Sunzi_three->SetAttribute("Name", "小花");  //设置节点的属性
	Sunzi_three->SetAttribute("Gender", "Female");
	Sunzi_three->SetAttribute("Age", "13");
	TiXmlText *Sunzithree_hobby = new TiXmlText("爱好:跳舞");	// 创建文本
	Sunzi_three->LinkEndChild(Sunzithree_hobby);	// 给Sunzi_three节点添加文本
	Child_one->LinkEndChild(Sunzi_three);		// Sunzi_three节点插入到Child_one节点下

	Root->LinkEndChild(Child_one);	//子节点Child_one插入到Root节点下
	//保存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;
} 

insert image description here

3. Add data to XML file

#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");//获取根节点
	Root = tinyXmlDoc->RootElement();

	TiXmlElement *Child_two = new TiXmlElement("Child_two");
	// 插入属性
	Child_two->SetAttribute("Name", "大华");  //设置节点的属性
	Child_two->SetAttribute("Gender", "Male");
	Child_two->SetAttribute("Age", "38");
	TiXmlText *Hobby = new TiXmlText("爱好围棋");	// 创建文本
	Child_two->LinkEndChild(Hobby);	// 给Child_two节点添加文本
	
	TiXmlElement *Description = new TiXmlElement("Description");
	TiXmlText *descriptionText = new TiXmlText("还喜欢学习编程");		// 创建文本
	Description->LinkEndChild(descriptionText);		// 给Description节点添加文本
	Child_two->LinkEndChild(Description);				// 插入到Book1节点下
	Root->LinkEndChild(Child_two);	// 插入到根节点下
	// 保存到文件	
	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;
} 

After running 3 times, the result is as follows:
insert image description here

Fourth, modify the XML file

#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"); //获取根节点
	Root = tinyXmlDoc->RootElement();
	// 循环查找Child_two节点,修改属性值
	TiXmlElement *Child_two = new TiXmlElement("Child_two");
	TiXmlElement* pItem = Root->FirstChildElement("Child_two");
	for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two")) {
    
    
		// 找到属性Name=大华的节点
		if (strcmp(pItem->Attribute("Name"), "大华") == 0) {
    
    
			pItem->SetAttribute("Age", "39");
			// 设置Child_two的子节点Description的值
			TiXmlElement* Description = pItem->FirstChildElement("Description");	// 获得<Description>还喜欢学习编程</Description>
			TiXmlNode* des = Description->FirstChild();	// 获取元素指针		// 获得存储 "还喜欢学习编程" 的指针
			des->SetValue("最讨厌编程");	// 重新为其设置值				
		}
	}
	// 保存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;
} 

insert image description here

5. Parse the XML file

#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");
	}
	//获取文件根节点
	TiXmlElement* Root = new TiXmlElement("Root");
	if (Root){
    
    
		Root = tinyXmlDoc->RootElement();
	}
	// 解析Child_one节点
	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");  // 函数FirstChildElement():找到指定名字的元素
	if (pItem){
    
    
		for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two"))// 函数NextSiblingElement:在同一级元素中查找下一个指定名字的元素
		{
    
    
			// 解析Child_two节点的属性
			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的子节点
			TiXmlElement* Description = pItem->FirstChildElement("Description");
			if(Description){
    
    
				printf("Description = %s\n", Description->GetText());
			}
			printf("\n");
		}
	}
	printf("\n");
	system("pause");
}

insert image description here

6. Delete data from XML file

#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");
	}
	//获取文件根节点
	TiXmlElement* Root = new TiXmlElement("Root");
	if (Root){
    
    
		Root = tinyXmlDoc->RootElement();
	}
	// 删除Sunzi_two节点的Gender属性
	TiXmlElement* Child_one = Root->FirstChildElement("Child_one");
	TiXmlElement* pItem = Child_one->FirstChildElement("Sunzi_two");
	for (; pItem != NULL; pItem = pItem->NextSiblingElement("Sunzi_two")) {
    
    
		// 找到属性Name=小创的节点
		if (strcmp(pItem->Attribute("Name"), "小创") == 0) {
    
    
			// 删除Gender属性
			pItem->RemoveAttribute("Gender");
		}
	}
	//删除Child_two节点中属性Name="大华"的节点
	pItem = Root->FirstChildElement("Child_two");
	for (; pItem != NULL; ) {
    
    
		// 找到属性Name="大华"的节点
		if (strcmp(pItem->Attribute("Name"), "大华") == 0) {
    
    
			// 提前存储删除节点的下一个节点
			TiXmlElement* temporary = pItem->NextSiblingElement("Child_two");
			// 删除当前节点,删除后pItem为NULL,如果再继续使用它会报错
			Root->RemoveChild(pItem->ToElement());
			// 这里要进行赋值回来
			pItem = temporary;

		} else {
    
    
			// 寻找下一个Child_two节点
			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");
}

insert image description here

Summarize

The above is what I want to talk about today. This article only briefly introduces the use of tinyxml, including the operation demonstration of creating XML files and adding, deleting, modifying and checking.

Guess you like

Origin blog.csdn.net/a_13572035650/article/details/131905877