TinyXML-2介绍

1.简介

TinyXML-2 是一个简单、小巧的 C++ XML 解析库,它是 TinyXML 的一个改进版本,专注于易用性和性能。TinyXML-2 用于读取、修改和创建 XML 文档。它不依赖于外部库,并且可以很容易地集成到项目中。

tinyXML-2 的主要特点包括:

  • DOM 风格 API:TinyXML-2 提供了 Document Object Model(DOM)风格的API,允许开发者以树形结构的方式操作 XML 数据。
  • 轻量级:TinyXML-2 的代码量小,不需要外部依赖,适合嵌入式系统和移动设备。
  • 易于使用:TinyXML-2 的 API 设计直观,易于理解和集成。
  • 错误处理:TinyXML-2提供了详细的错误信息,帮助开发者快速定位问题。

2.环境搭建

下载地址:https://github.com/leethomason/tinyxml2/tree/10.0.0

解压文件后,直接引用以下两个文件即可。

3.代码示例

解析XML字符串。

cpp 复制代码
#include <iostream>
#include "tinyxml2.h"

using namespace std;
using namespace tinyxml2;

int main()
{
	static const char* xml =
		"<?xml version=\"1.0\"?>"
		"<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
		"<information>"
		"	<attributeApproach v='2' />"
		"	<textApproach>"
		"		<v>2</v>"
		"	</textApproach>"
		"</information>";

	XMLDocument doc;
	doc.Parse(xml);

	int v0 = 0;
	int v1 = 0;

	XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement("attributeApproach");
	attributeApproachElement->QueryIntAttribute("v", &v0);

	XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement("textApproach");
	textApproachElement->FirstChildElement("v")->QueryIntText(&v1);

	printf("Both values are the same: %d and %d\n", v0, v1);

	return doc.ErrorID();
}

使用以下代码,可以直接加载XML文件。

cpp 复制代码
XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );

写入XML文件。

cpp 复制代码
#include <iostream>
#include "tinyxml2.h"

using namespace std;
using namespace tinyxml2;

int main()
{
	XMLDocument* doc = new XMLDocument();

	XMLNode* declaration = doc->InsertFirstChild(doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""));

	XMLNode* element = doc->InsertEndChild(doc->NewElement("element"));

	XMLElement* sub[3] = { doc->NewElement("sub"), doc->NewElement("sub"), doc->NewElement("sub") };
	for (int i = 0; i < 3; ++i) 
	{
		sub[i]->SetAttribute("attrib", i);
	}
	element->InsertEndChild(sub[2]);

	XMLNode* comment = element->InsertFirstChild(doc->NewComment("comment"));

	element->InsertAfterChild(comment, sub[0]);
	element->InsertAfterChild(sub[0], sub[1]);
	sub[2]->InsertFirstChild(doc->NewText("Text!"));

	XMLElement* sub4 = doc->NewElement("textApproach");

	element->InsertAfterChild(sub[2], sub4);

	XMLElement* sub5 = doc->NewElement("v");
	sub4->InsertFirstChild(sub5);

	sub5->InsertFirstChild(doc->NewText("2"));

	doc->Print();

	doc->SaveFile("./pretty.xml");

	return 0;
}

运行结果:

4.更多参考

libVLC 专栏介绍-CSDN博客

Qt+FFmpeg+opengl从零制作视频播放器-1.项目介绍_qt opengl视频播放器-CSDN博客

QCharts -1.概述-CSDN博客

JSON++介绍

相关推荐
我是陈泽12 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
Mr.Z.41119 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
优雅的小武先生23 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death20023 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
虽千万人 吾往矣29 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人32 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈34 分钟前
C++——list
开发语言·c++·list
杨荧35 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
ZPC821042 分钟前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
镜花照无眠44 分钟前
Python爬虫使用实例-mdrama
开发语言·爬虫·python