当前位置:网站首页>tinyxml2 读取和修改文件
tinyxml2 读取和修改文件
2022-07-02 06:34:00 【懵懂的梦花火】
需要引入tinyxml2
读取文件数据
bool ReadYmlConfig()
{
// 新建一个空文档
tinyxml2::XMLDocument xmlDoc;
// 读取指定的xml文件并判断读取是否成功
tinyxml2::XMLError eResult = xmlDoc.LoadFile("Config/YmlConfig.cas");
if (eResult != tinyxml2::XML_SUCCESS)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_NOT_FOUND);
return false;
}
// 获得该文件的第一个节点(根节点)
tinyxml2::XMLNode * pRoot = xmlDoc.FirstChildElement("YmlConfig");
if (pRoot == nullptr)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_READ_ERROR);
return false;
}
else
{
// 获取到CameraConfig的节点
tinyxml2::XMLElement * pCameraGroupElement = pRoot->FirstChildElement("CameraConfig");
// 获取节点下的数据
m_strUsbCameraConfigPath = pCameraGroupElement->FirstChildElement("UsbCameraConfig")->GetText();
m_strWebCameraConfigPath = pCameraGroupElement->FirstChildElement("WebCameraConfig")->GetText();
// 获取到SaveConfig的节点
tinyxml2::XMLElement * pSaveConfigElement = pRoot->FirstChildElement("SaveConfig");
mVideoPath = pSaveConfigElement->FirstChildElement("SavePath")->GetText();
mVideoName = pSaveConfigElement->FirstChildElement("SaveVideName")->GetText();
mVideoWidth = std::atoi(pSaveConfigElement->FirstChildElement("SaveVideoWidth")->GetText());
mVideoHeight = std::atoi(pSaveConfigElement->FirstChildElement("SaveVideoHeight")->GetText());
mMarkerLength = std::atof(pSaveConfigElement->FirstChildElement("SaveMarkerLength")->GetText());
m_nSpaceSize = std::atoi(pSaveConfigElement->FirstChildElement("SaveSpaceSize")->GetText());
return true;
}
return false;
}
修改文件数据
bool SaveConfig()
{
// 新建一个空文档
tinyxml2::XMLDocument xmlDoc;
// 读取指定的xml文件并判断读取是否成功
tinyxml2::XMLError eResult = xmlDoc.LoadFile("Config/YmlConfig.cas");
if (eResult != tinyxml2::XML_SUCCESS)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_NOT_FOUND);
return false;
}
// 获得该文件的第一个节点(根节点)
tinyxml2::XMLNode * pRoot = xmlDoc.FirstChildElement("YmlConfig");
if (pRoot == nullptr)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_READ_ERROR);
return false;
}
else
{
// 获取到SaveConfig的节点
tinyxml2::XMLElement * pSaveConfigElement = pRoot->FirstChildElement("SaveConfig");
// 获取到需要修改的节点
tinyxml2::XMLElement* _savePath = pSaveConfigElement->FirstChildElement("SavePath");
// 内容是否被修改
if (_savePath->GetText() != ui.mPathLineEdit->text().toStdString().c_str())
{
// 修改内容
_savePath->SetText(ui.mPathLineEdit->text().toStdString().c_str());
}
tinyxml2::XMLElement* saveVideName = pSaveConfigElement->FirstChildElement("SaveVideName");
if (saveVideName->GetText() != ui.mVideoNameEdit->text().toStdString().c_str())
saveVideName->SetText(ui.mVideoNameEdit->text().toStdString().c_str());
tinyxml2::XMLElement* saveVideoWidth = pSaveConfigElement->FirstChildElement("SaveVideoWidth");
if (saveVideoWidth->GetText() != ui.mWidthSpinBox->text().toStdString().c_str())
saveVideoWidth->SetText(ui.mWidthSpinBox->text().toStdString().c_str());
tinyxml2::XMLElement* saveVideoHeight = pSaveConfigElement->FirstChildElement("SaveVideoHeight");
if (saveVideoHeight->GetText() != ui.mHeightSpinBox->text().toStdString().c_str())
saveVideoHeight->SetText(ui.mHeightSpinBox->text().toStdString().c_str());
tinyxml2::XMLElement* _markerLength = pSaveConfigElement->FirstChildElement("SaveMarkerLength");
double _length = ui.mLabelLength->text().toInt() / 100.0;
if (std::atof(_markerLength->GetText()) != _length)
_markerLength->SetText(std::to_string(_length).c_str());
tinyxml2::XMLElement* _saveSpace = pSaveConfigElement->FirstChildElement("SaveSpaceSize");
if (_saveSpace->GetText() != ui.mSpaceSize->text().toStdString().c_str())
_saveSpace->SetText(ui.mSpaceSize->text().toStdString().c_str());
xmlDoc.SaveFile("Config/YmlConfig.cas");
return true;
}
return false;
}
边栏推荐
- 洞见云原生|微服务及微服务架构浅析
- What are the differences between TP5 and laravel
- Chrome用户脚本管理器-Tampermonkey 油猴
- Knife4j 2. Solution to the problem of file control without selection when uploading x version files
- 图像识别-数据采集
- MySql报错:unblock with mysqladmin flush-hosts
- Oracle modifies tablespace names and data files
- C语言之做木桶
- c语言编程题
- idea查看字节码配置
猜你喜欢

Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue

Learn combinelatest through a practical example

web安全与防御
![[go practical basis] how to set the route in gin](/img/23/f38d68c4fd238d453b9a7670483002.png)
[go practical basis] how to set the route in gin

Troubleshooting and handling of an online problem caused by redis zadd

Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc

Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程

在SQL注入中,为什么union联合查询,id必须等于0

Mysql 多列IN操作

一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
随机推荐
自定義Redis連接池
Enterprise level SaaS CRM implementation
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
图像识别-数据清洗
BugkuCTF-web16(备份是个好习惯)
图像识别-数据标注
微服务实战|熔断器Hystrix初体验
College Students' CET-4 and CET-6 composition template (self created version, successfully crossed CET-6)
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
图像识别-数据采集
idea查看字节码配置
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
Chrome browser plug-in fatkun installation and introduction
道阻且长,行则将至
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Chrome video download Plug-in – video downloader for Chrome
Customize redis connection pool