libxml2快速解析xml配置文件

一、下载

下载地址http://xmlsoft.org/downloads.html,可以git clone下来,也可以下载压缩包。

二、编译

在arm平台下运行。

cd libxml2-2.9.1
./autogen.sh
./configure  CROSS_COMPILE=arm-hisiv400-linux- --host=arm-hisiv400-linux --prefix=/opt/libxml2 --enable-shared --with-python=no
make
make install

三、使用

arm-hisiv400-linux-gcc test.c -o test -I /opt/libxml2/include/libxml2 -L /opt/libxml2/lib -lxml2


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

	static const char *TAG_RECTNODE = "RECTNODE";
	static const char *TAG_INFO = "INFO";
	static const char *TAG_ATTR_WIDTH = "Width";
	static const char *TAG_ATTR_HEIGHT = "Height";
	int main(){
	    xmlDocPtr doc;
	    xmlNodePtr cur;
	
	    doc = xmlParseFile("./test.xml"); //打开解析的xml文件
	    if (doc == NULL) {
	        printf( "open file fail!\r\n");
	        return false;
	    }
	
	    cur = xmlDocGetRootElement(doc); //拿到文件根节点
	    if (cur == NULL) {
	        printf( "empty document!\r\n");
	        xmlFreeDoc(doc);
	        return false;
	    }
	
	    cur = cur->xmlChildrenNode;
	    while (cur != NULL) {
	        if (!xmlStrcmp(cur->name, (const xmlChar *)TAG_INFO)) { //遍历根节点底下的子节点
	            char *strWallWidth = (char *)xmlGetProp(cur,(const xmlChar *)TAG_ATTR_WIDTH);
	        }
	        cur = cur->next;
	   }
	   	xmlFreeDoc(doc);
		xmlCleanupParser();
		return 0;
	}

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<RECTNODE>  #根节点
	<INFO    #子节点
		Width="7680"
		Height="2880"
	/>
</RECTNODE>

这样我们可以快速的搭建配置脚本环境,将需要的配置项写入子节点或者新建更多的节点来构建简易配置文件。该例子快速入门并且迅速上手,希望有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值