TinyXML-2是C++中轻量级XML操作库,支持解析与生成XML文件。1. 通过包含tinyxml2.h/cpp文件或CMake引入库;2. 使用XMLDocument加载文件并读取元素属性和文本内容;3. 可创建XML结构并保存到文件;4. 提供安全读取、遍历子元素等常用操作技巧,适用于中小型项目。

在C++中操作XML文件,TinyXML-2是一个轻量、易用且高效的库。它允许你解析已有的XML文件,也可以创建新的XML数据并保存到文件。下面介绍如何使用TinyXML-2进行XML的读取与生成。
1. 安装与配置 TinyXML-2
TinyXML-2 是一个只有头文件和源文件的小型库,无需复杂安装。你可以从其 GitHub 仓库获取:
- 下载地址:https://github.com/leethomason/tinyxml2
- 将 tinyxml2.h 和 tinyxml2.cpp 添加到你的项目中
- 编译时确保包含 tinyxml2.cpp 源文件
如果你使用的是 CMake,可以这样引入:
find_package(Git QUIET) include(FetchContent) FetchContent_Declare( tinyxml2 GIT_REPOSITORY https://github.com/leethomason/tinyxml2.git GIT_TAG master ) FetchContent_MakeAvailable(tinyxml2) target_link_libraries(your_target_name PRIVATE tinyxml2)
2. 解析 XML 文件
假设有一个名为 config.xml 的文件:
<?xml version="1.0" encoding="UTF-8"?><config><window width="800" height="600"><title>My Game</title></window><fullscreen value="false"></fullscreen></config>
使用 TinyXML-2 读取该文件并提取数据:
#include "tinyxml2.h"
#include <iostream>
using namespace tinyxml2;
<p>void readXML() {
XMLDocument doc;
XMLError result = doc.LoadFile("config.xml");
if (result != XML_SUCCESS) {
std::cerr </p>
<pre class="brush:php;toolbar:false;">// 获取根节点
const XMLElement* config = doc.FirstChildElement("config");
if (!config) return;
// 读取 window 元素属性
const XMLElement* window = config->FirstChildElement("window");
if (window) {
int width = 0, height = 0;
window->QueryIntAttribute("width", &width);
window->QueryIntAttribute("height", &height);
const char* title = window->FirstChildElement("title")->GetText();
std::cout FirstChildElement("fullscreen");
if (fs) {
bool value = false;
fs->QueryBoolAttribute("value", &value);
std::cout <p>}</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13230" title="Livestream Sales Script"><img
src="https://img.php.cn/upload/skill/000/000/081/179488448917987.jpg" alt="Livestream Sales Script" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13230" title="Livestream Sales Script" class="overflowclass">Livestream Sales Script</a>
<p class="overflowclass">直播带货脚本专家,擅长根据产品信息编写生动、吸引人且具说服力的直播脚本,突出核心卖点。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13230" title="Livestream Sales Script" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><a style="color:#f60; text-decoration:underline;" title="stream" href="https://m.php.cn/zt/19633.html" target="_blank">stream</a>>
3. 创建并生成 XML 文件
你可以使用 TinyXML-2 构建 XML 结构并写入文件:
void writeXML() {
XMLDocument doc;
<pre class="brush:php;toolbar:false;">// 声明
XMLDeclaration* decl = doc.NewDeclaration();
doc.InsertFirstChild(decl);
// 根元素
XMLElement* config = doc.NewElement("config");
doc.InsertEndChild(config);
// 添加 window 子元素
XMLElement* window = doc.NewElement("window");
window->SetAttribute("width", 1024);
window->SetAttribute("height", 768);
config->InsertEndChild(window);
XMLElement* title = doc.NewElement("title");
title->SetText("New Game");
window->InsertEndChild(title);
// 添加 fullscreen 元素
XMLElement* fs = doc.NewElement("fullscreen");
fs->SetAttribute("value", true);
config->InsertEndChild(fs);
// 保存到文件
XMLError result = doc.SaveFile("output.xml");
if (result != XML_SUCCESS) {
std::cerr <p>}</p>
运行后会生成 output.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?><config><window width="1024" height="768"><title>New Game</title></window><fullscreen value="true"></fullscreen></config>
4. 常见操作技巧
- 检查元素是否存在:始终判断指针是否为 nullptr,避免崩溃
- 安全读取属性:使用 QueryIntAttribute、QueryBoolAttribute 等返回 XMLError,可判断是否成功读取
- 文本内容处理:使用 GetText() 获取元素内的文本,注意空值判断
- 遍历子元素:使用 NextSiblingElement() 循环遍历同名节点
示例:遍历所有 item 节点
const XMLElement* item = config->FirstChildElement("item");
while (item) {
const char* name = item->Attribute("name");
std::cout NextSiblingElement("item");
}
基本上就这些。TinyXML-2 虽然功能不如大型库全面,但对大多数中小型项目已经足够,接口清晰,易于集成。只要掌握基本的节点查找、属性读取和元素创建,就能高效完成 XML 数据操作。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










