minixml3.1库的使用
2020-08-16 00:23 ljtcnblogs 阅读(1310) 评论(0) 编辑 收藏 举报大家好,这几天我使用minixml 3.1库解析了xml格式数据,由于是头一次使用,且时间比较紧,总结的比较简陋。
libmxml.so解析xml数据时的内存布局如下图,以下面简单例子说明:
加载方式为:mxmlLoadString(NULL, pacData, MXML_OPAQUE_CALLBACK);
采用 MXML_OPAQUE_CALLBACK方式,跟采用MXML_NO_CALLBACK、MXML_TEXT_CALLBACK方式是不一样的
解析xml代码如下:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <iconv.h> 5 #include "mxml.h" 6 7 #define EL_RESP "Response" 8 #define EL_CMDTYPE "CmdType" 9 #define EL_NAME "Name" 10 #define EL_DEVICEID "DeviceID" 11 12 int parsexml(char *pacData) 13 { 14 mxml_node_t *root = mxmlLoadString(NULL, pacData, MXML_OPAQUE_CALLBACK); 15 mxml_node_t *resp = mxmlFindElement(root, root, EL_RESP, NULL, NULL, MXML_DESCEND_FIRST); 16 mxml_node_t *node = NULL; 17 char* pkey[] = { EL_CMDTYPE, EL_NAME, EL_DEVICEID }; 18 int i = 0, icount =sizeof(pkey) / sizeof(pkey[0]); 19 node = mxmlFindElement(resp, root, pkey[0], NULL, NULL, MXML_DESCEND_FIRST); 20 for(; i < icount; i++) 21 { 22 mxml_node_t* child = mxmlGetFirstChild(node); 23 const char* pvalue = mxmlGetOpaque(node); 24 const char* pvalue1 = mxmlGetOpaque(child); 25 printf("*** %d. %s:[%s] [%s]\n", i+1, pkey[i], pvalue, pvalue1); 26 node = mxmlFindElement(node, root, pkey[i + 1], NULL, NULL, MXML_NO_DESCEND); 27 } 28 29 return 0; 30 } 31 32 int main(int argc, char* argv[]) 33 { 34 char *cfgpath = "./ljt.xml"; 35 FILE* pfile = fopen(cfgpath, "r"); 36 if(NULL == pfile) 37 { 38 printf("*** failed to open file %s\n", cfgpath); 39 return 0; 40 } 41 char acData[1500] = { 0 }; 42 fseek(pfile, 0, SEEK_END); 43 long long llsize = ftell(pfile); 44 fseek(pfile, 0, SEEK_SET); 45 fread(acData, llsize, 1, pfile); 46 fclose(pfile); 47 printf("*** filesize = %lld\n", llsize); 48 printf("%s\n", acData); 49 50 parsexml(acData); 51 52 return 0; 53 }
20231202补加
生成如下结构mxl
生成xml代码如下:
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include "mxml.h" 4 5 int main(int argc, char* argv[]) 6 { 7 mxml_node_t *pxml = NULL, *pResp = NULL; 8 pxml = mxmlNewXML("1.0"); 9 pResp = mxmlNewElement(pxml, "Response"); 10 //CmdType 11 mxml_node_t *cmdType = mxmlNewElement(pResp, "CmdType"); 12 mxmlNewText(cmdType, 0, "QueryList"); 13 //SN 14 mxml_node_t *sn = mxmlNewElement(pResp, "SN"); 15 mxmlNewText(sn, 0, "9527"); 16 //ID 17 mxml_node_t *id = mxmlNewElement(pResp, "ID"); 18 mxmlNewText(id, 0, "51201000000"); 19 //NodeList 20 mxml_node_t *nodelist = mxmlNewElement(pResp, "NodeList"); 21 mxmlElementSetAttr(nodelist, "Num", "3"); 22 23 mxml_node_t *item = NULL, *name = NULL, *level = NULL; 24 //NodeList-item 1 25 item = mxmlNewElement(nodelist, "item"); 26 //NodeList-item-ID 27 id = mxmlNewElement(item, "ID"); 28 mxmlNewText(id, 0, "11100010002"); 29 //NodeList-item-Name 30 name = mxmlNewElement(item, "Name"); 31 mxmlNewText(name, 0, "张三"); 32 //NodeList-item-Level 33 level = mxmlNewElement(item, "Level"); 34 mxmlNewInteger(level, 11); 35 36 //NodeList-item 2 37 item = mxmlNewElement(nodelist, "item"); 38 //NodeList-item-ID 39 id = mxmlNewElement(item, "ID"); 40 mxmlNewText(id, 0, "11100010003"); 41 //NodeList-item-Name 42 name = mxmlNewElement(item, "Name"); 43 mxmlNewText(name, 0, "李四"); 44 //NodeList-item-Level 45 level = mxmlNewElement(item, "Level"); 46 mxmlNewInteger(level, 12); 47 48 const char* numVal = mxmlElementGetAttr(nodelist, "Num"); 49 printf("old node [NodeList] Num=[%s]\n", numVal); 50 mxmlElementSetAttr(nodelist, "Num", "2"); 51 numVal = mxmlElementGetAttr(nodelist, "Num"); 52 printf("new node [NodeList] Num=[%s]\n", numVal); 53 54 55 mxml_node_t *temp = mxmlFindElement(pxml, pxml, "NodeList", NULL ,NULL, MXML_DESCEND); 56 if(NULL != temp) 57 { 58 const char* pname = mxmlGetElement(temp); 59 const char* pvalue = mxmlGetOpaque(temp); 60 printf("*** element name = [%s] value = [%s]\n", pname, pvalue); 61 } 62 else 63 { 64 printf("*** no find element [%s]\n", "NodeList"); 65 } 66 char buff[1024]; 67 bzero(buff, sizeof(buff)); 68 mxmlSaveString(pxml, buff, sizeof(buff), MXML_NO_CALLBACK); 69 printf("*** new xml:\n%s\n", buff); 70 71 FILE* fp = fopen("./mytest.xml", "w"); 72 mxmlSaveFile(pxml, fp, MXML_NO_CALLBACK); 73 fclose(fp); 74 mxmlDelete(pxml); 75 76 return 0; 77 }