simple note of minixml

本文介绍了一个轻量级XML解析库MiniXML的应用实例。通过简单的代码示例,展示了如何加载XML文件,并利用回调函数指定节点数据类型,进而遍历和解析XML内容。

minixml-study note

由于项目的需求,需要对xml文件做解析,但我们的xml文件非常简单,又是在嵌入式设备上运行,因此需要找一个
无比轻量的xml解析库

http://cn.bing.com/search?q=minixml

因为只做如下类似的xml格式的解析,非常简洁,下了一个最原始的版本mxml-1.3,哥想
原始版本就应该能够解析了吧,

<?xml version="1.0" encoding="UTF-8"?>
<feature>
 <option>
  <name>wifi_ssid</name>
  <admin>false</admin>
 </option>
 <option>
  <name>wifi_restore</name>
  <admin>true</admin>
 </option>
</feature>

minixml库非常精湛短小,

step1,
tree = mxmlLoadFile(NULL, fp, type_cb);
type_cb作为一个回调函数,可以写也可以不写,实现这样的函数是为了告诉库当前的node是什么样的数据类型,
minixml定义了以下几种数据类型

typedef enum mxml_type_e  /**** The XML node type. ****/
{
  MXML_ELEMENT,    /* XML element with attributes */
  MXML_INTEGER,    /* Integer value */
  MXML_OPAQUE,    /* Opaque string */
  MXML_REAL,    /* Real value */
  MXML_TEXT    /* Text fragment */
} mxml_type_t;
举一个例子,<name type="Integer">2</name>,那可以写一个type_cb回调函数,返回MXML_INTEGER类型,那minixml就会解析2为整形,而
不会以一个text/string的形式存放。


step2.
mxmlWalkNext(tree,NULL,MXML_DESCEND);
调用函数遍历node,做你想做的事吧

作为解析就到此为止了,so easy!


sample code:哥简单修改了一下的test code,做了一个解析
  if (argv[1][0] == '<')
    tree = mxmlLoadString(NULL, argv[1], type_cb);
  else if ((fp = fopen(argv[1], "r")) == NULL)
  {
    perror(argv[1]);
    return (1);
  }
  else
  {
   /*
    * Read the file...
    */

    tree = mxmlLoadFile(NULL, fp, type_cb);

    fclose(fp);
  }

  mxml_node_t* tmp = NULL;
  mxml_node_t* ss = NULL;

   while(tree)
   {
        if(tree->type == MXML_ELEMENT)
        {
            if(strcmp(tree->value.element.name,"option") == 0)
            {
                tmp = tree->child;
                while(tmp)
                {
                    if(MXML_ELEMENT == tmp->type)
                    {
                        fprintf(stdout,"tmp val = %s\n",tmp->value.element.name);

                        ss = tmp->child;

                        if(ss && ss->type == MXML_TEXT)
                        {
                            fprintf(stdout,"ss val = %s\n",ss->value.text.string);    
                        }
                       
                    }
                    else if(MXML_TEXT)
                    {
                        /*fprintf(stdout,"tmp val = %s\n",tmp->value.text.string); */ 
                    }

                    tmp = tmp->next;
                }
            }
           
        }
        tree = mxmlWalkNext(tree,NULL,MXML_DESCEND);
   }

  
运行结果
tmp val = name
ss val = wifi_ssid
tmp val = admin
ss val = false
tmp val = name
ss val = wifi_restore
tmp val = admin
ss val = true

(venv) E:\pycharm\study\dorm_face_recognition\model_training>pip install dlib Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting dlib Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/f4/f8949b18ec1df2ef05fc2ea1d1dd82ff2d050b8704b7d0d088017315c221/dlib-20.0.0.tar.gz (3.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 6.6 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [48 lines of output] running bdist_wheel running build running build_ext Traceback (most recent call last): File "E:\python3.9.13\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "E:\python3.9.13\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "E:\pycharm\study\venv\Scripts\cmake.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named 'cmake' ================================================================================ ================================================================================ ================================================================================ CMake is not installed on your system! Or it is possible some broken copy of cmake is installed on your system. It is unfortunately very common for python package managers to include broken copies of cmake. So if the error above this refers to some file path to a cmake file inside a python or anaconda or miniconda path then you should delete that broken copy of cmake from your computer. Instead, please get an official copy of cmake from one of these known good sources of an official cmake: - cmake.org (this is how windows users should get cmake) - apt install cmake (for Ubuntu or Debian based systems) - yum install cmake (for Redhat or CenOS based systems) On a linux machine you can run `which cmake` to see what cmake you are actually using. If it tells you it's some cmake from any kind of python packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn't run from the same terminal window from which you are reading this error message, then you have not installed cmake. Windows users should take note that they need to tell the cmake installer to add cmake to their PATH. Since you can't run commands that are not in your PATH. This is how the PATH works on Linux as well, but failing to add cmake to the PATH is a particularly common problem on windows and rarely a problem on Linux. ================================================================================ ================================================================================ ================================================================================ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Failed to build dlib ERROR: Could not build wheels for dlib, which is required to install pyproject.toml-based projects
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值