一、下载和安装LIBXML2
【方法一】
Libxml2是个C语言的XML程式库,能简单方便的提供对XML文件的各种操作,并且支持XPATH查询,及部分的支持XSLT转换等功能。Libxml2的下载地址是
http://xmlsoft.org/
完全版的库是开源的,并且带有例子程式和说明文件。由于我是在linux下用C语言进行研发的,所以我下载的是libxml2-2.6.20.tar.gz版本的源码包。
具体安装步骤:
1、解压:$tar zxvf libxml2-2.6.20.tar.gz
2、进入解压后的安装目录:$cd libxml2-2.6.20
3、安装三部曲:1)$./configure
2)$make
3)$make install
安装完毕。
libxml2安装中出现的错误:
cannot remove `libtoolT': No such file or directory
解决方法:
修改configure文件
$ vim configure
删除这一行: $RM "$cfgfile"
重新再运行 $ ./configure
你在安装libxml2的时候,./configure --prefix=/usr/local
你在安装好后,需要重新定义:
export LD_LIBRARY_PATH=/usr/local/lib
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
具体的位置根据prefix指定的路径定。
【方法二】
#sudo apt-get install libxml2
#sudo apt-get install libxml2-dev
【方法三】
libxml2 安装使用 收藏
在项目中需要对数据的保存与交换,为了方便期间使用普通文本保存。但对文本解析时,考虑到程序的健壮性就需要处理多种意外情况,比较麻烦,因此对xml产生了点兴趣。xml本身网站上有大把的资料,可以随时参考。
Gnome项目提供了一个xml的C解析器:Libxml2。libxml2最新提供了xmlreader接口,使用很方便,不过版本在2.6以上才支持。今天倒腾了一了下,做了下简单测试,在编译自己的小程序上被拌了一下,写文纪念。
现在的最新版本是2.6.30,可以到http://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/ 下载。
安装很简单,三部走。
view plaincopy to clipboardprint?
01.// install
02.#./configure
03.#make
04.#make install
05.//test install
06.#make tests
07.//uninstall
08.#make uninstall
// install
#./configure
#make
#make install
//test install
#make tests
//uninstall
#make uninstall
如果不需要特别的定制,在configure阶段可以直接默认。 libxml2将默认把头文件与库安装在/usr/local/include/libxml2/libxml目录下。因此可能会让你在第一次编译自己的程序时遇到头文件“no such file”的错误(比如我自己就这么笨╮(╯▽╰)╭)。
libxml2提供了解决方法,它很体贴地在/usr/local/bin目录下为您提供了xml2-config、xmlcatalog、xmllint三个便利的工具(嘀咕下:其实一点都不便利,提前给人说下多好啊,还得害人去查资料)。其中xml2-config在编译时用得到。
xml2-config可以。。。自己看下面吧
view plaincopy to clipboardprint?
01.[root@Amanda ~]# xml2-config
02.Usage: xml2-config [OPTION]
03.Known values for OPTION are:
04. --prefix=DIR change libxml prefix [default /usr/local]
05. --exec-prefix=DIR change libxml exec prefix [default /usr/local]
06. --libs print library linking information
07. --cflags print pre-processor and compiler flags
08. --modules module support enabled
09. --help display this help and exit
10. --version output version information
[root@Amanda ~]# 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
这里说到编译时用到的参数: --cflags和--libs,帮助上说明这个为工程编译时提供辅助。它们提供的信息如下:
view plaincopy to clipboardprint?
01.[root@Amanda ~]# xml2-config --cflags
02.-I/usr/local/include/libxml2
03.[root@Amanda ~]# xml2-config --libs
04.-L/usr/local/lib -lxml2 -lz -lm
[root@Amanda ~]# xml2-config --cflags
-I/usr/local/include/libxml2
[root@Amanda ~]# xml2-config --libs
-L/usr/local/lib -lxml2 -lz -lm
所以在编译自己的测试程序时,可以直接使用 #gcc -I /usr/local/include/libxml2 -L/usr/local/lib -lxml2 -lz -lm -o test test.c 。
或者加入到makefile中,例如:
CFLAGS=`xml2-config --cflags`
LIBS=`xml2-config --libs`
测试程序:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL;
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
xml的创建使用libxml2
最新推荐文章于 2019-07-12 14:48:00 发布
本文详细介绍了如何利用libxml2库在C语言环境中创建和操作XML文档,包括基本的XML元素添加、属性设置等关键步骤。
摘要由CSDN通过智能技术生成