1. 程式人生 > >Ubuntu下libxml2的安裝和使用

Ubuntu下libxml2的安裝和使用

這篇文章主要介紹libxml2的安裝和使用,xml檔案的主要作用就是配置檔案,實際的應用在前面的章節Audio裝置檔案解析中有需要對audio_policy_configuration.xml檔案解析,google使用的是開源庫libxml2,在原始碼目錄/external/libxml2下面,現在就單獨對這個庫進行分析。

在終端中執行

[email protected]:~/test$ sudo apt-get install libxml2-dev
[email protected]:~/test$ sudo apt-get install libxml2
可以通過
[email protected]
:~/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/
通過執行
[email protected]:~/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;
}
編譯
[email protected]:~/test$ gcc -I/usr/include/libxml2 CreateXmlFile.c -o CreateXmlFile -lxml2
可以看到執行結果
[email protected]:~/test$ gcc -I/usr/include/libxml2 CreateXmlFile.c -o CreateXmlFile -lxml2
[email protected]:~/test$ ./CreateXmlFile
[email protected]:~/test& ls
CreatedXmlDemo.xml  CreateXmlFile
使用html開啟生成的檔案


可以看到生成的檔案和程式碼描述的相同,程式執行正確。如果需要使用cmake構建工程,請檢視 CMake加入第三方庫介紹。

如果覺得這篇文章有用,可以掃免費紅包支援。


相關推薦

no