c++解析xml

下载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;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值