#include "tinyxml.h"
#include "tinystr.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <iostream>
#include <vector>
#include <regex>
#include <fstream>
#pragma comment(lib,"tinyxml.lib")
using namespace std;
class path_save
{
public:
std::string file_name;
std::string file_path;
};
int find_all_files(const char * lpPath, std::vector<path_save>&lines_)
{
path_save txt_line;
using namespace std;
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind, lpPath);
strcat(szFind, "/*.*");
HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind)
return -1;
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
{
//发现子目录,递归之
char szFile[MAX_PATH] = { 0 };
strcpy(szFile, lpPath);
strcat(szFile, "/");
strcat(szFile, FindFileData.cFileName);
find_all_files(szFile, lines_);
}
}
else
{
//找到文件,处理之
char szFilePath[MAX_PATH] = { 0 };
sprintf_s(szFilePath, MAX_PATH, "%s/%s", lpPath, FindFileData.cFileName);
//string filename;
//filename = szFilePath;
//判断文件后缀
char* p = strrchr(FindFileData.cFileName, '.');
if (p)
{
//if (strcmp(p + 1, "avi") == 0 || strcmp(p + 1, "AVI") == 0)
{
txt_line.file_name = FindFileData.cFileName;
txt_line.file_path = szFilePath;
lines_.push_back(txt_line);
//std::cout << szFilePath << std::endl;
}
}
}
} while (::FindNextFile(hFind, &FindFileData));
::FindClose(hFind);
return 0;
}
int main()
{
vector<path_save>xml_path;
find_all_files("E:/VS2013project/trainxml22", xml_path);
vector<path_save>::const_iterator iter = xml_path.begin();
for (; iter != xml_path.end();)
{
TiXmlDocument *myDocument = new TiXmlDocument((*iter).file_path.c_str());
myDocument->LoadFile();
//获得根元素
TiXmlElement *RootElement = myDocument->RootElement();
//输出根元素名称
cout << (*iter).file_path.c_str() << endl;
cout << RootElement->Value() << endl;
//获得指定节点。
TiXmlElement *First = RootElement->FirstChildElement("object");
while (First != nullptr)
{
TiXmlElement *First2 = First->FirstChildElement("bndbox");
TiXmlElement *First3 = First2->FirstChildElement("ymin");
TiXmlElement *First4 = First2->FirstChildElement("ymax");
if (First3) // 存在这个节点
{
cout << First3->GetText() << endl;
int ymin = atoi(First3->GetText());
ymin = ymin*0.75;
string s;
s = to_string(ymin);
First3->FirstChild()->SetValue(s.c_str());
cout << First3->GetText() << endl;
}
else
{
cout << "不存在这个节点" << endl;
}
if (First4) // 存在这个节点
{
cout << First4->GetText() << endl;
int ymax = atoi(First4->GetText());
ymax = ymax*0.75;
string s;
s = to_string(ymax);
First4->FirstChild()->SetValue(s.c_str());
cout << First4->GetText() << endl;
myDocument->SaveFile(); // 保存文件
}
else
{
cout << "不存在这个节点" << endl;
}
First = First->NextSiblingElement("object");
}
iter++;
}
}
tinyxml修改XML内容
最新推荐文章于 2023-08-03 17:11:26 发布