首页 > 代码库 > json转xml

json转xml

#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 =http://www.mamicode.com/ 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