加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml

发布时间:2020-12-16 00:06:56 所属栏目:百科 来源:网络整理
导读:http://blog.chinaunix.net/uid-25885064-id-3180311.html 起先使用libxml2解析xml文件,使用该库文件还需要libiconv库文件,编译出来的程序很大。由于嵌入式本身就受到资源的限制,影响到了其它程序的存储。最后,只能放弃使用xml2了。mxml比较精小,适合在

http://blog.chinaunix.net/uid-25885064-id-3180311.html




起先使用libxml2解析xml文件,使用该库文件还需要libiconv库文件,编译出来的程序很大。由于嵌入式本身就受到资源的限制,影响到了其它程序的存储。最后,只能放弃使用xml2了。mxml比较精小,适合在嵌入式环境下解析xml文件,官方论坛也有中文教程。不多说,下面给出我写的一个实例,供大家参考。

1、创建一个文件夹,用来存放库文件及测试代码
# mkdir mxml
2、到官方网站http://www.minixml.org/software.php下载libmxml库文件,并将其解压到该目录下。我所使用的是2.7的版本,解压后目录为mxml-2.7。
# cd mxml
# tar -xzvf libxml2-sources-2.7.8.tar.gz
3、编译mxml库
# cd mxml-2.7
# ./configure
# make
4、创建一个test文件夹,存放info.xml及编写的测试代码
# cd ..
# mkdir test
# cd test
info.xml信息如下:

点击(此处)折叠或打开

  1. <?xmlversion="1.0"?>
  2. <node>
  3. <music<state stat"China"<url>http://www.baidu.com/url/state/music<search"USA".google/search/node>
test.c编写如下:

点击(此处)折叠或打开

#include<string.h>
  • #include<stdio>
  • #include<stdlib>
  • #include"mxml.h"

  • intmain(intargc,char**argv)
  • {
  • inti;
  • FILE*fp;
  • mxml_node_t*tree*node*search*state*url;

  • fp=fopen"info.xml""r");
  • if(fp=NULL{
  • printf"open the info.xml error!n";
  • return-1}
  • tree=mxmlLoadFile(MXML_TEXT_CALLBACK;
  • fclose(tree"Load file error!n"}

  • node=mxmlFindElement"node"(node"can not find element node!n"}

  • search"search"(search"can not find element search!n"}

  • for(state"state""stat";
  • state!{
  • (i=0;i<state->value.element.num_attrs+(memcmp.attrs[i].name"value:%sn".value;
  • url"url";
  • printf"url:%sn"(url}
  • }

  • mxmlDelete"success!n";

  • return 0;
  • }
  • 5、编译我们写的测试代码
    # gcc test.c -o test -static -Os -g -I ../mxml-2.7 -L ../mxml-2.7 -lmxml -lpthread
    6、运行测试
    # ./test
    测试结果如下:
    value:China
    url:http://www.baidu.com
    value:USA
    url:http://www.google.com
    success!

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读