加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用libxml读取分析配置文件

发布时间:2020-12-16 08:51:18 所属栏目:百科 来源:网络整理
导读:配置文件示例如下: ?xml version="1.0" encoding="UTF-8"?config Day7/Day partitions partition ip192.168.2.213/ip port5730/port /partition partition ip192.168.2.230/ip port9003/port /partition /partitions/config 首先定义存储信息的结构体: typ

配置文件示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <Day>7</Day>
  <partitions>
    <partition>
      <ip>192.168.2.213</ip>
      <port>5730</port>
    </partition>
    <partition>
      <ip>192.168.2.230</ip>
      <port>9003</port>
    </partition>
  </partitions>
</config>

首先定义存储信息的结构体:
typedef struct _partition {
	char ip[STRING_SIZE_MAX];
	int port;
} partition_t;

typedef struct _config {
	int day;
	int partitions_count;
	partition_t p[MAX];
} config_t;
然后就可以用linxml中的函数封装接口了,主要引用的头文件有:
#include "xmlwriter.h"
#include "xmlreader.h"
#include "parser.h"

接口封装:
int load_config(config_t *c,const char *file) {
	int ret = 0;
	xmlDocPtr doc;
	xmlNodePtr cur;
	xmlChar *key = NULL;
	doc = xmlParseFile(file);
	if(doc == NULL) {
		return -1;
	}
	cur = xmlDocGetRootElement(doc);//求根节点
	if(cur == NULL) {
		gerr("root element get fail.");
		xmlFreeDoc(doc);
		return -1;
	}
	//判断根节点是否为config
	if(xmlStrcmp(cur->name,(const xmlChar *)("config")) != 0) {
		gerr("root element error:%s",(const char *)(cur->name));
		xmlFreeDoc(doc);
		return -1;
	}
	//进到孩子节点
	cur = cur->xmlChildrenNode;
	while(cur != NULL) {
		if(xmlStrcmp(cur->name,(const xmlChar *)("Day")) == 0) {
			key = xml_node_strval(doc,cur);
			if(key != NULL) {
				c->day = atoi((char *)key);
				xmlFree(key);
				gdebug("%s=%d",(const char *)cur->name,c->day);
			}
		} else if(xmlStrcmp(cur->name,(const xmlChar *)("partitions")) == 0) {
			ret = load_partitions(c,doc,cur);
			if(ret != 0) {
				gdebug("parse device list fail.");
				return -1;
			}
		} 
		//遍历兄弟节点
		cur = cur->next;
	}
	xmlFreeDoc(doc);
	return 0;
}
static int load_partition(partition_t *pt,xmlDocPtr doc,xmlNodePtr cur) {
	int ret=0;
	xmlChar *key;
	cur = cur->xmlChildrenNode;
	while(cur != NULL) {
		if(xmlStrcmp(cur->name,(const xmlChar *)("ip")) == 0) {
			key = xml_node_strval(doc,cur);
			if(key != NULL) {
				strncpy(pt->ip,(const char *)key,sizeof(pt->ip));
				xmlFree(key);
				gdebug("%s=%s",pt->ip);
			}
		} else if(xmlStrcmp(cur->name,(const xmlChar *)("port")) == 0) {
			key = xml_node_strval(doc,cur);
			if(key != NULL) {
				pt->port = atoi((char *)key);
				xmlFree(key);
				gdebug("%s=%d",pt->port);
			}
		} 
		cur = cur->next;
	}
	return 0;
}

static int load_partitions(config_t *c,xmlNodePtr cur) {
	int ret = 0;
	int i = 0;
	cur = cur->xmlChildrenNode;
	while(cur != NULL) {
		if(xmlStrcmp(cur->name,(const xmlChar *)("partition")) == 0) {
			ret = load_partition(&c->p[i++],cur);
			if(ret != 0) {
				gerr("partition load failure.");
				return -1;
			}
			gdebug("partition load done.");
		}
		cur = cur->next;
	}
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读