pugixml XML配置文件 的增删改查

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<SXConfiguration>
	<LocalSetting>
		<VMSLoginServer>
			<exitServerName>2</exitServerName>
			<server>
				<name>2</name>
				<ip>152.131.121.131</ip>
				<port>13313</port>
			</server>
			<server>
				<name>23</name>
				<ip>168.202.32.232</ip>
				<port>54331</port>
			</server>
		</VMSLoginServer>
	</LocalSetting>
</SXConfiguration>

对应:加载、增加、删除操作

Erlang 复制代码
m_VMSServerNode = m_LocalNode.child("VMSLoginServer");
    if (m_VMSServerNode.empty()) {
        DEBUG_LOG(ERROR, "Load config file failed[LocalSetting]! path = %s\n", m_File.c_str());
        return false;
    }


if (!LoadVMSServerConfig()) {
        return false;
    }


bool LocalSetting::LoadVMSServerConfig()
{
    if (m_VMSServerNode.empty()) {
        return false;
    }
    for (pugi::xml_node serverNode : m_VMSServerNode.children("server")) {
        QString name = QString::fromStdString(serverNode.child_value("name"));
        QString ip = QString::fromStdString(serverNode.child_value("ip"));
        int port = serverNode.child("port").text().as_int();
        if (name == "" || ip == "" || port == 0) {
            return false;
        }
        m_vmsserver_hash[name] = { ip, port };
    }
    return true;
}

QHash<QString, VMSServerInfo> LocalSetting::GetVMSServerInfo()
{
    return m_vmsserver_hash;
}

bool LocalSetting::SetVMSServerValue(QString name, QString ip, int port)
{
    if (!m_VMSServerNode.empty()) {
        if (m_vmsserver_hash.contains(name)){
            RemoveVMSServer(name);
        }
        m_vmsserver_hash.insert(name, { ip, port });

        // 添加新的服务器节点
        bool ret = false;
        auto serverNode = m_VMSServerNode.append_child("server");
        ret = serverNode.append_child("name").append_child(pugi::node_pcdata).set_value(name.toStdString().c_str());
        if (!ret) return false;
        ret = serverNode.append_child("ip").append_child(pugi::node_pcdata).set_value(ip.toStdString().c_str());
        if (!ret) return false;
        ret = serverNode.append_child("port").append_child(pugi::node_pcdata).set_value(QString::number(port).toStdString().c_str());
        if (!ret) return false;
        m_Doc.save_file(m_File.c_str(), "\t", 1U, pugi::encoding_utf8);
        return true;
    }
}

bool LocalSetting::SetExitServerName(QString name)
{
    if (!m_VMSServerNode.empty()) {
        pugi::xml_node exitServerName = m_VMSServerNode.child("exitServerName");
        if (exitServerName) {
            std::string str_name = name.toStdString();
            const char* tem_name = str_name.c_str();
            bool ret = exitServerName.text().set(tem_name);
            if (ret) {
                m_Doc.save_file(m_File.c_str(), "\t", 1U, pugi::encoding_utf8);
            }
        }
        else
        {
            m_VMSServerNode.append_child("exitServerName");
            DEBUG_LOG(ERROR, "exitServerName node not found!/n");
            return false;
        }
    }
}

QString LocalSetting::GetExitServerName()
{
    if (!m_VMSServerNode.empty()) {
        pugi::xml_node exitServerName = m_VMSServerNode.child("exitServerName");
        if (exitServerName) {
            QString name = exitServerName.text().as_string("");
            if (name != NULL) {
                return name;
            }
        }
    }
    return QString();
}

bool LocalSetting::RemoveVMSServer(const QString& name) {
    // 检查名称是否存在
    if (!m_vmsserver_hash.contains(name)) {
        //qWarning() << "Server not found:" << name;
        return false;
    }

    // 遍历当前 XML 文件中的所有服务器节点,并根据 serverMap 更新或删除
    for (auto serverNode : m_VMSServerNode.children("server")) {
        QString server_node_name = QString::fromStdString(serverNode.child_value("name"));
        // 如果 serverMap 中有这个服务器的信息,则更新或者删除它
        if (name == server_node_name) {
            serverNode.parent().remove_child(serverNode);
            // 从服务器映射表中移除
            m_vmsserver_hash.remove(name);
        }
    }
    int ret = m_Doc.save_file(m_File.c_str(), "\t", 1U, pugi::encoding_utf8);
    if (!ret) return false;
    return true;
}
相关推荐
P7进阶路3 小时前
72.是否可以把所有Bean都通过Spring容器来管理?(Spring的applicationContext.xml中配置全局扫 描)
xml·java·spring
迷失蒲公英8 小时前
XML与Go结构互转实现(序列化及反序列化)
xml·开发语言·golang
小狗蛋ing2 天前
反编译APK获取xml资源
xml·反编译
yqcoder2 天前
同源策略详解
xml·前端·javascript
油丶酸萝卜别吃2 天前
MyBatis中XML文件的模板
xml·数据库·mybatis
武子康3 天前
大数据-259 离线数仓 - Griffin架构 修改配置 pom.xml sparkProperties 编译启动
xml·java·大数据·hive·hadoop·架构
seasugar3 天前
Maven怎么会出现一个dependency-reduced-pom.xml的文件
xml·java·maven
SEO-狼术4 天前
Oxygen XML Developer Crack
xml
北辰浮光5 天前
[spring]XML配置文件标签
xml·spring