C语言xml文件存储数据文件(一) 您所在的位置:网站首页 c语言读取xml文件 C语言xml文件存储数据文件(一)

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

2024-07-09 21:38| 来源: 网络整理| 查看: 265

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

———————————————— 版权声明:本文为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复制 1920 1080 3 0 0 255

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 #include #include #include #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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有