Chinaunix首页 | 论坛 | 博客
  • 博客访问: 472707
  • 博文数量: 285
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 629
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-14 17:53
个人简介

相信自己,快乐每一天

文章分类

全部博文(285)

分类: C/C++

2014-06-12 16:12:40

    最近项目有项递归目录,保存为xml的工作,运用了libxml2这个开源库,对xml进行了详细的理解。简单介绍下思路,就是递归所有目录和子文件,生成一颗正常的树,并将这棵树写如xml。功能简单,主要是记录下libxml的使用方法   废话少说,直接上代码

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <list>
  6. #include <iostream>
  7. #ifdef __cplusplus
  8. extern "C"
  9. {
  10. #endif
  11. #include <libxml/parser.h>
  12. #include <libxml/tree.h>
  13. #ifdef __cplusplus
  14. }
  15. #endif

  16. struct node_t{                          //递归树的结构,用的是儿子列表法
  17.     std::string name;
  18.     std::list<struct node_t*> pChild;
  19. };
  20. typedef struct node_t node;

  21. node root;
  22. xmlDocPtr doc = NULL;
  23. xmlNodePtr root_node = NULL;
  24. void List(const char *path, int level,node *parentnode,xmlNodePtr parentXmlNode)
  25. {
  26.     struct dirent *ent = NULL;
  27.     DIR *pDir;
  28.     if((pDir = opendir(path)) != NULL)
  29.     {
  30.         while(NULL != (ent = readdir(pDir)))
  31.         {
  32.             node *tmpnode = new node;
  33.             if(!tmpnode)
  34.             {
  35.                 std::cout<<"alloc node errorn";
  36.                 return ;
  37.             }

  38.             xmlNodePtr tmpxmlnode;

  39.             if(ent->d_type == 8 && ent->d_name[0] != '.') // d_type:8-文件,4-目录
  40.             {
  41.                 tmpnode->name = ent->d_name;
  42.                 parentnode->pChild.push_back(tmpnode);
  43.                 tmpxmlnode = xmlNewChild(parentXmlNode,NULL,BAD_CAST ent->d_name,BAD_CAST tmpnode->name.c_str());
  44.             }
  45.             else if(ent->d_name[0] != '.')
  46.             {
  47.                 char *p = (char*)malloc(strlen(path) + strlen(ent->d_name) + 2);
  48.                 strcpy(p, path);
  49.                 strcat(p, "/");
  50.                 strcat(p, ent->d_name);
  51.                 tmpnode->name = p;
  52.                 parentnode->pChild.push_back(tmpnode);
  53.                 tmpxmlnode = xmlNewChild(parentXmlNode,NULL,BAD_CAST ent->d_name,BAD_CAST ent->d_name);
  54.                 List(p, level+1, tmpnode, tmpxmlnode); // 递归遍历子目录
  55.                 free(p);
  56.             }
  57.         }
  58.         closedir(pDir);
  59.     }

  60. }

  61. void testcase()
  62. {
  63.     std::list<struct node_t*>::iterator itr = (*(*(*(root.pChild.begin()))->pChild.begin())->pChild.begin())->pChild.begin();

  64.     int num = root.pChild.size();
  65.     for(;itr != (*(*(*(root.pChild.begin()))->pChild.begin())->pChild.begin())->pChild.end(); itr++)
  66.     {
  67.         std::cout<<"path is "<<(*itr)->name<<std::endl;
  68.     }
  69. }

  70. int main()
  71. {
  72.     std::string path = "/media/disk/ffmpeg-0.9.2/mytest";
  73.     root.name = path;
  74.     doc = xmlNewDoc(BAD_CAST "1.0");
  75.     root_node = xmlNewNode(NULL, BAD_CAST "root");
  76.     xmlDocSetRootElement(doc,root_node);
  77.     List(path.c_str(), 0, &root, root_node);
  78.     //testcase();

  79.     xmlSaveFormatFileEnc("test.xml",doc,"UTF-8",1);
  80.     if(doc)
  81.     {
  82.         xmlFreeDoc(doc);
  83.     }
  84.     xmlCleanupParser();
  85.     return 0;
  86. }

  
阅读(589) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~