XmlRPC入门_组合类型操作

1、数组操作

#include <iostream>
#include <winsock2.h>
#include <windows.h>

#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value* myArrayP = xmlrpc_build_value(&env, "(iis)", 5, 7,"abc");
    xmlrpc_value* firstElementP;
    xmlrpc_value* secondElementP;
    xmlrpc_value* thirdElementP;
    xmlrpc_int firstInt;
    xmlrpc_int secondInt;
    const char* thirdStr;

    printf("Array has %u elements\n", xmlrpc_array_size(&env, myArrayP));

    xmlrpc_array_read_item(&env, myArrayP, 0, &firstElementP);
    xmlrpc_array_read_item(&env, myArrayP, 1, &secondElementP);
    xmlrpc_array_read_item(&env, myArrayP, 2, &thirdElementP);

    xmlrpc_read_int(&env, firstElementP, &firstInt);
    xmlrpc_read_int(&env, secondElementP, &secondInt);
    xmlrpc_read_string(&env, thirdElementP, &thirdStr);
    printf("First element is %d\n", firstInt);
    printf("Second element is %d\n", secondInt);
    printf("Third element is %s\n", thirdStr);

    system("pause");
}

 2、结构体操作

#include <iostream>
#include <winsock2.h>
#include <windows.h>

#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value *myStructP =
        xmlrpc_build_value(&env, "{s:i}", "age", 32);

    xmlrpc_value * ageP;

    xmlrpc_struct_find_value(&env, myStructP, "age", &ageP);

    if (ageP) {
        xmlrpc_int age;
        xmlrpc_read_int(&env, ageP, &age);
        printf("age is %d\n", age);
    }
    else
        printf("There is no member named 'age'");

    system("pause");
}

  3、数组结构体组合操作

#include <iostream>
#include <winsock2.h>
#include <windows.h>

#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    xmlrpc_env env;
    xmlrpc_env_init(&env);

    xmlrpc_value *temperaturesP;

    temperaturesP = xmlrpc_build_value(&env,
        "({s:d,s:d}{s:d,s:d}{s:d,s:d})",
        "min", 0.2,
        "max", 20.1,
        "min", 0.5,
        "max", 31.9,
        "min", 5.75,
        "max", 35.9
    );

    struct {
        double min;
        double max;
    } temp[3];

    xmlrpc_decompose_value(&env, temperaturesP, "({s:d,s:d,*}{s:d,s:d,*}{s:d,s:d,*})",
        "min", &temp[0].min,
        "max", &temp[0].max,
        "min", &temp[1].min,
        "max", &temp[1].max,
        "min", &temp[2].min,
        "max", &temp[2].max);
    for (int i = 0; i < 3; i++)
    {
        std::cout << "temp[" << i << "] min value is:" << temp[i].min << std::endl;
        std::cout << "temp[" << i << "] max value is:" << temp[i].max << std::endl;
    }
    system("pause");
}
posted @ 2023-11-30 17:16  左边的翼  阅读(23)  评论(0编辑  收藏  举报