tinyxml2 常用读取接口对照表

  • Attribute():读字符串属性
  • BoolAttribute() / IntAttribute() / DoubleAttribute() ...:按指定类型直接读,支持默认值
  • QueryXXXAttribute():按指定类型读,但会告诉你"成功 / 不存在 / 格式错误",可区分"没写"和"写错"
  • FindAttribute():只找属性对象本身
  • GetText() / QueryXXXText():读节点文本,不是读属性

1. tinyxml2 常用接口总对照表

接口 作用 返回值 是否支持默认值 能否区分"属性不存在"和"格式错误" 常见用途
Attribute(name) 读取属性字符串值 const char* 读取字符串属性
Attribute(name, value) 判断属性值是否等于某字符串 const char*nullptr 判断某属性是否等于指定值
BoolAttribute(name, defaultValue) 读取 bool 属性 bool 简单读取布尔开关
IntAttribute(name, defaultValue) 读取 int 属性 int 简单读取整数
UnsignedAttribute(name, defaultValue) 读取 unsigned 属性 unsigned 读取无符号整数
Int64Attribute(name, defaultValue) 读取 int64_t 属性 int64_t 大整数
Unsigned64Attribute(name, defaultValue) 读取 uint64_t 属性 uint64_t 大无符号整数
FloatAttribute(name, defaultValue) 读取 float 属性 float 浮点参数
DoubleAttribute(name, defaultValue) 读取 double 属性 double 精度更高的浮点参数
QueryBoolAttribute(name, &out) 查询 bool 属性 XMLError 需要严格判断配置合法性
QueryIntAttribute(name, &out) 查询 int 属性 XMLError 需要判断是否读取成功
QueryUnsignedAttribute(name, &out) 查询 unsigned 属性 XMLError 严格校验
QueryInt64Attribute(name, &out) 查询 int64_t 属性 XMLError 严格校验
QueryUnsigned64Attribute(name, &out) 查询 uint64_t 属性 XMLError 严格校验
QueryFloatAttribute(name, &out) 查询 float 属性 XMLError 严格校验
QueryDoubleAttribute(name, &out) 查询 double 属性 XMLError 严格校验
QueryStringAttribute(name, &out) 查询字符串属性 XMLError 部分能 需要显式判断字符串属性是否存在
FindAttribute(name) 找到属性节点 const XMLAttribute* 只区分 是否存在 想进一步检查属性本身
GetText() 读取元素文本 const char* 读取 <tag>text</tag>
QueryIntText(&out) / QueryBoolText(&out) 读取元素文本并转类型 XMLError 严格读取文本值

2. Attribute() 系列

2.1 Attribute(name)

cpp 复制代码
const char* val = node->Attribute("Type");

含义:读取属性 patchType 的字符串值。

返回:1、属性存在:返回属性值字符串;2、属性不存在:返回 nullptr。

特点:1、只读字符串;2、没有默认值;3、不做类型转换;4、最常用于字符串属性读取。

2.2 Attribute(name, value)

cpp 复制代码
if (node->Attribute("Type", "string")) { ... }

含义:检查属性 Type 是否存在,并且值是否等于 "string"

返回:1、相等:返回属性值指针(可当 true);2、不存在或不相等:返回 nullptr。

注意:这里第二个参数 不是默认值 ,而是比较值

特点:1、只读字符串;2、没有默认值;3、不做类型转换;4、最常用于字符串属性读取。


3. BoolAttribute() / IntAttribute() / DoubleAttribute() 系列

这类接口特点很统一:

  • 直接按目标类型返回值
  • 可以给默认值
  • 如果属性存在,返回解析后的值
  • 如果属性不存在,返回默认值
  • 如果转换失败或者格式错误,通常也会回退到默认值
  • 不能区分"没写属性"还是"属性写错了"

3.1 BoolAttribute()

cpp 复制代码
bool enabled = node->BoolAttribute("enable", false);

含义:读取 enable 属性并转成 bool。

适用场景:适合"属性可选,没写就走默认值"的场景。

3.2 IntAttribute()

cpp 复制代码
int width = node->IntAttribute("Width", 0);

含义:读取整数属性。

3.3 DoubleAttribute()

cpp 复制代码
double sigma = node->DoubleAttribute("sigma", 0.2);

含义:读取 double 属性,读不到就用默认值。

3.4 这一类接口的共同特点总结

接口 典型类型 不存在时 类型错误时 是否适合严格校验
BoolAttribute() bool 返回默认值 通常返回默认值 不太适合
IntAttribute() int 返回默认值 通常返回默认值 不太适合
DoubleAttribute() double 返回默认值 通常返回默认值 不太适合

所以:

  • 想省事 :用 XXXAttribute(default)
  • 想严格检查配置 :用 QueryXXXAttribute()

4. QueryXXXAttribute() 系列

这类接口和上面最大的区别是:

它不直接把结果作为返回值,而是:

  • 通过输出参数拿到值
  • 通过返回码告诉你是否成功

这是做严格配置校验时最好用的一组接口。

4.1 QueryIntAttribute()

cpp 复制代码
int width = 0;
XMLError err = node->QueryIntAttribute("Width", &width);

返回值通常有:

  • XML_SUCCESS:成功
  • XML_NO_ATTRIBUTE:属性不存在
  • XML_WRONG_ATTRIBUTE_TYPE:属性存在,但类型不对

例子 1:正确

XML 复制代码
<Item Width="128" />

结果:

cpp 复制代码
err == XML_SUCCESS
width == 128

例子 2:属性不存在

XML 复制代码
<Item />

结果:

cpp 复制代码
err == XML_NO_ATTRIBUTE

例子 3:类型错误

XML 复制代码
<Item Width="abc" />

结果:

cpp 复制代码
err == XML_WRONG_ATTRIBUTE_TYPE

这就是它比 IntAttribute() 强的地方。

4.2 QueryBoolAttribute()

cpp 复制代码
bool enable = false;
XMLError err = node->QueryBoolAttribute("Enable", &enable);

作用: 严格读取 bool 配置。

4.3 QueryDoubleAttribute()

cpp 复制代码
double threshold = 0.0;
XMLError err = node->QueryDoubleAttribute("Threshold", &threshold);

4.4 QueryStringAttribute()

cpp 复制代码
const char* patchType = nullptr;
XMLError err = node->QueryStringAttribute("Type", &Type);

作用:严格查询字符串属性是否存在。

4.5 QueryXXXAttribute() 总结

接口 返回值 输出参数 能区分不存在 能区分类型错误 适合场景
QueryIntAttribute() XMLError int* 严格解析配置
QueryBoolAttribute() XMLError bool* 严格布尔配置
QueryDoubleAttribute() XMLError double* 严格数值配置
QueryStringAttribute() XMLError const char** 字符串一般无类型错误问题 严格字符串读取

5. Attribute()QueryXXXAttribute() 的核心区别

对比项 Attribute() / IntAttribute() 这类 QueryXXXAttribute() 这类
用法 简单 稍复杂
返回结果 直接给值 / 指针 返回状态码
是否支持默认值 IntAttribute 等支持,Attribute 不支持 不支持
能否区分属性不存在 通常不方便 可以
能否区分类型错误 通常不行 可以
适合 宽松配置读取 严格配置校验

一句话概括:

  • 宽松读取,用 XXXAttribute()
  • 严格校验,用 QueryXXXAttribute()

6. FindAttribute()

cpp 复制代码
const tinyxml2::XMLAttribute* attr = node->FindAttribute("Type");

含义:查找属性对象本身,而不是直接取值。

返回:1、找到:返回属性对象指针;2、找不到:返回 nullptr。

作用:

  • 先判断属性是否存在
  • 再自己处理属性名、属性值

例如:

cpp 复制代码
const XMLAttribute* attr = node->FindAttribute("Type");
if (attr) 
{
    std::cout << attr->Name() << " = " << attr->Value() << std::endl;
}

7. GetText() 和属性读取的区别

注意不要把"属性"和"节点文本"混在一起。

7.1 读属性:Attribute()

cpp 复制代码
<Item Width="128" Height="64" />

这里 WidthHeight属性

读取方式:

cpp 复制代码
node->IntAttribute("Width", 0);

7.2 读文本:GetText()

XML 复制代码
<Image>abcdefg12345</Image>

这里 abcdefg12345节点文本,不是属性。

读取方式:

cpp 复制代码
const char* text = imageNode->GetText();

7.3 对照表

XML 写法 数据类型 读取方式
<Node Width="128"/> 属性 Attribute() / IntAttribute()
<Node>true</Node> 节点文本 GetText() / QueryBoolText()
<Image>base64...</Image> 节点文本 GetText()

8. QueryXXXText()

这是和 QueryXXXAttribute() 对应的一组"读取节点文本"的接口。

8.1 QueryIntText()

XML 复制代码
<Width>128</Width>
cpp 复制代码
int width = 0;
XMLError err = widthNode->QueryIntText(&width);

8.2 QueryBoolText()

XML 复制代码
<Enable>true</Enable>
cpp 复制代码
bool enable = false;
XMLError err = enableNode->QueryBoolText(&enable);

9. 一张最实用的记忆表

需求 推荐接口
读取字符串属性 Attribute("name")
判断属性是否等于某字符串 Attribute("name", "value")
读取 bool,允许默认值 BoolAttribute("name", def)
读取 int,允许默认值 IntAttribute("name", def)
读取 double,允许默认值 DoubleAttribute("name", def)
严格判断 int 属性是否存在且合法 QueryIntAttribute("name", &v)
严格判断 bool 属性是否存在且合法 QueryBoolAttribute("name", &v)
严格判断字符串属性是否存在 QueryStringAttribute("name", &v)
只判断属性是否存在 FindAttribute("name")
读取 <tag>text</tag> 文本 GetText()
严格读取文本并转类型 QueryIntText() / QueryBoolText()
相关推荐
MC皮蛋侠客2 小时前
C++中使用Redis指南:基于redis-plus-plus库
开发语言·c++·redis
仟濹3 小时前
【算法打卡day35(2026-03-31 周二)】DFS专项训练2(今日算法:DFS & 记忆化搜索 & 回溯)
c++·算法·蓝桥杯·深度优先
liuyao_xianhui3 小时前
优选算法_岛屿的最大面积_floodfill算法_C++
java·开发语言·数据结构·c++·算法·leetcode·链表
寻寻觅觅☆3 小时前
东华OJ-基础题-33-数字之和(C++)
数据结构·c++·算法
xvhao20133 小时前
C++freopen的用法
开发语言·c++
南境十里·墨染春水4 小时前
C++ 笔记 仿函数(函数对象)
开发语言·c++·笔记
快乐的划水a4 小时前
封装动态库并调用
c++
xvhao20134 小时前
P4084 [USACO17DEC] Barn Painting G 题解
数据结构·c++·算法·深度优先·动态规划
云栖梦泽4 小时前
Linux内核与驱动:5.并发与竞争
linux·c++