XML解析 minixml使用

  • 包含头文件: mxml.h
  • 编译时需要添加动态库:libmxml.so

安装

./configure --enable-threads=no && make
sudo make install

格式对大小写敏感

<?xml version="1.0" encoding="utf-8"?>
<china>
  <city>
    <name isbig="true">
      北京
    </name>
    <area/>
    <population>
      a;slkdj
    </population>
    <GDP>
      56789
    </GDP>
  </city>
  <city_two>
    <name isbig="true">
      北京
    </name>
    <area/>
    <population>
      a;slkdj
    </population>
    <GDP>
      56789
    </GDP>
  </city_two>
</china>

程序

 #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <string.h>
  #include <fcntl.h>
  #include <mxml.h>

int main(int argc, char *argv[])
  {
      // 默认输入1.0即可,默认编码就是UTF8编码
      mxml_node_t* headNode = mxmlNewXML("1.0");
      mxml_node_t* china = mxmlNewElement(headNode,"china");
      mxml_node_t* city = mxmlNewElement(china,"city");
      mxml_node_t* info = mxmlNewElement(city,"name");
      // 给标签属性赋值
      mxmlElementSetAttr(info,"isbig","true");
      // 给标签设置内容
      mxmlNewText(info,0,"北京");
      info= mxmlNewElement(city,"area");
      info= mxmlNewElement(city,"population");
      mxmlNewText(info,0,"a;slkdj");
      // gdp
      info = mxmlNewElement(city,"GDP");
      mxmlNewText(info,0,"56789");
      mxml_node_t* city_two = mxmlNewElement(china,"city_two");
      info = mxmlNewElement(city_two,"name");
      // 给标签属性赋值
      mxmlElementSetAttr(info,"isbig","true");
      // 给标签设置内容
      mxmlNewText(info,0,"北京");
      info= mxmlNewElement(city_two,"area");
      info= mxmlNewElement(city_two,"population");
      mxmlNewText(info,0,"a;slkdj");
      // gdp
      info = mxmlNewElement(city_two,"GDP");
      mxmlNewText(info,0,"56789");
      // 保存节点到xml文件
      FILE* fd = fopen("china.xml","w");
      mxmlSaveFile(headNode,fd,MXML_NO_CALLBACK);
      fclose(fd);
      return 0;
  }

解析

 #include <stdio.h>                                                      
  #include <unistd.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <string.h>
  #include <mxml.h>
      
✹ int main(int argc, char *argv[])                                        
  {
      FILE *fd= fopen(argv[1],"r");
      mxml_node_t* root = mxmlLoadFile(NULL,fd,MXML_NO_CALLBACK);
      /*
       *  DESCEND含义是不查看任何的子节点在XML元素层次中,仅查看同级的伙伴
       *  MXML_DESCEND_FIRST含义是向下搜索到一个节点的第一个匹配子节点,但
       *  DESCEND含义是不查看任何的子节点在XML元素层次中,
       * */
      mxml_node_t* china = mxmlFindElement(root,root,"china",NULL,NULL,MXM
      while(china)
     {        
          mxml_node_t* info = mxmlWalkNext(china,root,MXML_DESCEND);
          while(info)
          {
              mxml_node_t* node = mxmlWalkNext(info,root,MXML_DESCEND_FIRS
              printf("context =  %s\n",mxmlGetText(node,0));
               node = mxmlWalkNext(node,root,MXML_NO_DESCEND);
              printf("context =  %s\n",mxmlGetText(node,0));
               node = mxmlWalkNext(node,root,MXML_NO_DESCEND);
              printf("context =  %s\n",mxmlGetText(node,0));
               node = mxmlWalkNext(node,root,MXML_NO_DESCEND);
              printf("context =  %s\n",mxmlGetText(node,0));
              info = mxmlFindElement(info,root,"city",NULL,NULL,MXML_NO_DE
          }
          // arg1: 当前节点 arg2: 顶级节点 arg3: 查找的节点名称 arg4: 属性 arg5: value arg6: 查找模式
          china = mxmlFindElement(china,root,"china",NULL,NULL,MXML_NO_DES
      }
  
          return 0;
  }

posted on 2021-05-11 16:27  lodger47  阅读(815)  评论(0)    收藏  举报

导航