Ubuntu下libxml2的安装和使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WAN_EXE/article/details/79173248

这篇文章主要介绍libxml2的安装和使用,xml文件的主要作用就是配置文件,实际的应用在前面的章节Audio设备文件解析中有需要对audio_policy_configuration.xml文件解析,google使用的是开源库libxml2,在源码目录/external/libxml2下面,现在就单独对这个库进行分析。

在终端中执行

wang@wang:~/test$ sudo apt-get install libxml2-dev
wang@wang:~/test$ sudo apt-get install libxml2
可以通过

wang@wang:~/test$ dpkg -s libxml2-dev
查看安装状况。

Package: libxml2-dev
Status: install ok installed
Priority: optional
Section: libdevel
Installed-Size: 2862
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Multi-Arch: same
Source: libxml2
Version: 2.9.1+dfsg1-3ubuntu4.12
Depends: libxml2 (= 2.9.1+dfsg1-3ubuntu4.12)
Suggests: pkg-config
Description: Development files for the GNOME XML library
 XML is a metalanguage to let you design your own markup language.
 A regular markup language defines a way to describe information in
 a certain class of documents (eg HTML). XML lets you define your
 own customized markup languages for many classes of document. It
 can do this because it's written in SGML, the international standard
 metalanguage for markup languages.
 .
 Install this package if you wish to develop your own programs using
 the GNOME XML library.
Homepage: http://xmlsoft.org/
通过执行

wang@wang:~/test$ dpkg -L libxml2-dev
查看安装位置

/usr/include/libxml2
/usr/include/libxml2/libxml
/usr/include/libxml2/libxml/xpointer.h
/usr/include/libxml2/libxml/catalog.h
/usr/include/libxml2/libxml/xmlreader.h
/usr/include/libxml2/libxml/xmlexports.h
已经安装成功。

 接下来写个demo使用libxml2进行文件解析,编写CreateXmlFile.c

#include<stdio.h>
#include<libxml/parser.h>
#include<libxml/tree.h>

int main(int argc, char **argv)
{
 	//Define document pointer
  	xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");

	//Define node pointer
	xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root");

	//Set the root element of the document
	xmlDocSetRootElement(doc,root_node);

	//Create child nodes directly in the root node
	xmlNewTextChild(root_node,NULL,BAD_CAST"newnode1",BAD_CAST"newnode1 content");
	xmlNewTextChild(root_node,NULL,BAD_CAST"newnode2",BAD_CAST"newnode2 content");

	//Create a new node
	xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");
	//Create a new text node
	xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");
	//Add a new node to parent
	xmlAddChild(root_node,node);
	xmlAddChild(node,content);
	//Create a new property carried by a node
	xmlNewProp(node,BAD_CAST"attribute",BAD_CAST"yes");

	//Create a son and grandson node element
	node = xmlNewNode(NULL,BAD_CAST"son");
	xmlAddChild(root_node,node);
	xmlNodePtr grandson = xmlNewNode(NULL,BAD_CAST"grandson");
	xmlAddChild(node,grandson);
	xmlAddChild(grandson,xmlNewText(BAD_CAST"THis is a grandson node"));
						    
	//Dump an XML document to a file
	int nRel = xmlSaveFile("CreatedXmlDemo.xml",doc);
	if(nRel != -1) {
		//Free up all the structures used by a document,tree included
		xmlFreeDoc(doc);
	}
	return 0;
}
编译

wang@wang:~/test$ gcc -I/usr/include/libxml2 CreateXmlFile.c -o CreateXmlFile -lxml2
可以看到执行结果

wang@wang:~/test$ gcc -I/usr/include/libxml2 CreateXmlFile.c -o CreateXmlFile -lxml2
wang@wang:~/test$ ./CreateXmlFile
wang@wang:~/test& ls
CreatedXmlDemo.xml  CreateXmlFile
使用html打开生成的文件


可以看到生成的文件和代码描述的相同,程序运行正确。如果需要使用cmake构建工程,请查看 CMake加入第三方库介绍。

如果觉得这篇文章有用,可以扫免费红包支持。


猜你喜欢

转载自blog.csdn.net/WAN_EXE/article/details/79173248