json轉xml

來源:互聯網
上載者:User

標籤:close   count   buffer   arrays   tin   roo   root   doc   create   

#include    <stdio.h>#include    <string.h>#include    <stdlib.h>#include    <sys/stat.h>#include    <libxml/xmlmemory.h>#include    <libxml/parser.h>#include    "cJSON.h"void json_loop(cJSON* json_root, xmlNodePtr xml_root){    int        count, i;    cJSON    *json_cur = json_root, *json_node;        xmlNodePtr    xml_node;    xmlNodePtr    xml_content;    for(json_cur = json_root; json_cur; json_cur = json_cur->next)    {        count = cJSON_GetArraySize(json_cur);        if(0 == count)        {            fprintf(stdout, "value:[%s]\n", json_cur->valuestring);            xml_node = xmlNewNode(NULL, json_cur->string);            xml_content = xmlNewText(json_cur->valuestring);            xmlAddChild(xml_node, xml_content);            xmlAddChild(xml_root, xml_node);            continue;            }        else        {            if(NULL == json_cur->string)                fprintf(stdout, "loop\n");            else            {                xml_node = xmlNewNode(NULL, json_cur->string);                json_loop(json_cur->child, xml_node);                xmlAddChild(xml_root, xml_node);            }        }    }}int main(int argc, char* argv[]){    char    filename[1024], line[1024], *data;        FILE    *fp = NULL;    struct    stat st;    xmlDocPtr    xml_doc;    xmlNodePtr    xml_root;        if(1 >= argc)    {        fprintf(stderr, "usage:%s filename\n", argv[0]);        return -1;    }    memset(filename, 0, sizeof(filename));    strcpy(filename, argv[1]);        if(NULL == (fp = fopen(filename, "r")))    {        fprintf(stderr, "open file [%s] failed!\n", filename);        return -1;    }    memset(&st, 0, sizeof(st));    stat(filename, &st);        data = (char*)malloc(st.st_size + 1);    memset(data, 0, st.st_size + 1);    memset(line, 0, sizeof(line));    while(fgets(line, sizeof(line) - 1, fp))    {        strncpy(data + strlen(data), line, strlen(line));        memset(line, 0, sizeof(line));    }    fclose(fp);    cJSON    *json_root = cJSON_Parse(data);        char    *json_data = cJSON_Print(json_root);    fprintf(stdout, "JSON:\n%s\n", json_data);    free(json_data);        xmlBufferPtr    buff = xmlBufferCreate();    xml_doc = xmlNewDoc(BAD_CAST"1.0");    xml_root = xmlNewNode(NULL, json_root->child->string);    fprintf(stdout, "xml_root:[%s]\n", xml_root->name);    xmlDocSetRootElement(xml_doc, xml_root);        json_loop(json_root->child->child, xml_root);                xmlChar*    dump;    int            size;    xmlDocDumpFormatMemory( xml_doc, &dump, &size, 1 );    xmlBufferEmpty( buff );    xmlBufferAdd( buff, dump, size );    xmlFree( dump );        fprintf(stdout, "-------------------------------------\n");    fprintf(stdout, "XML:\n%s\n", xmlBufferContent(buff));    xmlBufferFree(buff);    xmlFreeDoc(xml_doc);    cJSON_Delete(json_root);    return 0;    }

問題:JSON中的迴圈節點如何處理?

json轉xml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.