c 解析xml c开源轻量级xml库minixml

minixml

<?xml version="1.0" encoding="UTF-8"?>
<ControllCommand>
    <!--客厅灯光 -->
    <Controll machine="light" type="场景同步"
        house="keting" sence="all"
        subnet_id="3" device_id="102" serial_area_id="1" length="11"></Controll>
    
    <Controll machine="light" type="场景同步"
        house="keting" sence="group"
        subnet_id="3" device_id="102" serial_area_id="1" length="11"></Controll>
    
    <Controll machine="light" type="回路控制"
        house="keting" sence="1"
        subnet_id="3" device_id="102" serial_area_id="1" length="4"></Controll>
</ControllCommand>

获取属性

#include <mxml.h>
#include <stdio.h>
#include <string.h>

// <Controll machine="light" type="场景同步" house="keting" sence="all" subnet_id="3" device_id="102" serial_area_id="1" length="11"/>
typedef struct myst1
{
    char machine[64];
    char type[64];
    char house[64];
    char sence[64];
    char subnet_id[64];
    char device_id[64];
    char serial_area_id[64];
    char length[64];
} lightStruct;

int main()
{
    FILE *fp;
    mxml_node_t *node;
    mxml_node_t *tree;
    lightStruct light={0};

    fp = fopen("./ctrl.xml", "r");
    tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
        

    for (node = mxmlFindElement(tree, tree, "Controll", "machine", "light",MXML_DESCEND);node != NULL;node = mxmlFindElement(node, tree, "Controll", "machine", "light",MXML_DESCEND))
    {
        // const char *mxmlElementGetAttr (mxml_node_t *node,const char *name);
        // printf("%s\n", mxmlElementGetAttr (node,"machine"));
        strcpy(light.machine,mxmlElementGetAttr (node,"machine"));
        strcpy(light.type , mxmlElementGetAttr (node,"type"));
        strcpy(light.house , mxmlElementGetAttr (node,"house"));
        strcpy(light.sence , mxmlElementGetAttr (node,"sence"));
        strcpy(light.subnet_id , mxmlElementGetAttr (node,"subnet_id"));
        strcpy(light.device_id , mxmlElementGetAttr (node,"device_id"));
        strcpy(light.serial_area_id , mxmlElementGetAttr (node,"serial_area_id"));
        strcpy(light.length , mxmlElementGetAttr (node,"length"));        
    }
    printf("%s\n", light.machine);

    fclose(fp);    

    return 0;
}


先下载安装minixml,gcc编译

gcc -o main main.c -lmxml -lpthread


转载于:https://my.oschina.net/u/2255341/blog/544337

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值