mxml_demo.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <time.h>
  17. #include "ql_api_osi.h"
  18. #include "ql_api_nw.h"
  19. #include "ql_log.h"
  20. #include "ql_api_datacall.h"
  21. #include "mxml.h"
  22. #include "mxml_demo.h"
  23. #define QL_MXML_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_MXML_DEMO_LOG(msg, ...) QL_LOG(QL_MXML_DEMO_LOG_LEVEL, "ql_mxml_demo", msg, ##__VA_ARGS__)
  25. static ql_task_t mxml_app_task = NULL;
  26. static int xml_parase_form_string(char *TEST_BUF)
  27. {
  28. mxml_node_t *pHead,*pNode;
  29. /* 加载 */
  30. pHead = mxmlLoadString(NULL, TEST_BUF, MXML_NO_CALLBACK);
  31. /* 查找字段为root的节点 */
  32. for (pNode = mxmlFindElement(pHead, pHead, "people", NULL, NULL, MXML_DESCEND);
  33. pNode != NULL;
  34. pNode = mxmlFindElement(pNode, pHead, "people", NULL, NULL, MXML_DESCEND))
  35. {
  36. const char *name = mxmlElementGetAttr(pNode, (const char*)"name");
  37. const char *age = mxmlElementGetAttr(pNode, (const char*)"age");
  38. const char *jop = mxmlElementGetAttr(pNode, (const char*)"job");
  39. QL_MXML_DEMO_LOG("--->getname = :%s,getage = :%s,getjob = :%s\n", name, age, jop);
  40. }
  41. mxmlDelete(pHead);
  42. return 0;
  43. }
  44. static void save_xml_file(char *dir_path,char *file_path,char * buff)
  45. {
  46. int err=0;
  47. int fd=0;
  48. err = ql_mkdir(dir_path, 0);
  49. if(err < 0)
  50. {
  51. if(err == QL_DIR_DIR_ALREADY_EXIST)
  52. {
  53. QL_MXML_DEMO_LOG("dir exist, not create");
  54. }
  55. else
  56. {
  57. QL_MXML_DEMO_LOG("make dir failed");
  58. goto exit;
  59. }
  60. }
  61. fd = ql_fopen(file_path, "wb+");
  62. if(fd < 0)
  63. {
  64. QL_MXML_DEMO_LOG("open file failed");
  65. err = fd;
  66. goto exit;
  67. }
  68. err = ql_fwrite(buff, strlen(buff) + 1, 1, fd); //strlen not include '\0'
  69. if(err < 0)
  70. {
  71. QL_MXML_DEMO_LOG("write file failed");
  72. goto exit;
  73. }
  74. QL_MXML_DEMO_LOG("save test xml file finished.");
  75. exit:
  76. if(fd)
  77. {
  78. ql_fclose(fd);
  79. }
  80. }
  81. static void read_xml_from_file(char *path,char *buff)
  82. {
  83. int err=0;
  84. int fd=0;
  85. fd = ql_fopen(path, "r");
  86. if(fd < 0)
  87. {
  88. QL_MXML_DEMO_LOG("open file failed");
  89. err = fd;
  90. goto exit;
  91. }
  92. err=ql_fread((void *)buff, 1024, 1, fd);
  93. if(err == 0)
  94. {
  95. goto exit;
  96. }
  97. else if(err < 0)
  98. {
  99. if(QL_FILE_READ_ZERO == err)
  100. {
  101. err = QL_FILE_OK;
  102. }
  103. else
  104. {
  105. QL_MXML_DEMO_LOG("read file failed");
  106. }
  107. goto exit;
  108. }
  109. exit:
  110. if(fd)
  111. {
  112. ql_fclose(fd);
  113. }
  114. }
  115. static void mxml_demo_thread(void * arg)
  116. {
  117. QL_MXML_DEMO_LOG("============ xml demo test start===================\r\n");
  118. char s8Buff[1024] = {0};
  119. ql_rtos_task_sleep_s(5);
  120. mxml_node_t *pHead = NULL, *pBody = NULL, *pPeople = NULL, *pNode = NULL;
  121. /* 创建xml文件 */
  122. pHead = mxmlNewXML("1.0");
  123. /* 创建节点root */
  124. pBody = mxmlNewElement(pHead, "root");
  125. /* 在root节点下创建子节点num */
  126. pNode = mxmlNewElement(pBody, "num");
  127. mxmlNewText(pNode, 0, "3"); //num节点创建文本内容
  128. /* 在root节点下创建子节点people */
  129. pPeople = mxmlNewElement(pBody, "people");
  130. /* 在people节点下创建子节点name */
  131. //pNode = mxmlNewElement(pPeople, "name");
  132. mxmlElementSetAttrf(pPeople, "name", "zhangsan");
  133. mxmlElementSetAttrf(pPeople, "age", "23");
  134. mxmlElementSetAttrf(pPeople, "job", "staff");
  135. /* 在root节点下创建子节点people */
  136. pPeople = mxmlNewElement(pBody, "people");
  137. mxmlElementSetAttrf(pPeople, "name", "lisi");
  138. mxmlElementSetAttrf(pPeople, "age", "20");
  139. mxmlElementSetAttrf(pPeople, "job", "student");
  140. /* 在root节点下创建子节点people */
  141. pPeople = mxmlNewElement(pBody, "people");
  142. mxmlElementSetAttrf(pPeople, "name", "wangwu");
  143. mxmlElementSetAttrf(pPeople, "age", "40");
  144. mxmlElementSetAttrf(pPeople, "job", "teacher");
  145. /* 保存生成的xml数据 */
  146. mxmlSaveString(pHead, s8Buff, sizeof(s8Buff), MXML_NO_CALLBACK);
  147. QL_MXML_DEMO_LOG("xml data strlen:%d,content:%s", strlen(s8Buff),s8Buff);
  148. /* 删除节点,释放内存 */
  149. mxmlDelete(pHead);
  150. QL_MXML_DEMO_LOG("xml parase test start");
  151. QL_MXML_DEMO_LOG("save and read xml file test start");
  152. save_xml_file(XML_DIR_PATH,XML_FILE_PATH,s8Buff);
  153. memset(s8Buff,0,sizeof(s8Buff));
  154. read_xml_from_file(XML_FILE_PATH,s8Buff);
  155. QL_MXML_DEMO_LOG("xml data strlen:%d,content:%s", strlen(s8Buff),s8Buff);
  156. xml_parase_form_string(s8Buff);
  157. ql_rtos_task_delete(mxml_app_task);
  158. return;
  159. }
  160. int ql_mxml_app_init(void)
  161. {
  162. QlOSStatus err = QL_OSI_SUCCESS;
  163. err = ql_rtos_task_create(&mxml_app_task, 8*1024, 23, "mxml_demo_app", mxml_demo_thread, NULL, 1);
  164. if(err != QL_OSI_SUCCESS)
  165. {
  166. QL_MXML_DEMO_LOG("mxml demp task create failed");
  167. }
  168. return err;
  169. }