c语言读取xml配置文件

c语言读取xml配置文件

c语言要实现读取xml配置文件的功能。需要先编译libxml2库。

1、编译libxml2库

libxml2库从网络下载得到,这里下载的文件是:libxml2-sources-2.9.9.tar.gz

1.1、将libxml2文件拷贝到ubuntu系统下并解压

切换到libxml2库存在的路径下。

执行解压缩命令:

tar -zxvf libxml2-sources-2.9.9.tar.gz 

在这里插入图片描述

解压之后并切换到libxml2库路径下。

在这里插入图片描述

1.2、配置libxml2库

执行配置命令

./configure --prefix=/mnt/work/test/test/c/output

在这里插入图片描述

1.3、编译libxml2库

执行编译命令:make

在这里插入图片描述

编译过程中出现出错

libxml.c:14:20: fatal error: Python.h: No such file or directory

需要安装python,执行命令:

sudo apt-get install python-dev

在这里插入图片描述

安装完python-dev之后,再次编译成功。

执行make install执行安装

在这里插入图片描述

安装完成之后,查看output路径下,增加了相关的文件。

在这里插入图片描述

2、xml配置文件

xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<bmp_para>
  <para id="1">
     <width>1920</width>
     <height>1080</height>
     <bit>3</bit>
     <blue>0</blue>
     <green>0</green>
     <red>255</red>     
  </para>
</bmp_para>

3、c代码读取xml文件

c实现代码如下:

/*******************************************************
* file:testReadXml.c
* date:2021-05-18
* version:1.0.0.1
* author:jack8126
* description: read para from xml file
*******************************************************/
#include <stdio.h>
#include <assert.h>
 
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
 
#define DEFAULT_XML_FILE "test.xml"
 
//解析para字段,提取出width,height,bit,red,green,blue参数
static int parse_bmp(xmlDocPtr doc, xmlNodePtr cur)
{
    assert(doc || cur);
    xmlChar *key;
 
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
	    //获取width
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"width"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("width: %s\r\n", key);
	        xmlFree(key);
	    }
	    //获取height
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"height"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("height: %s\r\n", key);
	        xmlFree(key);
	    }
	    //获取bit
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"bit"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("bit: %s\r\n", key);
	        xmlFree(key);
	    }
	    //获取 blue
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"blue"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("blue: %s\r\n", key);
	        xmlFree(key);
	    }
	    //获取 green
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"green"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("green: %s\r\n", key);
	        xmlFree(key);
	    }
	    //获取 red
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"red"))) {
	        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
	        printf("red: %s\r\n", key);
	        xmlFree(key);
	    }

	    cur = cur->next;
    }
    return 0;
}
 
static int parse_para(const char *file_name)
{
	assert(file_name);

	xmlDocPtr doc;   //xml整个文档的树形结构
	xmlNodePtr cur;  //xml节点
	xmlChar *id;     //phone id

	//获取树形结构
	doc = xmlParseFile(file_name);
	if (doc == NULL) {
		fprintf(stderr, "Failed to parse xml file:%s\n", file_name);
		goto FAILED;
	}

	//获取根节点
	cur = xmlDocGetRootElement(doc);
	if (cur == NULL) {
		fprintf(stderr, "Root is empty.\n");
		goto FAILED;
	}

	if ((xmlStrcmp(cur->name, (const xmlChar *)"bmp_para"))) {
		fprintf(stderr, "The root is not bmp_para.\n");
		goto FAILED;
	}

	//遍历处理根节点的每一个子节点
	cur = cur->xmlChildrenNode;
	while (cur != NULL) {
		if ((!xmlStrcmp(cur->name, (const xmlChar *)"para"))) {
			id = xmlGetProp(cur, "id");
			printf("id:%s\r\n",id);
			parse_bmp(doc, cur);
		}
		cur = cur->next;
	}
	xmlFreeDoc(doc);
	return 0;
	FAILED:
	if (doc) {
		xmlFreeDoc(doc);
	}
	return -1;
}
 
int main(int argc, char*argv[])
{
    char cFileNameRead[64] = {0}; 

	if(argc < 2)
	{
		printf("please input like this:\r\n");
		printf("./testReadXml.bin test.xml \r\n");
		printf("test.xml --------------- input xml file \r\n");
		return -1;
	}

    sprintf(cFileNameRead,"%s",argv[1]);
	printf("cFileNameRead=%s\r\n",cFileNameRead);

	if (parse_para(cFileNameRead) != 0) {
		fprintf(stderr, "Failed to parse bmp para.\n");
		return -1;
	}

	return 0;
}



4、编译程序

执行编译命令

gcc -g testReadXml.c -o testReadXml.bin -I /mnt/work/test/test/c/output/include/libxml2/ -L /mnt/work/test/test/c/output//lib/ -lxml2

在这里插入图片描述

gcc编译时,需要制定libxml2库的位置和头文件的位置。

5、执行程序

执行命令如下:

./testReadXml.bin test.xml

执行效果如下:

在这里插入图片描述

如上图打印信息,可以通过libxml读取到test.xml文件中的关于bmp的宽高和红绿蓝参数。

参考链接:

https://blog.csdn.net/qingzhuyuxian/article/details/82596386

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值