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;
}