当前位置:网站首页>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;
}
边栏推荐
- 图像识别-数据采集
- Ora-12514 problem solving method
- BugkuCTF-web21(详细解题思路及步骤)
- Chrome video download Plug-in – video downloader for Chrome
- Who is better for Beijing software development? How to find someone to develop system software
- 记录下对游戏主机配置的个人理解与心得
- Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
- Oracle modify database character set
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- Flink - use the streaming batch API to count the number of words
猜你喜欢
图像识别-数据标注
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
Microservice practice | teach you to develop load balancing components hand in hand
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
数构(C语言)——第四章、矩阵的压缩存储(下)
西瓜书--第五章.神经网络
zk配置中心---Config Toolkit配置与使用
FragmentTabHost实现房贷计算器界面
数构(C语言--代码有注释)——第二章、线性表(更新版)
随机推荐
Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
BugkuCTF-web24(解题思路及步骤)
深入剖析JVM是如何执行Hello World的
Chrome browser tag management plug-in – onetab
What are the differences between TP5 and laravel
MySQL error: unblock with mysqladmin flush hosts
图像识别-数据标注
Watermelon book -- Chapter 5 neural network
道阻且长,行则将至
Micro service practice | introduction and practice of zuul, a micro service gateway
西瓜书--第六章.支持向量机(SVM)
JVM instruction mnemonic
微服务实战|手把手教你开发负载均衡组件
Enterprise level SaaS CRM implementation
Watermelon book -- Chapter 6 Support vector machine (SVM)
图像识别-数据清洗
Matplotlib剑客行——布局指南与多图实现(更新)
上班第一天的报错(AWVS卸载不彻底)
[go practical basis] how to customize and use a middleware in gin
Customize redis connection pool