C语言 使用libxml解析 生成和解析xml

前端之家收集整理的这篇文章主要介绍了C语言 使用libxml解析 生成和解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

##安装 libxml

libxml是一个用来解析XML文档的函数库。它用C语言写成,并且能为多种语言所调用

###下载libxml 貌似libxml官方已经被河蟹掉了。没关系这里有 libxml的下载地址 2.7.4

###下载之后解压 tar -zxvf libxml2-2.7.4.tar.gz

安装

  • cd libxml2-2.7.4
  • ./configure && make && make install

##写测试代码

###生成xml的代码

int main(int argc,char **argv)

{

    xmlDocPtr doc = NULL;

    xmlNodePtr root_node = NULL,node = NULL,node1 = NULL;

    doc = xmlNewDoc(BAD_CAST "1.0");

    root_node = xmlNewNode(NULL,BAD_CAST "root");

    xmlDocSetRootElement(doc,root_node);

    xmlNewChild(root_node,NULL,BAD_CAST "node1",BAD_CAST "content of node1");

    node=xmlNewChild(root_node,BAD_CAST "node3",BAD_CAST"node has attributes");

    xmlNewProp(node,BAD_CAST "attribute",BAD_CAST "yes");

    node = xmlNewNode(NULL,BAD_CAST "node4");

    node1 = xmlNewText(BAD_CAST"other way to create content");

    xmlAddChild(node,node1);

    xmlAddChild(root_node,node);

    xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-",doc,"UTF-8",1);

    xmlFreeDoc(doc);

    xmlCleanupParser();

    xmlMemoryDump();

    return(0);
}

编译 :

gcc -I /usr/local/include/libxml2  -L /usr/local/lib -lxml2 test.c -o test

执行

./test

解析xml

新建待解析xml,文件内容:

<?xml version="1.0"?>
<story>
        <storyinfo>
                <author>John Fleck</author>
                <datewritten>June 2,2002</datewritten>
                <keyword>example keyword</keyword>
        </storyinfo>
        <body>
                <headline>This is the headline</headline>
                <para>This is the body text.</para>
        </body>
</story>

###解析的c语言代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

void
parseStory (xmlDocPtr doc,xmlNodePtr cur) {

        xmlChar *key;
        cur = cur->xmlChildrenNode;
        while (cur != NULL) {
                if ((!xmlStrcmp(cur->name,(const xmlChar *)"keyword"))) {
                        key = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
                        printf("keyword: %s\n",key);
                        xmlFree(key);
                }
                cur = cur->next;
        }
        return;
}

static void parseDoc(char *docname) {

        xmlDocPtr doc;
        xmlNodePtr cur;

        doc = xmlParseFile(docname);

        if (doc == NULL ) {
                fprintf(stderr,"Document not parsed successfully. \n");
                return;
        }

        cur = xmlDocGetRootElement(doc);
        cur = xmlDocGetRootElement(doc);

        if (cur == NULL) {
                fprintf(stderr,"empty document\n");
                xmlFreeDoc(doc);
                return;
        }

        if (xmlStrcmp(cur->name,(const xmlChar *) "story")) {
                fprintf(stderr,"document of the wrong type,root node != story");
                xmlFreeDoc(doc);
                return;
        }

        cur = cur->xmlChildrenNode;
        while (cur != NULL) {
                if ((!xmlStrcmp(cur->name,(const xmlChar *)"storyinfo"))){
                        parseStory (doc,cur);
                }

                cur = cur->next;
        }

        xmlFreeDoc(doc);
        return;
}

int main(int argc,char **argv) {

        char *docname;

        if (argc <= 1) {
                printf("Usage: %s docname\n",argv[0]);
                return(0);
        }

        docname = argv[1];
        parseDoc (docname);

        return (1);
}

###编译 gcc keyword.c -o keyword -I/usr/local/include/libxml2 -lxml2

###运行 ./keyword story.xml

参考博文

http://www.jb51.cc/article/p-rdtnxufp-bh.html http://www.cnblogs.com/shanzhizi/archive/2012/07/09/2583739.html

原文链接:https://www.f2er.com/xml/297017.html

猜你在找的XML相关文章