xml解析及编译汇总 valgrind检测内存泄露 http://blog.csdn.net/lifan5/article/details/8030285

xml解析及编译汇总

分类: c语言   1039人阅读  评论(0)  收藏  举报
[cpp]  view plain copy
  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <stdlib.h>   
  4. #include <libxml/xmlmemory.h>   
  5. #include <libxml/parser.h>  
  6. #include <libxml/tree.h>  
  7.   
  8.   
  9. void   
  10. get_connnectrequestUrl(char *docname) {   
  11.     xmlDocPtr doc;   
  12.     xmlNodePtr cur;   
  13.     doc = xmlParseFile(docname);   
  14.     if (doc == NULL ) {   
  15.         printf("Document not parsed successfully. \n");   
  16.         return;   
  17.     }   
  18.     cur = xmlDocGetRootElement(doc);   
  19.     if (cur == NULL) {   
  20.         printf("empty document\n");   
  21.         xmlFreeDoc(doc);   
  22.         return;   
  23.     }   
  24.     if (xmlStrcmp(cur->name, (const xmlChar *) "Context")) {   
  25.         printf("document of the wrong type, root node != Context");   
  26.         xmlFreeDoc(doc);   
  27.         return;   
  28.     }   
  29.     cur = cur->xmlChildrenNode;   
  30.   
  31.     while (cur != NULL){  
  32.         xmlNodePtr cur2_tmp = cur;  
  33.         xmlChar *key;  
  34.         xmlChar *attr;  
  35.         char connectrequestUrl[32];  
  36.         if ((!xmlStrcmp(cur->name, (const xmlChar *)"list"))){  
  37.             attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);  
  38.             if((!xmlStrcmp(attr,  (const xmlChar *)"rrm_cell"))){  
  39.                 xmlFree(attr);  
  40.                 cur = cur->xmlChildrenNode;  
  41.                 while(cur != NULL){  
  42.                     xmlNodePtr cur2_tmp = cur;  
  43.                     if ((!xmlStrcmp(cur->name, (const xmlChar *)"item"))){  
  44.                         attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);  
  45.                         if((!xmlStrcmp(attr,  (const xmlChar *)"Cell_Map_s"))){  
  46.                             xmlFree(attr);  
  47.                             cur = cur->xmlChildrenNode;  
  48.                             while(cur != NULL){  
  49.                                 xmlNodePtr cur2_tmp = cur;  
  50.                                 if ((!xmlStrcmp(cur->name, (const xmlChar *)"p"))){  
  51.                                     attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);  
  52.                                     if((!xmlStrcmp(attr,  (const xmlChar *)"Hnb_IP"))){  
  53.                                         xmlFree(attr);  
  54.                                         printf("********************\n");  
  55.                                         key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);  
  56.                                         memcpy(connectrequestUrl,(const char *)key,strlen((const char *)key));  
  57.                                         connectrequestUrl[strlen((const char *)key)]='\0';  
  58.                                         printf("the url is:%s\n",connectrequestUrl);  
  59.   
  60.                                         xmlFree(key);  
  61.                                         }  
  62.                                     else{  
  63.                                         xmlFree(attr);  
  64.                                         }  
  65.                                     }  
  66.   
  67.                                 /*reset cul*/  
  68.                                 cur = cur2_tmp;  
  69.                                 cur = cur -> next;  
  70.                                 }  
  71.                             }  
  72.                         else{  
  73.                             xmlFree(attr);  
  74.                             }  
  75.                           
  76.                         }  
  77.                           
  78.   
  79.   
  80.   
  81.                     /*RESET cur*/  
  82.                     cur = cur2_tmp;  
  83.                     cur = cur -> next;  
  84.                     }  
  85.                   
  86.                 }  
  87.             else{  
  88.                 xmlFree(attr);  
  89.                 }  
  90.               
  91.               
  92.             }  
  93.         cur = cur2_tmp;  
  94.         cur = cur -> next;  
  95.         }  
  96.         xmlFreeDoc(doc);  
  97.   
  98.         }  
  99.   
  100.   
  101.         int main(void){  
  102.             while(1){  
  103.             get_connnectrequestUrl("config.xml");  
  104.             sleep(5);  
  105.             //return 0;  
  106.         }  
  107.         }  
  108.           

编译:gcc -o testxml4 testxml4.c -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2

使用valgrind检测内存泄露的情况:valgrind --tool=memcheck --leak-check=full ./testxml4

交叉编译: arm-none-linux-gnueabi-gcc -o testxml4 testxml4.c -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值