栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

windows系统下C++使用TinyXML读写XML文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

windows系统下C++使用TinyXML读写XML文件

作者:RayChiu_Labloy
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处


目录

下载

windows环境编译(假编译)准备  

对xml文件读、写、编辑、删除操作

写文件

读文件 

 删除节点编辑节点


下载

官网:TinyXML download | SourceForge.net

windows环境编译(假编译)准备  

下载完源码后打开发现里边源码就几个

        没必要编译成静态库或者动态库,把上面列举的2个头文件和4个cpp文件拷贝(新建同名文件复制内容进去)到示例工程目录下即可直接使用。

对xml文件读、写、编辑、删除操作

写文件
#include 
#include "tinyxml.h"
using namespace std;

int main()
{
	// 新建的xml文件名字
	string filename = "E:/api/tinyxml/temp/yolo.xml";

	//新建一个xml文件
	// 定义一个TiXmldocument类指针
	TiXmldocument* pWriteDoc = new TiXmldocument();

	// xml的声明(三个属性:版本,编码格式,独立文件声明)
	TiXmlDeclaration* pDeclare = new TiXmlDeclaration("1.0", "UTF-8", "yes");
	pWriteDoc->linkEndChild(pDeclare);			// 连接到最后

	// 根节点
	TiXmlElement* pRootElement = new TiXmlElement("annotation");
	pWriteDoc->linkEndChild(pRootElement);		// 把根节点连接到最后

	// 二级节点
	TiXmlElement* pFolderElement = new TiXmlElement("folder");
	pRootElement->linkEndChild(pFolderElement);	// 连接到根节点下
	TiXmlText* folderContent = new TiXmlText("JPEGImages");	
	pFolderElement->linkEndChild(folderContent);	// 给folder节点添加文本

	TiXmlElement* pFileNameElement = new TiXmlElement("filename");
	pRootElement->linkEndChild(pFileNameElement);	// 连接到根节点下
	TiXmlText* fileNameContent = new TiXmlText("1.bmp");
	pFileNameElement->linkEndChild(fileNameContent);	// 给filename节点添加文本

	TiXmlElement* pPathElement = new TiXmlElement("path");
	pRootElement->linkEndChild(pPathElement);	// 连接到根节点下
	TiXmlText* pathContent = new TiXmlText("E:/projects/pyHome/about_yolo/yolov5_bottleCap_defect_detection/VOCData/JPEGImages/1.bmp");
	pPathElement->linkEndChild(pathContent);	// 给path节点添加文本

	TiXmlElement* pSizeElement = new TiXmlElement("size");
	pRootElement->linkEndChild(pSizeElement);	// 连接到根节点下
	TiXmlElement* pWithElement = new TiXmlElement("width");
	pSizeElement->linkEndChild(pWithElement);	// 连接到二级节点size节点下的三级节点
	TiXmlText* withContent = new TiXmlText("1280");		// width节点文本
	pWithElement->linkEndChild(withContent);	// 给三级节点width添加文本
	TiXmlElement* pHeightElement = new TiXmlElement("height");
	pSizeElement->linkEndChild(pHeightElement);	// 连接到二级节点size节点下的三级节点
	TiXmlText* heightContent = new TiXmlText("960");		// height节点文本
	pHeightElement->linkEndChild(heightContent);	// 给三级节点height添加文本
	TiXmlElement* pDepthElement = new TiXmlElement("depth");
	pSizeElement->linkEndChild(pDepthElement);	// 连接到二级节点size节点下的三级节点
	TiXmlText* depthContent = new TiXmlText("3");		// depth节点文本
	pDepthElement->linkEndChild(depthContent);	// 给三级节点depth添加文本

	///		保存到文件	
	pWriteDoc->SaveFile(filename.c_str());
	printf("new xml success, file's name is %snn", filename.c_str());
	return 0;
}

读文件 
#include 
#include "tinyxml.h"
using namespace std;

int main()
{
	// 新建的xml文件名字
	string filename = "E:/api/tinyxml/temp/yolo.xml";

	//从文件中读取
	// 定义一个TiXmldocument类指针
	TiXmldocument* pReaddocument = new TiXmldocument();

	// 读取文件
	if (!pReaddocument->LoadFile(filename.c_str()))
	{
		printf("Could not load example xml file %s. Error='%s'n", filename.c_str(), pReaddocument->ErrorDesc());
		return 0;
	}

	printf("read xml file success, file' name is %s nn", filename.c_str());

	//读取文档声明信息(第一个子节点转换得到文档声明)
	TiXmlDeclaration* pDeclar = pReaddocument->FirstChild()->ToDeclaration();
	if (pDeclar != NULL)
	{
		printf("read declare, version is %s , encoding is %sn", pDeclar->Version(), pDeclar->Encoding());
	}

	// 得到文件根节点
	TiXmlElement* pRootElement = new TiXmlElement("annotation");
	pRootElement = pReaddocument->RootElement();

	//遍历元素,打印
	printf("begin read all xml element nn");

	// 遍历所有的size
	// 函数FirstChildElement()		:	找到指定名字的元素
	// 函数NextSiblingElement		:	在同一级元素中查找下一个指定名字的元素
	int i = 0;
	for (TiXmlElement* pItem = pRootElement->FirstChildElement("size"); pItem; pItem = pItem->NextSiblingElement("size"))
	{
		printf("read the %d size n", ++i);

		// 宽
		TiXmlElement* pWith = pItem->FirstChildElement("with");
		if (pWith != NULL)
		{
			printf("the %d size's with = %s n", i, pWith->GetText());
		}
		// 高
		TiXmlElement* pHeight = pItem->FirstChildElement("height");
		if (pHeight != NULL)
		{
			printf("the %d size's height = %s n", i, pHeight->GetText());
		}
		// 深度
		TiXmlElement* pDepth = pItem->FirstChildElement("depth");
		if (pDepth != NULL)
		{
			printf("the %d size's depth = %s n", i, pDepth->GetText());
		}

		printf("nn");
	}
	return 0;
}

 删除节点编辑节点
#include 
#include "tinyxml.h"
using namespace std;

int main()
{
	// 新建的xml文件名字
	string filename = "E:/api/tinyxml/temp/yolo.xml";

	//从文件中读取
	// 定义一个TiXmldocument类指针
	TiXmldocument* pReaddocument = new TiXmldocument();

	// 读取文件
	if (!pReaddocument->LoadFile(filename.c_str()))
	{
		printf("Could not load yolo xml file %s. Error='%s'n", filename.c_str(), pReaddocument->ErrorDesc());
		return 0;
	}

	printf("read xml file success, file' name is %s nn", filename.c_str());

	//读取文档声明信息(第一个子节点转换得到文档声明)
	TiXmlDeclaration* pDeclar = pReaddocument->FirstChild()->ToDeclaration();
	if (pDeclar != NULL)
	{
		printf("read declare, version is %s , encoding is %sn", pDeclar->Version(), pDeclar->Encoding());
	}

	// 得到文件根节点
	TiXmlElement* pRootElement = new TiXmlElement("annotation");
	pRootElement = pReaddocument->RootElement();

	//删除元素,属性
	TiXmlElement* pSize = pRootElement->FirstChildElement("size");
	if (pSize != NULL)
	{
		// 这里演示删除"depth"元素,删除其他节点也是一样的办法
		TiXmlElement* pDepth = pSize->FirstChildElement("depth");
		if (pDepth != NULL)
		{
			pSize->RemoveChild(pDepth);
		}

		// 这里演示修改"addr"元素
		TiXmlElement* pHeight = pSize->FirstChildElement("height");
		if (pHeight != NULL)
		{
			pHeight->SetValue("height"); //修改节点文本
			//pHeight->SetAttribute("name", "value");	// 修改属性值
		}

		//text文本是height的FirstChild,因此也可用SetValue函数:
		TiXmlNode* pText = pHeight->FirstChild();
		if (pText != NULL)
		{
			pText->SetValue("961");
		}
	}

	//	再次保存到文件	
	if (pReaddocument->SaveFile(filename.c_str()))
	{
		printf("save file successn");
	}
	return 0;
}

 

【如果对您有帮助,交个朋友给个一键三连吧,您的肯定是我博客高质量维护的动力!!!】

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号