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" />结果:
cpperr == XML_SUCCESS width == 128例子 2:属性不存在
XML<Item />结果:
cpperr == XML_NO_ATTRIBUTE例子 3:类型错误
XML<Item Width="abc" />结果:
cpperr == 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" />
这里 Width、Height 是属性。
读取方式:
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() |