Ubuntu下libxml2的安装和使用

Ubuntu下libxml2的安装和使用这篇文章主要介绍 libxml2 的安装和使用 xml 文件的主要作用就是配置文件 实际的应用在前面的章节 Audio 设备文件解析中有需要对 audio policy configuratio xml 文件解析 google 使用的是开源库 libxml2 在源码目录 external libxml2 下面 现在就单独对这个库进行分析 在终端中执行 wang wang test sudoapt

这篇文章主要介绍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 
  
    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 
  
    #include 
   
     #include 
    
      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打开生成的文件

Ubuntu下libxml2的安装和使用


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

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

Ubuntu下libxml2的安装和使用


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230053.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午3:14
下一篇 2026年3月16日 下午3:14


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号