libxml2解析xml_libxml2解析模式

libxml2解析xml_libxml2解析模式利用libxml2解析xml文档#include<stdio.h>#include<string.h>#include<libxml/parser.h>#include<li

利用libxml2解析xml文档
  #include<stdio.h>
#include<string.h>
#include<libxml/parser.h>
#include<libxml/tree.h>
int parse_xml_file(char *buf,int len){
xmlDocPtr doc;
xmlNodePtr root,node,detail;
xmlChar *name,*value;
doc=xmlParseMemory(buf,len); //parse xml in memory
if(doc==NULL){
printf(“doc == null

  ”);
return -1;
}
root=xmlDocGetRootElement(doc);
for(node=root->children;node;node=node->next){
if(xmlStrcasecmp(node->name,BAD_CAST”content”)==0)
break;
}
if(node==NULL){
printf(“no node = content

  ”);
return -1;
}
for(node=node->children;node;node=node->next){
if(xmlStrcasecmp(node->name,BAD_CAST”pro”)==0){ //get pro node
name=xmlGetProp(node,BAD_CAST”id”);
value=xmlNodeGetContent(node);
printf(“this is %s:

  %s

  ”,(char*)name,(char*)value); //get value, CDATA is not parse and don’t take into value
xmlFree(name);
xmlFree(value);
}else if(xmlStrcasecmp(node->name,BAD_CAST”details”)==0){ //get details node
for(detail=node->children;detail;detail=detail->next){ //traverse detail node
if(xmlStrcasecmp(detail->name,BAD_CAST”detail”)==0){
name=xmlGetProp(detail,BAD_CAST”name”);
value=xmlNodeGetContent(detail);
if(strlen((char*)value)!=0){
printf(“%s : %s

  ”,(char*)name,(char*)value);
}else{
printf(“%s has no value

  ”,(char*)name);
}
xmlFree(name);
xmlFree(value);
}
}
}
}
xmlFreeDoc(doc);
return 0;
}
int main(void){
char *content;
unsigned long filesize;
FILE *file;
if((file=fopen(“testxml”,”r”))==NULL){
perror(“openf file error”);
}
fseek(file,0,SEEK_END);
filesize=ftell(file);
rewind(file);
content=(char *)malloc(filesize+1);
memset(content,0,filesize+1);
fread(content,1,filesize,file);
fclose(file);
printf(“content:

  %s

  ”,content);
if(parse_xml_file(content,filesize)<0){
perror(“parse xml failed”);
}
return 0;
}

2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/96733.html

(0)
上一篇 2024年 5月 22日 15:16
下一篇 2024年 5月 22日

相关推荐

关注微信