根据索引获取子元素 - XmlGetChildByIndex
函数简介
根据索引获取子元素。
接口名称
XmlGetChildByIndex
DLL调用
int64_t XmlGetChildByIndex(int64_t parent, int32_t index, int32_t* err)
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| parent | int64_t | 父元素句柄 |
| index | int32_t | 子元素索引(从0开始) |
| err | int32_t* | 错误码输出参数,可为NULL |
示例
int32_t err = 0;
int64_t doc = XmlParseFile("config.xml", &err);
int64_t root = XmlGetRootElement(doc, &err);
int32_t count = XmlGetChildCount(root, &err);
for (int32_t i = 0; i < count; i++) {
int64_t child = XmlGetChildByIndex(root, i, &err);
const char* name = XmlGetElementName(child, &err);
printf("子元素[%d]:%s\n", i, name);
FreeStringPtr(instance, name);
}
XmlFree(doc);
返回值
| 返回值类型 | 说明 |
|---|---|
| int64_t | 返回子元素句柄,失败时返回0 |
注意事项
- 索引从0开始
- 如果索引超出范围,返回0
- 配合
XmlGetChildCount可以遍历所有子元素
