ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::xml Class Reference

XML操作工具类 More...

#include <xml_serialize.h>

Static Public Member Functions

static bool readxml (const char *aname, tinyxml2::XMLDocument &axml)
 读取XML文件
static bool writexml (const char *aname, tinyxml2::XMLDocument &axml)
 写入XML文件
static tinyxml2::XMLElement * get_child (tinyxml2::XMLElement *aele, const char *astr)
 获取子元素
static tinyxml2::XMLElement * get_child (tinyxml2::XMLDocument &axml, const char *astr)
 从文档获取子元素
static tinyxml2::XMLElement * set_child (tinyxml2::XMLElement *aele, const char *astr)
 创建子元素
static tinyxml2::XMLElement * set_child (tinyxml2::XMLDocument &axml, const char *astr)
 在文档中创建子元素
template<typename T>
static bool get (tinyxml2::XMLElement *aele, const char *akey, T &aval)
 获取元素值
template<typename T>
static bool get (tinyxml2::XMLElement *aele, T &aval)
 获取元素值(无键名)
template<typename T>
static bool set (tinyxml2::XMLElement *aele, const char *akey, const T &aval)
 设置元素值
template<typename T>
static bool set (tinyxml2::XMLElement *aele, const T &aval)
 设置元素值(无键名)
template<typename T>
static bool get_xmlattr (tinyxml2::XMLElement *aele, const char *akey, T &aval)
 获取XML属性值
template<typename T>
static bool set_xmlattr (tinyxml2::XMLElement *aele, const char *akey, const T &aval)
 设置XML属性值
static std::string str (tinyxml2::XMLDocument &axml)
 将XML文档转换为字符串
static bool foreach (tinyxml2::XMLElement *aele, const char *akey, const std::function< bool(tinyxml2::XMLElement *)> &afun)
 遍历指定名称的子元素
static bool foreach (tinyxml2::XMLElement *aele, const std::function< bool(tinyxml2::XMLElement *)> &afun)
 遍历所有子元素
static bool foreach_xmlattr (tinyxml2::XMLElement *aele, const std::function< bool(const char *, const char *)> &afun)
 遍历所有XML属性

Detailed Description

XML操作工具类

提供XML文件的读写、元素查找和遍历功能。 封装了tinyxml2库的常用操作。

Note
所有方法均为静态方法,禁止实例化。
// 读取XML文件
tinyxml2::XMLDocument doc;
ngl::xml::readxml("config.xml", doc);
// 获取子元素
tinyxml2::XMLElement* element = ngl::xml::get_child(doc, "root/child");
// 读取属性值
int value;
ngl::xml::get(element, "attr_name", value);

Member Function Documentation

◆ foreach() [1/2]

bool ngl::xml::foreach ( tinyxml2::XMLElement * aele,
const char * akey,
const std::function< bool(tinyxml2::XMLElement *)> & afun )
static

遍历指定名称的子元素

按标签名遍历子元素

Parameters
aele父元素
akey子元素名称
afun回调函数,返回false停止遍历
Returns
遍历完成返回true
Parameters
aele父元素
akey子元素标签名过滤
afun遍历回调,返回false提前终止
Return values
true遍历完成
false回调返回false或参数无效

◆ foreach() [2/2]

bool ngl::xml::foreach ( tinyxml2::XMLElement * aele,
const std::function< bool(tinyxml2::XMLElement *)> & afun )
static

遍历所有子元素

遍历所有直接子元素

Parameters
aele父元素
afun回调函数,返回false停止遍历
Returns
遍历完成返回true
Parameters
aele父元素
afun遍历回调,返回false提前终止
Return values
true遍历完成
false回调返回false或父元素为空

◆ foreach_xmlattr()

bool ngl::xml::foreach_xmlattr ( tinyxml2::XMLElement * aele,
const std::function< bool(const char *, const char *)> & afun )
static

遍历所有XML属性

遍历元素的所有XML属性

Parameters
aele元素指针
afun回调函数,接收属性名和值,返回false停止遍历
Returns
遍历完成返回true
Parameters
aele目标元素
afun属性回调,参数为(属性名, 属性值),返回false提前终止
Return values
true遍历完成
false回调返回false或元素为空

◆ get() [1/2]

template<typename T>
bool ngl::xml::get ( tinyxml2::XMLElement * aele,
const char * akey,
T & aval )
static

获取元素值

从XML元素获取值。

Template Parameters
T值类型
Parameters
aele元素指针
akey键名
aval输出值
Returns
获取成功返回true

从指定的XML元素中读取值,并转换为模板参数类型。 XML将标量存储为文本,因此解析通过项目的词法转换辅助函数进行。

Template Parameters
T值类型
Parameters
aeleXML元素指针
akey子元素名称,如果为nullptr则读取当前元素
aval输出参数,存储读取的值
Returns
读取成功返回true,否则返回false

◆ get() [2/2]

template<typename T>
bool ngl::xml::get ( tinyxml2::XMLElement * aele,
T & aval )
static

获取元素值(无键名)

从XML元素获取值(简化版本)。

Template Parameters
T值类型
Parameters
aele元素指针
aval输出值
Returns
获取成功返回true
Template Parameters
T值类型
Parameters
aeleXML元素指针
aval输出参数,存储读取的值
Returns
读取成功返回true,否则返回false

◆ get_child() [1/2]

tinyxml2::XMLElement * ngl::xml::get_child ( tinyxml2::XMLDocument & axml,
const char * astr )
static

从文档获取子元素

从XML文档根节点查找子元素

Parameters
axmlXML文档
astr子元素路径
Returns
子元素指针,未找到返回nullptr
Parameters
axmlXML文档对象
astr子元素标签名
Returns
找到的XMLElement指针,未找到返回nullptr

◆ get_child() [2/2]

tinyxml2::XMLElement * ngl::xml::get_child ( tinyxml2::XMLElement * aele,
const char * astr )
static

获取子元素

按点分路径查找子元素

Parameters
aele父元素
astr子元素路径
Returns
子元素指针,未找到返回nullptr

支持"a.b.c"格式的路径,逐层查找嵌套子元素。

Parameters
aele起始父元素
astr点分路径字符串
Returns
找到的XMLElement指针,路径无效或未找到返回nullptr

◆ get_xmlattr()

template<typename T>
bool ngl::xml::get_xmlattr ( tinyxml2::XMLElement * aele,
const char * akey,
T & aval )
static

获取XML属性值

从XML元素获取属性值。

Template Parameters
T值类型
Parameters
aele元素指针
akey属性名
aval输出值
Returns
获取成功返回true

从指定的XML元素中读取属性值,并转换为模板参数类型。

Template Parameters
T值类型
Parameters
aeleXML元素指针
akey属性名称
aval输出参数,存储读取的值
Returns
读取成功返回true,否则返回false

◆ readxml()

bool ngl::xml::readxml ( const char * aname,
tinyxml2::XMLDocument & axml )
static

读取XML文件

从文件加载XML文档

Parameters
aname文件路径
axmlXML文档对象
Returns
读取成功返回true
Parameters
anameXML文件路径
axml输出的XML文档对象
Return values
true加载成功
false文件路径为空或加载失败

◆ set() [1/2]

template<typename T>
bool ngl::xml::set ( tinyxml2::XMLElement * aele,
const char * akey,
const T & aval )
static

设置元素值

设置XML元素的值。

Template Parameters
T值类型
Parameters
aele元素指针
akey键名
aval输入值
Returns
设置成功返回true

将模板参数类型的值转换为字符串并设置到指定的XML元素中。

Template Parameters
T值类型
Parameters
aeleXML元素指针
akey子元素名称,如果为nullptr则设置当前元素
aval要设置的值
Returns
设置成功返回true,否则返回false

◆ set() [2/2]

template<typename T>
bool ngl::xml::set ( tinyxml2::XMLElement * aele,
const T & aval )
static

设置元素值(无键名)

设置XML元素的值(简化版本)。

Template Parameters
T值类型
Parameters
aele元素指针
aval输入值
Returns
设置成功返回true
Template Parameters
T值类型
Parameters
aeleXML元素指针
aval要设置的值
Returns
设置成功返回true,否则返回false

◆ set_child() [1/2]

tinyxml2::XMLElement * ngl::xml::set_child ( tinyxml2::XMLDocument & axml,
const char * astr )
static

在文档中创建子元素

在XML文档根节点下创建新的子元素

Parameters
axmlXML文档
astr子元素名称
Returns
子元素指针,创建失败返回nullptr
Parameters
axmlXML文档对象
astr新子元素标签名
Returns
新创建的XMLElement指针,标签名为空返回nullptr

◆ set_child() [2/2]

tinyxml2::XMLElement * ngl::xml::set_child ( tinyxml2::XMLElement * aele,
const char * astr )
static

创建子元素

在指定父元素下创建新的子元素

Parameters
aele父元素
astr子元素名称
Returns
子元素指针,创建失败返回nullptr
Parameters
aele父元素
astr新子元素标签名
Returns
新创建的XMLElement指针,父元素为空返回nullptr

◆ set_xmlattr()

template<typename T>
bool ngl::xml::set_xmlattr ( tinyxml2::XMLElement * aele,
const char * akey,
const T & aval )
static

设置XML属性值

设置XML元素的属性值。

Template Parameters
T值类型
Parameters
aele元素指针
akey属性名
aval输入值
Returns
设置成功返回true

将模板参数类型的值转换为字符串并设置到指定的XML元素属性中。

Template Parameters
T值类型
Parameters
aeleXML元素指针
akey属性名称
aval要设置的值
Returns
设置成功返回true,否则返回false

◆ str()

std::string ngl::xml::str ( tinyxml2::XMLDocument & axml)
static

将XML文档转换为字符串

将XML文档序列化为字符串

Parameters
axmlXML文档
Returns
XML字符串
Parameters
axmlXML文档对象
Returns
XML内容的字符串表示

◆ writexml()

bool ngl::xml::writexml ( const char * aname,
tinyxml2::XMLDocument & axml )
static

写入XML文件

将XML文档保存到文件

Parameters
aname文件路径
axmlXML文档对象
Returns
写入成功返回true
Parameters
aname目标文件路径
axml要保存的XML文档对象
Return values
true保存成功
false路径为空或保存失败

The documentation for this class was generated from the following files: