RTU_ALL_AI/release/inc/myXml.h

73 lines
2.0 KiB
C

/**
* @file myXml.h
* @brief XML 解析库 — 纯 C 接口封装 tinyxml2
* @details 提供不透明句柄的 C 风格 XML DOM 操作。
* 内部 C++ tinyxml2 实现,外部纯 C 兼容。
*/
#ifndef _MY_XML_H_
#define _MY_XML_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* 不透明句柄 */
typedef void *myXmlDoc_t;
typedef void *myXmlElem_t;
/* ========== 文档生命周期 ========== */
/** 从文件加载 XML 文档,失败返回 NULL */
myXmlDoc_t myXml_load_file(const char *path);
/** 新建空 XML 文档(含 XML 声明) */
myXmlDoc_t myXml_new_doc(const char *root_name);
/** 释放文档 */
void myXml_free_doc(myXmlDoc_t doc);
/** 获取根元素 */
myXmlElem_t myXml_root_elem(myXmlDoc_t doc);
/** 保存文档到文件 */
int myXml_save_file(myXmlDoc_t doc, const char *path);
/* ========== 元素查询 ========== */
/** 查找第一个匹配名称的子元素 */
myXmlElem_t myXml_first_child(myXmlElem_t parent, const char *name);
/** 查找下一个匹配名称的兄弟元素 */
myXmlElem_t myXml_next_sibling(myXmlElem_t elem, const char *name);
/** 获取字符串属性,不存在返回默认值 */
const char *myXml_attr_str(myXmlElem_t elem, const char *name, const char *def_val);
/** 获取 float 属性,不存在返回默认值 */
float myXml_attr_float(myXmlElem_t elem, const char *name, float def_val);
/** 获取 int 属性,不存在返回默认值 */
int myXml_attr_int(myXmlElem_t elem, const char *name, int def_val);
/* ========== 元素创建 ========== */
/** 为父节点创建子元素 */
myXmlElem_t myXml_new_child(myXmlElem_t parent, const char *name);
/** 设置字符串属性 */
void myXml_set_attr(myXmlElem_t elem, const char *name, const char *value);
/** 设置 int 属性 */
void myXml_set_attr_int(myXmlElem_t elem, const char *name, int value);
/** 设置 float 属性 */
void myXml_set_attr_float(myXmlElem_t elem, const char *name, float value);
#ifdef __cplusplus
}
#endif
#endif