当前位置:网站首页>Tinyxml2 reading and modifying files
Tinyxml2 reading and modifying files
2022-07-02 09:39:00 【Ignorant dream fireworks】
Need to introduce tinyxml2
Read file data
bool ReadYmlConfig()
{
// Create a new empty document
tinyxml2::XMLDocument xmlDoc;
// Read the specified xml File and judge whether the reading is successful
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;
}
// Get the first node of the file ( The root node )
tinyxml2::XMLNode * pRoot = xmlDoc.FirstChildElement("YmlConfig");
if (pRoot == nullptr)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_READ_ERROR);
return false;
}
else
{
// Get CameraConfig The node of
tinyxml2::XMLElement * pCameraGroupElement = pRoot->FirstChildElement("CameraConfig");
// Get the data under the node
m_strUsbCameraConfigPath = pCameraGroupElement->FirstChildElement("UsbCameraConfig")->GetText();
m_strWebCameraConfigPath = pCameraGroupElement->FirstChildElement("WebCameraConfig")->GetText();
// Get SaveConfig The node of
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;
}
Modify file data
bool SaveConfig()
{
// Create a new empty document
tinyxml2::XMLDocument xmlDoc;
// Read the specified xml File and judge whether the reading is successful
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;
}
// Get the first node of the file ( The root node )
tinyxml2::XMLNode * pRoot = xmlDoc.FirstChildElement("YmlConfig");
if (pRoot == nullptr)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_READ_ERROR);
return false;
}
else
{
// Get SaveConfig The node of
tinyxml2::XMLElement * pSaveConfigElement = pRoot->FirstChildElement("SaveConfig");
// Get the node that needs to be modified
tinyxml2::XMLElement* _savePath = pSaveConfigElement->FirstChildElement("SavePath");
// Whether the content has been modified
if (_savePath->GetText() != ui.mPathLineEdit->text().toStdString().c_str())
{
// Modify the content
_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;
}
边栏推荐
- Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
- Knife4j 2. Solution to the problem of file control without selection when uploading x version files
- Read 30 minutes before going to bed every day_ day3_ Files
- tinyxml2 读取和修改文件
- Read Day6 30 minutes before going to bed every day_ Day6_ Date_ Calendar_ LocalDate_ TimeStamp_ LocalTime
- Knife4j 2.X版本文件上传无选择文件控件问题解决
- Required request body is missing:(跨域问题)
- Insight into cloud native | microservices and microservice architecture
- Micro service practice | introduction and practice of zuul, a micro service gateway
- C语言之分草莓
猜你喜欢

每天睡觉前30分钟阅读_day4_Files

并网逆变器PI控制(并网模式)

Hystrix implements request consolidation

tinyxml2 读取和修改文件

自定義Redis連接池

图像识别-数据标注

三相并网逆变器PI控制——离网模式

BugkuCTF-web21(详细解题思路及步骤)

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd

Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
随机推荐
MySQL事务
Chrome浏览器标签管理插件–OneTab
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
C语言之数据插入
别找了,Chrome浏览器必装插件都在这了
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
图像识别-数据增广
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
TD联合Modelsim进行功能仿真
Typora安装包分享
Amq6126 problem solving ideas
ZK configuration center -- configuration and use of config Toolkit
Mathematics in machine learning -- point estimation (I): basic knowledge
VIM operation command Encyclopedia
大学生四六级作文模板(自创版,成功跨过六级)
上班第一天的报错(AWVS卸载不彻底)
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
个人经历&&博客现状
VIM操作命令大全