Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121994
  • 博文数量: 20
  • 博客积分: 1627
  • 博客等级: 上尉
  • 技术积分: 383
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 16:11
文章分类

全部博文(20)

文章存档

2012年(5)

2011年(2)

2010年(12)

2009年(1)

我的朋友

分类: C/C++

2009-01-19 22:05:15

linux c 使用libxml2读取配置文件
 
   由于在c下读取文本的配置文件比较麻烦所以想找一个方便读取配置文件的方法。但是在网上左找右找,大家的方法都一样。还是读文件啊读文件。后来找到了libxml,发现用法比较简单。研究了一把。确实还不错,人家不但带.so还带.a方便打包。呵呵。废话不说了。直接上程序。
 
 
首先创建一个xml吧。就叫它my.xml
 

<?xml version="1.0"?>
<root>
        <mysql>
               <host>127.0.0.1</host>
                <port>3306</port>
                <db>test</db>
                <username>root</username>
                <password>123456</password>
        </mysql>
</root>

 

然后贴出分析这个xml的程序readxml.c

 

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


int parseStory(xmlDocPtr doc, xmlNodePtr cur)
{
        xmlChar *key;
        cur = cur->xmlChildrenNode;
        while(cur != NULL)
        {
                if((!xmlStrcmp(cur->name, (const xmlChar*)"mysql")))
                {
                        printf("[%s]\n", cur->name);
                        parseStory(doc, cur);
                }
                else if((!xmlStrcmp(cur->name, (const xmlChar*)"host")))
                {
                        key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        printf(" %s=>%s\n", cur->name, key);
                        xmlFree(key);
                }
                else if((!xmlStrcmp(cur->name, (const xmlChar*)"port")))
                {
                        key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        printf(" %s=>%s\n", cur->name, key);
                        xmlFree(key);
                }
                else if((!xmlStrcmp(cur->name, (const xmlChar*)"db")))
                {
                        key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        printf(" %s=>%s\n", cur->name, key);
                        xmlFree(key);
                }
                else if((!xmlStrcmp(cur->name, (const xmlChar*)"username")))
                {
                        key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        printf(" %s=>%s\n", cur->name, key);
                        xmlFree(key);
                }
                else if((!xmlStrcmp(cur->name, (const xmlChar*)"password")))
                {
                        key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        printf(" %s=>%s\n", cur->name, key);
                        xmlFree(key);
                }
                cur = cur->next;
        }
        return 0;

}

int main(int argc, char **argv)
{
        //定义2个指针,doc指向整个dom;cur指向结点,以后遍历树就靠这个指针鸟
        xmlDocPtr doc;
        xmlNodePtr cur;

        //获取doc指针,也是把其他格式转成utf8的功能吧
        doc=xmlParseFile(argv[1]);
        if(doc==NULL)
        {
                printf("Document not parsed successfully. \n");
                exit(1);
        }
        printf("xmlParseFile ok.\n");

        //取得结点指针
        cur=xmlDocGetRootElement(doc);
        if(cur==NULL)
        {
                printf("empty document. \n");
                xmlFreeDoc(doc);
                exit(1);
        }
        printf("xmlDocGetRootElement ok.\n");

        //取得根结点指针,我这里是root,记住,这里一定要是根结点
        if (xmlStrcmp(cur->name, (const xmlChar *)"root"))
        {
                printf("document of the wrong type, root node != root\n");
                xmlFreeDoc(doc);
                exit(1);
        }
        printf("ok.\n");

        //通过这个递归函数,遍历出所有感兴趣的结点。
        parseStory(doc, cur);

        //一定要释放doc哦。
        xmlFreeDoc(doc);
        exit(0);
}

 

奶奶滴,怎么中文到这里就变了啊。早知道就不加中文了。害得俺又加一次注释。

 

最后Makefile文件

CC=gcc -Wall -g
LIBS=`xml2-config --libs`
CFLAGS=`xml2-config --cflags`

readxml: readxml.o
        $(CC) -o readxml readxml.o $(LIBS)

readxml.o: readxml.c
        $(CC) -c readxml.c $(CFLAGS)

clean:
        rm -f *.o readxml

 

最后编译运行结果如下:

 

huying@ubuntu:~/c/xml$ make
gcc -Wall -g -c readxml.c `xml2-config --cflags`
gcc -Wall -g -o readxml readxml.o `xml2-config --libs`
huying@ubuntu:~/c/xml$ ./readxml my.xml
xmlParseFile ok.
xmlDocGetRootElement ok.
ok.
[mysql]
  host=>127.0.0.1
  port=>3306
  db=>test
  username=>root
  password=>123456

 

祝大家好运。有问题大家互相交流吧。我这里只用到了简单的读取配置文件功能,这个libxml还能生成修改xml,有需要的时候再研究吧。希望对大家有用。

阅读(3218) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2009-07-21 13:59:39

顶~~~~~~~~~~!!!

chinaunix网友2009-05-06 15:55:25

非常有用。 希望把修改的code都能发上来。