当前位置:网站首页>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;
}
边栏推荐
- Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- Safety production early warning system software - Download safety production app software
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- Micro service practice | introduction and practice of zuul, a micro service gateway
- Operation and application of stack and queue
- zk配置中心---Config Toolkit配置与使用
- 一次聊天勾起的回忆
- Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
- Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
猜你喜欢

定时线程池实现请求合并

In depth analysis of how the JVM executes Hello World

概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法

Supplier selection and prequalification of Oracle project management system

攻防世界-Web进阶区-unserialize3

Mysql默认事务隔离级别及行锁

Microservice practice | Eureka registration center and cluster construction
![[go practical basis] how to verify request parameters in gin](/img/de/50db131d6993e5d955e3416c667c4c.png)
[go practical basis] how to verify request parameters in gin

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

How to use pyqt5 to make a sensitive word detection tool
随机推荐
Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Ora-12514 problem solving method
Read 30 minutes before going to bed every day_ day4_ Files
Chrome browser tag management plug-in – onetab
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Microservice practice | fuse hytrix initial experience
每天睡觉前30分钟阅读_day4_Files
In depth analysis of how the JVM executes Hello World
微服务实战|Eureka注册中心及集群搭建
Hystrix implements request consolidation
图像识别-数据采集
Chrome浏览器标签管理插件–OneTab
微服务实战|声明式服务调用OpenFeign实践
What are the waiting methods of selenium
自定義Redis連接池
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Redis installation and deployment (windows/linux)
How to choose between efficiency and correctness of these three implementation methods of distributed locks?
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)