zoukankan      html  css  js  c++  java
  • 利用libxml2解析xml文档

    C程序里可以利用libxml2库去解析xml文档。利用libxml2可以很轻松的解析,生成xml文件。

    比较好的教程如下:http://www.blogjava.net/wxb_nudt/archive/2007/11/18/161340.html

    这里演示一个小例子,包含了遍历节点,获取节点属性与值,以及获取CDATA里面的内容。

    测验的xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <root name="test">
            <content>
                    <pro id="moonApple"><![CDATA[<say>i still have lots to work on</say>]]></pro>
                    <details>
                            <detail name="dancing">like it</detail>
                            <detail name="singing">poor , just listen</detail>
                            <detail name="laugh"/>
                            <detail name="eating"><![CDATA[<food>candy</food>]]></detail>
                    </details>
            </content>
    </root>
    

    test.c文件:

    #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\n");
                    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\n");
                    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:\n%s\n",(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\n",(char*)name,(char*)value);                                       
                                             }else{
                                                    printf("%s has no value\n",(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:\n%s\n",content);
            if(parse_xml_file(content,filesize)<0){
                    perror("parse xml failed");
            }
            return 0;
    }
    

    输出结果:

    this is moonApple:
    <say>i still have lots to work on</say>
    dancing : like it
    singing : poor , just listen
    laugh has no value
    eating : <food>candy</food>
    

    这里主要关注XML文件里面的CDATA里面的内容~

    更具体的使用方法和其他细节可以访问官网和上面的连接~

  • 相关阅读:
    bzoj2733 永无乡 平衡树按秩合并
    bzoj2752 高速公路 线段树
    bzoj1052 覆盖问题 二分答案 dfs
    bzoj1584 打扫卫生 dp
    bzoj1854 游戏 二分图
    bzoj3316 JC loves Mkk 二分答案 单调队列
    bzoj3643 Phi的反函数 数学 搜索
    有一种恐怖,叫大爆搜
    BZOJ3566 概率充电器 概率dp
    一些奇奇怪怪的过题思路
  • 原文地址:https://www.cnblogs.com/aLittleBitCool/p/2117548.html
Copyright © 2011-2022 走看看