下载tinyxml,并把它添加到项目中,
#include "tinyxml.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <cstring>
#include "fstream"
#include <io.h>
#include <vector>
#include <algorithm>
using namespace std;
void loadXML(string XmlFilePath)
{
ofstream fout;
TiXmlDocument doc;//申明一个文档类型变量,用来存储读取的xml文档
if (!doc.LoadFile(XmlFilePath.c_str())) //检测xml文档是否存在
{
cerr << doc.ErrorDesc() << endl;
}
TiXmlElement* root = doc.FirstChildElement();//指向xml文档的根元素
if (root == NULL)//检测根元素存在性
{
cerr << "Failed to load file: No root element." << endl;
doc.Clear();
}
//elem指向根的第一个孩子元素
for (TiXmlElement* elem0 = root->FirstChildElement(); elem0 != NULL; elem0 = elem0->NextSiblingElement())
{
if (strcmp(elem0->Value(), "object") == 0)
{
TiXmlElement* name = elem0->FirstChildElement();
TiXmlNode* name_value = name->FirstChild();
string img_name = name_value->Value();
cout << "name:" << img_name << endl;
for (TiXmlElement* elem1 = elem0->FirstChildElement(); elem1 != NULL; elem1 = elem1->NextSiblingElement())
{
if (strcmp(elem1->Value(), "bndbox") == 0)
{
vector<double>box_width;
for (TiXmlElement* elem2 = elem1->FirstChildElement(); elem2 != NULL; elem2 = elem2->NextSiblingElement())
{
TiXmlNode* node_value = elem2->FirstChild();
cout << elem2->Value() << ":" << node_value->Value() << endl;
//if (node_value->Value())
//{
// double data = atoi(node_value->Value());
//}
}
}
}
}
inline void GetFileAll(string path, vector<string> &dirpath){
struct _finddata_t fileinfo;
long long ld;
if ((ld = _findfirst((path + "\\*").c_str(), &fileinfo)) != -1l){
do{
if ((fileinfo.attrib&_A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){
GetFileAll(path + "\\" + fileinfo.name, dirpath);
//cout <<"filename:"<< fileinfo.name << endl;
}
}
else
{
dirpath.push_back(path + "\\" + fileinfo.name);
/*cout << fileinfo.name << endl;*/
}
} while (_findnext(ld, &fileinfo) == 0);
_findclose(ld);
}
}
int main()
{
string firpath = "G:\\class\\data\\new\\";
std::vector<std::string> veTotalNames;
vector<string> dirpath;
GetFileAll(firpath, dirpath);
for (int i = 0; i <dirpath.size(); ++i)
{
string XmlFilePath = dirpath[i];
loadXML(XmlFilePath);
}
system("pause");
return 0;
}