使用mini-XML库实现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<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include"D:\Tornado2.2\XML\xmlTest\src\vxw5\config.h"
#include"D:\Tornado2.2\XML\xmlTest\src\mxml.h"

int vmain()
{
    FILE *fp;
    FILE *fptra;
    mxml_node_t *tree;
    mxml_node_t *xml;    /* <?xml ... ?> */
    mxml_node_t *data;   /* <data> */
    mxml_node_t *node;   /* <node> */
    mxml_node_t *group;  /* <group> */
    /* 创建一个新的文档的版本号 */
    xml = mxmlNewXML("1.0");

    data = mxmlNewElement(xml, "data");

        node = mxmlNewElement(data, "node");
        mxmlElementSetAttrf(node, "id", "0x01");
        mxmlElementSetAttrf(node, "msg", "This is 0x01");

        node = mxmlNewElement(data, "node");
        mxmlElementSetAttrf(node, "id", "0x02");
        mxmlElementSetAttrf(node, "msg", "This is 0x02");

        node = mxmlNewElement(data, "node");
        mxmlElementSetAttrf(node, "id", "0x03");
        mxmlElementSetAttrf(node, "msg", "This is 0x03");

        group = mxmlNewElement(data, "group");

            node = mxmlNewElement(group, "node");
            mxmlElementSetAttrf(node, "id", "0x04");
            mxmlElementSetAttrf(node, "msg", "This is 0x04");

            node = mxmlNewElement(group, "node");
            mxmlElementSetAttrf(node, "id", "0x05");
            mxmlElementSetAttrf(node, "msg", "This is 0x05");

        node = mxmlNewElement(data, "node");
        mxmlElementSetAttrf(node, "id", "0x06");
        mxmlElementSetAttrf(node, "msg", "This is 0x06");

        node = mxmlNewElement(data, "node");
        mxmlElementSetAttrf(node, "id", "0x07");
        mxmlElementSetAttrf(node, "msg", "This is 0x07");
    /* 创建一个文件并将生成的xml保持存到文件中,注意保存路径是已经存在的 */
    fp = fopen("host:d:\\Tornado2.2\\XML\\user.xml", "w");
    mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
    fclose(fp);
    mxmlDelete(xml);
 
    fptra = fopen("host:d:\\Tornado2.2\\XML\\user.xml", "r");
    tree = mxmlLoadFile(NULL, fptra, MXML_TEXT_CALLBACK);
    fclose(fptra);
    /* 遍历上述创建的文档,并将所有的属性和值打印 */
    for (node = mxmlFindElement(tree, tree, "node", NULL, NULL, MXML_DESCEND);
         node != NULL;
         node = mxmlFindElement(node, tree, "node", NULL, NULL, MXML_DESCEND))
    {
        printf("id = %s, msg = %s \n", 
            mxmlElementGetAttr( node, "id"), mxmlElementGetAttr( node, "msg"));
    }
    printf("\n");
    return 0 ;
}

最终生成的xml文件

<?xml version="1.0" encoding="utf-8"?>
<data>
    <node id="0x01" msg="This is 0x01"/>
    <node id="0x02" msg="This is 0x02"/>
    <node id="0x03" msg="This is 0x03"/>
    <group>
        <node id="0x04" msg="This is 0x04"/>
        <node id="0x05" msg="This is 0x05"/>
    </group>
    <node id="0x06" msg="This is 0x06"/>
    <node id="0x07" msg="This is 0x07"/>
</data>

用到的资源在这里 https://download.csdn.net/download/jianzhao6205/10601698  

如果有没有分的可以私信我,我看到了会发给你的,不为赚积分,共同学习

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值