⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmltest.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
📖 第 1 页 / 共 2 页
字号:
    getchar();
    exit( - 1);
}


/************************************************************/

#include "mcoxml.h"

static const char xml_header1[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data>\n";
static const char xml_header2[] = "</data>";

MCO_RET output_schema(mco_db_h db)
{
    mco_trans_h t;
    FILE* f = 0;
    MCO_RET rc;

    {

        printf("\n********* Class \"Person\" xml schema: ********* \n\n");
        mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);

        if (output_to)
        {
            f = fopen(OUT_PATH("person.xsd"), "w");
            if (f == 0)
            {
                output_to = 0;
            }
        }
        Person_xml_schema(t, f, &do_print);
        if (output_to)
        {
            fclose(f);
        }

        rc = mco_trans_commit(t);
        if (rc)
        {
            return rc;
        }
    }

    mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    {
        printf("\n\n********* Class \"Children\" xml schema:********* \n\n");

        if (output_to)
        {
            f = fopen(OUT_PATH("children.xsd"), "w");
            if (f == 0)
            {
                output_to = 0;
            }
        }
        Children_xml_schema(t, f, &do_print);
        if (output_to)
        {
            fclose(f);
        }

        rc = mco_trans_commit(t);

        if (rc)
        {
            return rc;
        }
    }

    {
        printf("\n\n********* Class \"Dog\" xml schema:********* \n\n");
        mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
        if (output_to)
        {
            f = fopen(OUT_PATH("dog.xsd"), "w");
            if (f == 0)
            {
                output_to = 0;
            }
        }
        Dog_xml_schema(t, f, &do_print);
        if (output_to)
        {
            fclose(f);
        }

        rc = mco_trans_commit(t);
        if (rc)
        {
            return rc;
        }
    }

    return 0;
}


MCO_RET output_data(mco_db_h db)
{
    mco_trans_h t;
    Person p_obj;
    Children c_obj;
    Dog d_obj;
    MCO_RET rc;
    mco_cursor_t c;

    FILE* f = 0;

    rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    if (rc)
    {
        return rc;
    }

    if (output_to)
    {
        f = fopen(xml_name, "w");
    }
    if (!f)
    {
        output_to = 0;
    }

    do_print(f, xml_header1, (int)strlen(xml_header1));

    rc = Person_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Person_from_cursor(t, &c, &p_obj);
        if (rc)
        {
            return rc;
        }
        rc = Person_xml_get(&p_obj, f, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    rc = Children_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Children_from_cursor(t, &c, &c_obj);
        if (rc)
        {
            return rc;
        }
        rc = Children_xml_get(&c_obj, f, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    rc = Dog_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Dog_from_cursor(t, &c, &d_obj);
        if (rc)
        {
            return rc;
        }
        rc = Dog_xml_get(&d_obj, f, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    do_print(f, xml_header2, (int)strlen(xml_header2));
    if (f)
    {
        fclose(f);
    }

    rc = mco_trans_commit(t);
    return rc;
}

/************************************************************/

int main(int argc, char** argv)
{
    MCO_RET rc;
    mco_db_h db = 0;
    char* p;
    int i;
    char* start_mem;
    mco_trans_h t;
    mco_xml_policy_t policy;
    mco_runtime_info_t info;

    if (argc > 1)
    {
        for (i = 1; i < argc; i++)
        {
            p = argv[i];
            printf("%s\n", p);
            start: switch (*p)
            {
                case (' '): p++;
                goto start;

                case ('/'): case ('-'): 

                switch ((*++p))
                {
                    case ('F'): case ('f'): output_to = 1;

                    break;
                    default:
                        break;
                    }
                    break;
                default:
                    break;

            }
        }
    }

    _SH_();
    mco_get_runtime_info(&info);
    if (info.mco_shm_supported)
    {
        start_mem = (char*)MAP_ADDRESS;
    }
    else
    {
        start_mem = (char*)malloc(SEGSZ);
        if (!start_mem)
        {
            printf("Couldn't allocated memory\n");
            exit(1);
        }
    };

    /* set fatal error handler */
    mco_error_set_handler(&errhandler);

    rc = mco_runtime_start();
    rc = mco_db_open(dbname, XMLtestDB_get_dictionary(), start_mem, SEGSZ, (uint2)PAGESIZE);

    if (rc)
    {
        printf("\nerror creating database");
        if (!info.mco_shm_supported)
        {
            free(start_mem);
        }
        exit(1);
    }

    /* connect to the database, obtain a database handle */
    mco_db_connect(dbname, &db);

    rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    if (rc)
    {
        goto end;
    };

    if (MCO_E_UNSUPPORTED == (rc = mco_xml_get_policy(t, &policy)))
    {
        printf("\nXML is not supported!\n\n");
        goto end;
    }

    policy.text_coding = MCO_TEXT_ASCII; //BASE64;
    policy.blob_coding = MCO_TEXT_ASCII; //BASE64;
    policy.ignore_field = MCO_YES;

    mco_xml_set_policy(t, &policy);

    rc = mco_trans_commit(t);
    if (rc)
    {
        goto end;
    }

    rc = insert(db);
    if (rc)
    {
        goto end;
    }

    rc = output_data(db);
    if (rc)
    {
        goto end;
    }

    rc = output_schema(db);
    if (rc)
    {
        goto end;
    }

    rc = erase(db);

    end: 
    /* disconnect from the database, db is no longer valid */
    mco_db_disconnect(db);
    /* destroy the db */
    mco_db_close(dbname);

    if (!info.mco_shm_supported)
    {
        free(start_mem);
    }

    printf("\nPress Enter to finish\n");
    getchar();

    PROG_EXIT(0);
    ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -