clang mini xml


原文链接: clang mini xml

昨天简单写了在VxWorks平台应用mini-XML库解析xml文件,以及怎样用。

https://blog.csdn.net/jianzhao6205/article/details/81638197

今天将根据函数手册写一个例程,主要功能为:使用库函数生成一个xml树保存到user.xml文件,注意fopen()函数文件路径的写法("host:d:\Tornado2.2\XML\user.xml"),对生成的文件进行解析并打印。很简单的一个小程序

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<mxml.h>
int main()
{
    //文件头
    mxml_node_t *root=mxmlNewXML("1.0");
    //根标签 china 
    mxml_node_t*china =mxmlNewElement(root,"china");
    //字标签 城市
    mxml_node_t*city=mxmlNewElement(china,"city");
    mxml_node_t*info=mxmlNewElement(city,"name");
    //给标签赋值
    mxmlNewText(info,0,"北京");
    mxmlElementSetAttr(info,"isbig","Yes");
    //面积
    info=mxmlNewElement(city,"area");
    mxmlNewText(info,0,"16410平方公里");
    //人口
    info=mxmlNewElement(city,"population");
    mxmlNewText(info,0,"2171万人");
    info=mxmlNewElement(city,"GDP");
    mxmlNewText(info,0,"21410亿");

    //东京
    //字标签 城市
    city=mxmlNewElement(china,"city");
    info=mxmlNewElement(city,"name");
    //给标签赋值
    mxmlNewText(info,0,"东京");
    mxmlElementSetAttr(info,"isbig","Yes");
    //面积
    info=mxmlNewElement(city,"area");
    mxmlNewText(info,0,"2188平方公里");
    //人口
    info=mxmlNewElement(city,"population");
    mxmlNewText(info,0,"3670万人");
    info=mxmlNewElement(city,"GDP");
    mxmlNewText(info,0,"31700亿元");
    

    //数据保存到磁盘文件
    FILE *fp=fopen("china.xml","w");
    mxmlSaveFile(root,fp,MXML_NO_CALLBACK);
    fclose(fp);
    mxmlDelete(root);
    //删除释放
    //printf("Hello world\n");
    return 0;
}

最终生成的xml文件

————————————————
版权声明:本文为CSDN博主「zzzcyj」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jianzhao6205/article/details/81676068

`