安装:
#sudo apt-get install libxml2
#sudo apt-get install libxml2-dev
sudo apt-get install libxml2-dev libxslt-dev
安装成功后:所有的头文件在/usr/include/libxml2/这个文件目录下,lib文件在/usr/lib在这个下面。
下面是在网上找的一段测试linxml2库是否装上的代码。
#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);
xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1");
node=xmlNewChild(root_node, NULL, BAD_CAST "node3",BAD_CAST"node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST"other way to create content");
xmlAddChild(node, node1);
xmlAddChild(root_node, node);
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
return(0);
}
用下面的命令编译通过。
g++ xmlCreator.cpp -o xmlCreator -I /usr/include/libxml2/ -L /usr/lib -lxml2
后来我把这段代码移到Eclipse下编译。为工程添加链接库:右键单击工程-》properities->c/c++ build->setting
1.gcc c linker->libraries->Libraries(-l) 添加”xml2".
2.gcc assembler->general->include path(-I) 添加“/usr/include/libxml2/ “
1、下载libxml2
地址ftp://xmlsoft.org/libxml2/,这里选择的是libxml2-2.8.0.tar.gz
2、解压
tar zxvf libxml2-2.8.0.tar.gz
3、配置安装
./configure
make
sudo make install
4、安装libxml-dev
sudo apt-get install libxml2-dev
5、把libxml2下的libxml拷贝一份到libxml2所在的目录 #sudo cp -r /usr/include/libxml2/libxml /usr/include/libxml
6、编写完程序后,编译时,需要引用xml2库
g++ a.cpp -lxml2