tinyxml c语言封装
嵌入式开发过程中配置文件可选: 二进制,.conf,.xml,.ini,数据库 等。二进制 文件压缩性好,占用空间相比其他的较小,但是不宜与扩展;.conf 是标准的linux配置使用的文件,一般使用开源的libconfig进行操作,结构化良好,支持c/c++,可读性扩展性良好,但是js解析时比较麻烦,目前没有找到好的方法,都是读取文件后转换成xml;.ini 上位机中可能使用稍多,扩展性较差,一般在linux 我会使用conf 非 ini配置文件;.xml 兼容性好,性能可能稍微差(与使用的开源库可能有关系)一般都是c++进行封装的,但c/c++/vs/js 都可对其进行解析,对于多语言开发的小型数据存储使用 xml;数据库 再大型数据存储中使用较多,性能较好;(个人见解,如有问题多多指教);
tinyxml2 库短小精悍,比较好用,刚刚使用,但是因为其为 c++库 所以对其进行二次封装;
1 #ifndef TINYXMLC_H_ 2 #define TINYXMLC_H_ 3 4 #ifdef _cplusplus 5 #include "tinyxml2.h" 6 7 #include<iostream> 8 9 using namespace tinyxml2; 10 using namespace std; 11 12 extern "C" 13 { 14 #endif //_cplusplus 15 16 /* 17 * 函数名: OpenDocument 18 * 参数 : xml 文件名 19 * 描述 : 构建xml文档对象,读取xml文件,返回指针 20 * 返回值: 成功 XMLDocument 指针; 失败 返回NULL 21 */ 22 void* OpenDocument(const char* xmlFileName); 23 24 /* 25 * 函数名 : CloseDocument 26 * 参数 : xml 对象 27 * 描述 : 释放xml 对象内存空间 28 * 返回值 : 无 29 */ 30 void CloseDocument(void* xmlDocument); 31 32 /* 33 * 函数名 : GetRootElement 34 * 参数 : xml 对象 35 * 描述 : 获取根元素对象 36 * 返回值 : 成功 返回跟元素对象指针 , 失败返回 NULL 37 */ 38 void* GetRootElement(void* xmlDocument); 39 40 /* 41 * 函数名: GetRootElementValue 42 * 参数 : xml 文档对象 43 * 描述 : 获取根元素 的值 44 * 返回 : 成功 返回根元素的值 ,失败 返回 NULL 45 */ 46 const char* GetElementValue(void* xmlDocument); 47 48 /* 49 * 函数名: GetNodeByName 50 * 参数 : 根元素 节点名 51 * 描述 : 根据节点名 查找第一个名为nodeName的节点 52 * 返回值: 成功返回节点指针 失败返回NULL 53 */ 54 void* GetNodeByName(void* xmlElementRoot, const char* nodeName); 55 56 57 58 /* 59 * 函数名 : SaveDocument 60 * 参数 : document对象 保存文件名 61 * 描述 : 将document 保存到 xmlFileName 中 62 * 返回值 : 无 63 */ 64 65 void SaveDocument(void* xmlDocument,const char* xmlFileName); 66 67 /* 68 * 函数名 : SetElementAttribute 69 * 参数 : xmlElement对象 属性 属性值 70 * 描述 : 设置 XMLElement 中 attribute 的值 为 value 71 * 返回值 : 成功返回true 失败返回 false 72 */ 73 int SetElementAttribute(void* xmlElement,const char* attribute,const char* value); 74 75 /* 76 * 函数名 : SetElementNewText 77 * 参数 : xmlElement对象 属性 属性值 78 * 描述 : 设置 XMLElement 中 attribute 的值 为 value 79 * 返回值 : 成功返回true 失败返回 false 80 */ 81 int SetElementNewText(void* xmlElement,const char* text); 82 83 /* 84 * 函数名 : GetElementNewText 85 * 参数 : xmlElement对象 86 * 描述 : 获取 XMLElement 中 text 87 * 返回值 : 成功返回字符串 失败返回 NULL 88 */ 89 char* GetElementText(void* xmlElement); 90 91 /* 92 * 函数名 : FindNextSiblingElement 93 * 参数 : xmlElement对象 94 * 描述 : 查找当前element 的下一个节点 95 * 返回值 : 成功返回下一个节点 失败返回NULL 96 */ 97 void* FindNextSiblingElement(void* xmlElement); 98 99 100 /* 101 * 函数名: GetNodeAttributeValue 102 * 参数 : 根元素 属性名 属性值 103 * 描述 : 获取 element 节点属性值 104 * 返回值: 成功返回节点指针 失败返回NULL 105 */ 106 char* GetNodeAttributeValue(void* xmlElement, const char* attrKey); 107 108 109 /* 110 * 函数名: GetNodeAttributeValue 111 * 参数 : 根元素 属性名 属性值 112 * 描述 : 在同级中查找 attrKey 的值未 value 的节点 113 * 返回值: 成功返回节点指针 失败返回NULL 114 */ 115 void* FindNodeAtSameLevelByAttributeValue(void* xmlElement, const char* attrKey,const char* value); 116 117 #ifdef _cplusplus 118 } 119 #endif //_cplusplus 120 121 #endif //TINYXMLC_H_
c语言测试tinyxml 代码:
1 #include "tinyxmlc.h" 2 3 #include <stdio.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <unistd.h> 7 8 int main() 9 { 10 void* p_xmlDocument = OpenDocument("ConfigSet.xml"); 11 if(NULL == p_xmlDocument) 12 { 13 printf("xml file open error!\n"); 14 exit(1) ; 15 } 16 /*获取根节点值*/ 17 printf("%s\n",GetElementValue(p_xmlDocument)); 18 19 20 /*根据名字查找*/ 21 void* p_xmlElement = GetNodeByName(GetRootElement(p_xmlDocument),"logitem"); 22 23 24 printf("p_xmlElement: %s\n",GetElementText(p_xmlElement)); 25 26 void* p_nextElement = NULL; 27 28 //获取下一个节点 29 if(NULL != (p_nextElement = FindNextSiblingElement(p_xmlElement))) 30 { 31 //设置属性 32 SetElementAttribute(p_nextElement,"enable","8"); 33 34 //设置text 值 35 SetElementNewText(p_nextElement,"testText1"); 36 } 37 38 //根据 属性键值id=20 获取element 根据element 获取id 的值 39 printf("----%s\n",(char*)GetNodeAttributeValue(FindNodeAtSameLevelByAttributeValue(p_xmlElement,"id","20"),"id")); 40 41 SaveDocument(p_xmlDocument,"ConfigSet.xml"); 42 CloseDocument(p_xmlDocument); 43 return 0; 44 }
待续
【作者】:雨点~
【邮箱】:1003278902@qq.com

浙公网安备 33010602011771号