当前位置:网站首页>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;
}
边栏推荐
- Thinkphp5 how to determine whether a table exists
- web安全与防御
- Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
- How to install PHP in CentOS
- 上班第一天的报错(AWVS卸载不彻底)
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- Knife4j 2. Solution to the problem of file control without selection when uploading x version files
- 告别996,IDEA中必装插件有哪些?
- 微服务实战|微服务网关Zuul入门与实战
- c语言编程题
猜你喜欢
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Microservice practice | fuse hytrix initial experience
Chrome user script manager tempermonkey monkey
[go practical basis] how to verify request parameters in gin
Chrome浏览器插件-Fatkun安装和介绍
c语言编程题
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
Microservice practice | declarative service invocation openfeign practice
The channel cannot be viewed when the queue manager is running
洞见云原生|微服务及微服务架构浅析
随机推荐
BugkuCTF-web21(详细解题思路及步骤)
Insight into cloud native | microservices and microservice architecture
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Ora-12514 problem solving method
Who is better for Beijing software development? How to find someone to develop system software
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
Solution to amq4036 error in remote connection to IBM MQ
数构(C语言--代码有注释)——第二章、线性表(更新版)
分享一篇博客(水一篇博客)
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
kinect dk 获取CV::Mat格式的彩色RGB图像(openpose中使用)
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Oracle delete tablespace and user
ClassFile - Attributes - Code
Knife4j 2.X版本文件上传无选择文件控件问题解决
Flink - use the streaming batch API to count the number of words