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


你可能感兴趣的:(C语言,mini-xml)