tinyxml修改XML内容

#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++;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值