当前位置: 首页 > news >正文

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字符串。

#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文件。

XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );

写入XML文件。

#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++介绍

http://www.lryc.cn/news/347627.html

相关文章:

  • JAVA课程设计
  • 基于SpringBoot+Vue的旅游网站系统
  • http代理ip按流量划算还是个数划算?
  • Banana Pi BPI-F3, 进迭时空K1芯片设计,定位工业级应用,网络通信及工业自动化
  • 安科瑞工业IT产品及解决方案—电源不接地,设备外壳接地【监测系统对地绝缘电阻】
  • 栈:概念与实现
  • 【Linux】查找服务器中某个文件的完整路径
  • windows server 2019 安装 docker环境
  • 【Linux】探索 Linux du 命令:管理磁盘空间的利器
  • Service 和 Ingress
  • C++(类和对象—封装)
  • 如何训练一个大模型:LoRA篇
  • Spring Cloud学习笔记(Nacos):基础和项目启动
  • 音频提取特征
  • AJAX前端与后端交互技术知识点以及案例
  • [AutoSar]BSW_Diagnostic_003 ReadDataByIdentifier(0x22)介绍
  • 买卖股票的最佳时机 II(LeetCode 122)
  • Spring Boot:让微服务开发像搭积木一样简单!
  • WordPress 、Typecho 站点的 MySQL/MariaDB 数据库优化
  • ==与===的区别
  • 什么是ACID及基本实现的示例
  • 【启明智显技术分享】SSD202核心板Rootfs下如何烧录mac地址
  • springboot3 集成spring-authorization-server (一 基础篇)
  • AVL树!
  • 知识付费系统怎么安装教程,教师课堂教学该掌握哪些表达技巧?
  • 基于MetaGPT的LLM Agent学习实战(一)
  • 【IMX6ULL项目】IMX6ULL上Linux系统实现产测工具框架
  • 【Linux基础】Vim保姆级一键配置教程(手把手教你把Vim打造成高效率C++开发环境)
  • Gartner发布准备应对勒索软件攻击指南:勒索软件攻击的三个阶段及其防御生命周期
  • IB 公式解析