libxml2 解析文档的例子

一、libxml2 解析文档的例子(mingw)

  1. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. //在mingw环境下,xmlfree 等可能出现问题,见http://blog.csdn.net/king_on/article/details/7543577   
  5. #define IN_LIBXML   
  6.   
  7. #include <libxml/parser.h>   
  8. #include <libxml/tree.h>   
  9.   
  10. /** 
  11. 判断节点是否是叶子节点 
  12. 1. 如果node=NULL,return 0 
  13. 2. 如果node->type不是XML_ELEMENT_NODE,return 0 
  14. 3. 如果node->type为XML_ELEMENT_NODE,并且node->children中没有XML_ELEMENT_NODE类型的节点,那么return 1 
  15. */  
  16. int isLeafNode(xmlNode *node)  
  17. {  
  18.     if(node==NULL)  
  19.         return(0);  
  20.   
  21.     if(node->type!=XML_ELEMENT_NODE)  
  22.         return(0);  
  23.   
  24.     xmlNode *childNode;  
  25.     for(childNode=node->children;childNode!=NULL;childNode=childNode->next)  
  26.     {  
  27.         if(childNode->type==XML_ELEMENT_NODE)  
  28.             return(0);  
  29.     }  
  30.     return(1);  
  31. }  
  32. void print(xmlDocPtr doc,xmlNode *rootNode)  
  33. {  
  34.     xmlNode *curNode;  
  35.     xmlChar *v;  
  36.     for(curNode=rootNode; curNode!=NULL; curNode=curNode->next)  
  37.     {  
  38.         if(curNode->type==XML_ELEMENT_NODE)  
  39.         {  
  40.             printf("node name:%s",curNode->name);  
  41.             if(isLeafNode(curNode))  
  42.             {  
  43.                 v=xmlNodeGetContent(curNode);//获取节点内容   
  44.                 printf("\tnode value:%s",v);  
  45.                 xmlFree(v);//施放v   
  46.             }  
  47.             xmlAttr *attr=curNode->properties;//首个节点或NULL   
  48.             while(attr!=NULL)  
  49.             {  
  50.                 v=xmlGetProp(curNode,attr->name);//获取属性内容   
  51.                 printf("\tattr(%s)=%s",attr->name,v);  
  52.                 xmlFree(v);//施放v   
  53.                 attr=attr->next;  
  54.             }  
  55.             printf("\n");  
  56.         }  
  57.         print(doc,curNode->children);  
  58.     }  
  59. }  
  60. int main(int argv,char *argc[])  
  61. {  
  62.     LIBXML_TEST_VERSION  
  63.   
  64.     xmlDocPtr doc;  
  65.     //XML_PARSE_NOBLANKS 在解析时忽略空节点   
  66.     doc=xmlReadFile(argc[1],NULL,XML_PARSE_NOBLANKS);  
  67.   
  68.     //在mingw环境下,xmlfree 等可能出现问题,见http://blog.csdn.net/king_on/article/details/7543577   
  69.     if(!xmlFree) xmlMemGet(&xmlFree,&xmlMalloc,&xmlRealloc,NULL);  
  70.   
  71.     xmlNode *rootNode=xmlDocGetRootElement(doc);  
  72.     print(doc,rootNode);  
  73.   
  74.     xmlFreeDoc(doc);  
  75.     xmlCleanupParser();  
  76.   
  77.     return(0);  
  78. }  

 

二、libxml2 xmlTextReader 解析xml实例

  1. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. #define IN_LIBXML   
  5.   
  6. #include <libxml/xmlreader.h>   
  7.   
  8. static FILE *file;  
  9. void printAttribute(xmlTextReaderPtr reader)  
  10. {  
  11.     if(1==xmlTextReaderHasAttributes(reader))  
  12.     {  
  13.         xmlChar *name,*value;  
  14.         int res=xmlTextReaderMoveToFirstAttribute(reader);//首先移动current instance 指向第一个元素   
  15.         while(1==res)  
  16.         {  
  17.             name=xmlTextReaderConstName(reader);  
  18.             value=xmlTextReaderConstValue(reader);  
  19.             fprintf(file,"\tattribute=[%s],value=[%s]\n",name,value);  
  20.             res=xmlTextReaderMoveToNextAttribute(reader);  
  21.         }  
  22.         xmlTextReaderMoveToElement(reader);//重新将current instance指向current node, 否则之后读取节点value时会出现混乱   
  23.     }  
  24. }  
  25. void printNode(xmlTextReaderPtr reader)  
  26. {  
  27.     const xmlChar *name,*value;  
  28.     name=xmlTextReaderConstName(reader);  
  29.   
  30.     if(xmlTextReaderNodeType(reader)==XML_READER_TYPE_ELEMENT)  
  31.     {  
  32.         fprintf(file,"startMarkup=%s",name);  
  33.         printAttribute(reader);  
  34.     }else if(xmlTextReaderNodeType(reader)==XML_READER_TYPE_END_ELEMENT)  
  35.     {  
  36.         //fprintf(file,"endMarkup=%s",name);   
  37.     }else  
  38.     {  
  39.         fprintf(file,"flag=%s",name);  
  40.     }  
  41.     if(xmlTextReaderHasValue(reader))  
  42.     {  
  43.         value=xmlTextReaderConstValue(reader);  
  44.         fprintf(file,"\tvalue=%s",value);  
  45.     }  
  46.   
  47.     fprintf(file,"\n");  
  48. }  
  49. int main(int argv,char *argc[])  
  50. {  
  51.     LIBXML_TEST_VERSION  
  52.   
  53.     //static char buffer[]="<root>"   
  54.     //"<student>123</student>"   
  55.     //"</root>";   
  56.   
  57.     xmlTextReaderPtr reader;  
  58.     reader=xmlReaderForFile(argc[1],NULL,XML_PARSE_NOBLANKS);//忽略空节点   
  59.     //reader=xmlReaderForMemory(buffer,strlen(buffer),"something.xml",NULL,0);   
  60.   
  61.     if(reader==NULL)  
  62.     {  
  63.         fprintf(stderr,"error to read");  
  64.         xmlCleanupParser();  
  65.         return(1);  
  66.     }  
  67.     file=fopen("result.xml","w");  
  68.   
  69.     int ret=xmlTextReaderRead(reader);  
  70.     while(ret==1)  
  71.     {  
  72.         printNode(reader);  
  73.         ret=xmlTextReaderRead(reader);  
  74.     }  
  75.   
  76.     fclose(file);  
  77.     xmlFreeTextReader(reader);  
  78.     if(ret!=0)  
  79.     {  
  80.         fprintf(stderr,"error to read");  
  81.         xmlCleanupParser();  
  82.         return(1);  
  83.     }  
  84.   
  85.     xmlCleanupParser();  
  86.   
  87.   
  88.     return(0);  
  89. }  

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值