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:
- #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目录下。
动态库编译方式:
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
- Libxml2库的编译和使用
- Libxml2库的编译和使用
- Libxml2库的编译和使用
- Libxml2库的编译和使用
- Libxml2库的编译和使用
- libxml2 编译和使用
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- libxml2的安装和使用
- ubuntu12.04 安装libxml2 使用libxml2编译
- 交叉编译libxml2的arm-linux库
- libxml2 libxslt库编译
- libxml2库的安装,xpath的使用
- 使用libxml2库设置encoding的问题
- 欢迎使用CSDN-markdown编辑器
- [LeetCode]Longest Substring Without Repeating Characters
- 七牛上传的响应处理大全
- leetcode Interleaving String
- 判断*.xls文件单元格数据类型方法封装
- Libxml2库的编译和使用
- sublime text2编写html时如何快速地在指定浏览器中预览
- 配置minicom
- 有时候人活着就是累
- Android多媒体开发【10】-- android中OpenMax的实现【1】整体框架
- 几个有意思的运维编程题
- sicily 1240. Faulty Odometer
- Navicat
- jquery 获取相应元素