Libxml2库的编译和使用

来源:互联网 发布:重装系统提示网络电缆 编辑:程序博客网 时间:2024/06/14 21:47

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-configUsage: 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

0 0
原创粉丝点击