Libxml2库的编译和使用

来源:互联网 发布:tomcat war 部署端口 编辑:程序博客网 时间:2024/06/07 05:36

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子目录下;

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:

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

3.   编译xml解析程序

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

动态库编译方式:

cc -o xmltest -I/usr/local/include/libxml2 -L/usr/local/lib -lxml2 xmltest.c

 

静态库的编译方式:

cc -o xmltest -lm -I/usr/local/include/libxml2 xmltest.c /usr/local/lib/libxml2.a

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

原创粉丝点击