1.打开配置文件 ,获取根节点指针。
2.读取配置文件相关内容。
3.修改/写入配置文件的相关内容。4.关闭配置文件。
下面进行详细的介绍:
前提:
a .所涉及的头文件:
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
b.宏定义
#define C_CONFIG_DEVICE_MAX_NAME_LEN 20
#define C_CONFIG_DEVICE_MAX_ADDRESS_LEN 6
#define file_path "/home/profile/path/ma_config.xml"
#define NULL 0
c. 所涉及的枚举类型以及 结构体
typedef struct{
unsignec char status;
uint time;
uint function;
char name[C_CONFIG_DEVICE_MAX_NAME_LEN];
char address[C_CONFIG_DEVICE_MAX_ADDRESS_LEN];
}ma_config_info_t;
typedef enum{
C_CONFIG_EVENT_STATUS,
C_CONFIG_EVENT_TIME,
C_CONFIG_EVENT_FUNCTION,
C_CONFIG_EVENT_NAME,
C_CONFIG_EVENT_ADDRESS,
C_CONFIG_EVENT_END
}ma_config_event_e;
typedef enum{
C_CONFIG_SUCCESS,
C_CONFIG_FALSE,
C_CONFIG_ENDS
}ma_event_result_e;
c.所涉及的全局变量
xmlDocPtr doc =NULL;
xmlNodePtr node = NULL;
ma_config_info_t ma_config_info;
d.函数的声明
int ma_config_file_open(char* file_path);
int ma_config_file_read_file(void);
int ma_config_read(int event_id ,char* data,int data_length);
int ma_config_file_write(int event_id ,char * data,int data_length);
int ma_config_close(void);
static int ma_read_phone_cfg(XmlNodePtr node);
static int ma_read_video_cfg(XmlNodePtr node);
static int ma_phone_set_cfg(int event_id ,char* data,int data_length);
static int ma_video_set_cfg(int event_id , char * data,int data_length);
xmlNodePtr ma_get_video_node(void);
xmlNodePtr ma_get_phone_node(void);
static int ma_string_to_char(char*,char*,uint );
static int ma_char_to_string(char*,char*,uint );
e 所需的配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<phone>
<status>0</status>
<time>0010</time>
<function>0100</fuction>
<name>huawei</name>
</phone>
<video>
<address>010203040506</address>
</video>
</config>