前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言xml文件存储数据文件(一)

C语言xml文件存储数据文件(一)

作者头像
全栈程序员站长
发布2022-09-14 16:03:39
1.1K0
发布2022-09-14 16:03:39
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

———————————————— 版权声明:本文为CSDN博主「jack8126」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/jack8126/article/details/117004179

本文,摘抄的,未验证过,纯属保存留用,请看原作者。

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配置文件如下:

代码语言:javascript
复制
<?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实现代码如下:

代码语言:javascript
复制
/******************************************************* * 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 ———————————————— 版权声明:本文为CSDN博主「jack8126」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/jack8126/article/details/117004179

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档