Libxml2库的编译和使用

Libxml2库提供了C语言解析和构造xml文档的接口,为后台C语言处理程序和前台应用程序提供了一种通用的通迅方式。

本文以libxml2-2.6.30版本来说明Libxml2库的使用方法。

1.    编译库文件

libxml2-2.6.30.tar.gz文件解压后,进入libxml2-2.6.30文件夹,顺序执行以下命令:

chmod +x ./configure

./configure

make

make install

chmod +x ./configure”命令增加configure脚本的可执行权限;

./configure”脚本根据当前编译系统的实际情况生成相应的makefile文件;

make”命令执行上一命令中生成的makefile文件生成相应的目标文件;

make install”命令主要把目标文件拷贝到/usr/local目录下,

/usr/local/lib目录下为以下库文件:

libxml2.a  libxml2.la  libxml2.so  libxml2.so.2  libxml2.so.2.6.30 pkgconfig  xml2Conf.sh

/usr/local/include/libxml2目录是Libxml库使用时需要的头文件,包含在libxml子目录下;


xml2-config
Usage: xml2-config [OPTION]


Known values for OPTION are:


  --prefix=DIR          change libxml prefix [default /usr/local]
  --exec-prefix=DIR     change libxml exec prefix [default /usr/local]
  --libs                print library linking information
  --cflags              print pre-processor and compiler flags
  --modules             module support enabled
  --help                display this help and exit
  --version             output version information


2.    使用Libxml2

Libxml2库的api参考可以从http://www.xmlsoft.org/html/index.html查询。下面以解析一个简单的xml文件为例,给出一个完整的例子。

Xml文档:

<ioMsg>

    <type>she</type>

    <subtype>

       <st1>123</st1>

       <st2>563</st2>

    </subtype>

</ioMsg>


C解析代码xmltest.c

  1. #include <libxml/parser.h>
  2. #include <libxml/tree.h>

  3. int main(int argc, char* argv[])
  4. {
  5.     xmlDocPtr doc; //定义解析文档指针
  6.     xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动)
  7.     xmlChar *szKey; //临时字符串变量
  8.     char *szDocName;
  9.     
  10.     if (argc <= 1) 
  11.     {
  12.        printf("Usage: %s docname/n", argv[0]);
  13.        return(0);
  14.     }
  15.     szDocName = argv[1];
  16.     doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件
  17.     if (NULL == doc)
  18.     { 
  19.        printf("Document not parsed successfully/n"); 
  20.        return -1;
  21.     }
  22.     curNode = xmlDocGetRootElement(doc); //确定文档根元素
  23.     if (NULL == curNode)
  24.     {
  25.        printf("empty document/n");
  26.        xmlFreeDoc(doc);
  27.        return -1;
  28.     }
  29.     if (xmlStrcmp(curNode->name, BAD_CAST "ioMsg"))
  30.     {
  31.        printf("document of the wrong type, root node != ioMsg/n");
  32.        xmlFreeDoc(doc);
  33.        return -1;
  34.     }
  35.     curNode = curNode->children;
  36.     while(curNode != NULL)
  37.     {
  38.        //取出节点中的内容
  39.        szKey = xmlNodeGetContent(curNode);
  40.        printf("Content value =%s/n", szKey);
  41.        curNode = curNode->next;
  42.      }
  43.      xmlFreeDoc(doc);
  44.     return 0; 
  45. }

3.    编译xml解析程序

假设Libxml2库是按步骤1的编译方式,其库文件和头文件分别位于/usr/local/lib/usr/local/include/libxml2目录下。

动态库编译方式:

gcc -o xmltest -I/usr/local/include/libxml2 -L/usr/local/libxmltest.c -lxml2  -lz -lm

-I/usr/local/include/libxml2”指定Libxml2库的头文件所在的路径,“-L/usr/local/lib”指定动态库所在路径。


4、libxml2主要函数 说明:

参见:   

http://www.cnblogs.com/sharpfeng/archive/2012/10/09/2717076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiny丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值