当前位置:网站首页>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;
}
边栏推荐
- Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
- Bold prediction: it will become the core player of 5g
- College Students' CET-4 and CET-6 composition template (self created version, successfully crossed CET-6)
- How to use pyqt5 to make a sensitive word detection tool
- vs+qt 设置应用程序图标
- Fragmenttabhost implements the interface of housing loan calculator
- Microservice practice | load balancing component and source code analysis
- Supplier selection and prequalification of Oracle project management system
- Microservice practice | declarative service invocation openfeign practice
- Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
猜你喜欢
自定义Redis连接池
微服务实战|微服务网关Zuul入门与实战
FragmentTabHost实现房贷计算器界面
Operation and application of stack and queue
vs+qt 设置应用程序图标
Microservice practice | declarative service invocation openfeign practice
图像识别-数据标注
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
Say goodbye to 996. What are the necessary plug-ins in idea?
Microservice practice | fuse hytrix initial experience
随机推荐
YOLO物体识别,生成数据用到的工具
What are the differences between TP5 and laravel
破茧|一文说透什么是真正的云原生
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
VIM operation command Encyclopedia
Idempotent design of Internet API interface
Read Day5 30 minutes before going to bed every day_ All key values in the map, how to obtain all value values
微服务实战|微服务网关Zuul入门与实战
大学生四六级作文模板(自创版,成功跨过六级)
图像识别-数据清洗
Mathematics in machine learning -- point estimation (I): basic knowledge
vs+qt 设置应用程序图标
The channel cannot be viewed when the queue manager is running
JVM指令助记符
How to install PHP in CentOS
Read 30 minutes before going to bed every day_ day3_ Files
记录一下初次使用Xray的有趣过程
Oracle modify database character set
图像识别-数据增广
Read Day6 30 minutes before going to bed every day_ Day6_ Date_ Calendar_ LocalDate_ TimeStamp_ LocalTime