使用libxml读取分析配置文件
来源:互联网 发布:人工智能屠宰场小女孩 编辑:程序博客网 时间:2024/05/16 15:49
配置文件示例如下:
<?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;然后就可以用libxml中的函数封装接口了,主要引用的头文件有:
#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;}//判断根节点是否为configif(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", (const char *)cur->name, 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", (const char *)cur->name, pt->port);}} cur = cur->next;}return 0;}static int load_partitions(config_t *c, xmlDocPtr doc, 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++], doc, cur);if(ret != 0) {gerr("partition load failure.");return -1;}gdebug("partition load done.");}cur = cur->next;}return 0;}
0 0
- 使用libxml读取分析配置文件
- libxml的使用(1)--读取xml
- libxml的使用(1)--读取xml
- libxml的使用(1)--读取xml
- libxml的使用(1)--读取xml
- libxml的使用(1)--读取xml
- libxml的使用(1)--读取xml
- libxml的使用(2)--读取节点属性
- libxml的使用(2)--读取节点属性
- libxml的使用(2)--读取节点属性
- libxml的使用(2)--读取节点属性
- 使用rapidxml读取配置文件
- 使用ResourceBundle读取配置文件
- 配置文件读取的使用
- 使用ResourceBundle读取配置文件
- 使用PropertyPlaceholderConfigurer读取配置文件
- 使用properties读取配置文件
- 使用配置文件读取连接字符串
- SpringMVC
- AVPlayer,AVAudioPlayer,语音合成
- 难懂的this
- JNI本地文件中打印输出到LOGCAT
- Spring @Transactional 如何开启事务
- 使用libxml读取分析配置文件
- torch.gt函数的用法
- 第十五周第一天
- 【bzoj4151】 [AMPPZ2014]The Cave
- nyoj1242 Distribution
- swift 快速奔跑的兔几 本节的内容是:图形用户界面
- 【LEETCODE】107-Binary Tree Level Order Traversal II
- 复合索引和效率
- 简单安卓QQ登录界面