How to convert XML to JSON in C++?

后端 未结 5 373
日久生厌
日久生厌 2021-01-24 08:00

I came across How to convert XML to JSON in ASP.NET C# (link) and one in javascript (at goessner.net/download/prj/jsonxml/). But have yet to find one in c++ that takes just a st

相关标签:
5条回答
  • 2021-01-24 08:37

    Ideally this would be done using XSLT instead writing a translator in C++/C#. You can find some solutions online (for example this one).

    However, if you would prefer to do it in C++ then I recommend the jsoncpp library.

    0 讨论(0)
  • 2021-01-24 08:38

    Have you looked at http://www.json.org/?

    0 讨论(0)
  • 2021-01-24 08:43

    By far, the only specifically designed C++ library that directly converts XML to JSON I found on the Internet is xml2json: https://github.com/Cheedoong/xml2json

    0 讨论(0)
  • 2021-01-24 08:46

    I implemented this function to convert xml to json using jsoncpp & rapidxml libraries, maybe will help someone. To compile : g++ -o xmltojson main.cpp -ljsoncpp. Tested on debian 9 only.

    #include <set>
    #include <string>
    #include <string.h>
    #include <iostream>
    
    #include <jsoncpp/json/json.h>
    #include <rapidxml/rapidxml.hpp>
    
    void parse_node_tree(const rapidxml::xml_node<>* node, Json::Value& parent)
    {
        Json::Value obj(Json::objectValue);
    
        std::multiset<std::string> array;
        for(rapidxml::xml_node<> *child = node->first_node(); child != nullptr; child = child->next_sibling())
        {
            if(child->type() != rapidxml::node_element)
                    continue;
    
            array.insert(child->name());
        }
    
        if(node->value_size() > 0)
        {
            obj["#text"] = node->value();
        }
    
        bool hasChilds = false;
    
        for(rapidxml::xml_attribute<> *attr = node->first_attribute(); attr != nullptr; attr = attr->next_attribute())
        {
            hasChilds = true;
            obj[attr->name()] = attr->value();
        }
    
        for(rapidxml::xml_node<> *child = node->first_node(); child != nullptr; child = child->next_sibling())
        {
            if(child->type() != rapidxml::node_element)
                       continue;
    
            hasChilds = true;
    
            Json::Value& next = obj[child->name()];
            if(array.count(child->name()) > 1 && !next.isArray())
            {
                    next = Json::arrayValue;
            }
    
            parse_node_tree(child, next);
        }
    
        // set result.
        if(parent.isArray())
        {
            parent.append(obj);
        }
        else
        {
            if(obj.isArray() || hasChilds)
                parent = obj;
            else
                parent = node->value();
        }
    }
    
    
    // convert xml string to json
    std::string xmltojson(const std::string& xml)
    {
        char xml_text[xml.size()+1];
    
        memset(xml_text, 0, xml.size());
        strncpy(xml_text, xml.c_str(), xml.size());
        xml_text[xml.size()] = '\0';
    
        rapidxml::xml_document<> doc;
    
        try
        {
                doc.parse<0>(xml_text);
        }
        catch(rapidxml::parse_error& exp)
        {
            std::cout << exp.what() << std::endl;
            return std::string();
        }
    
        rapidxml::xml_node<> *node = doc.first_node();
        if(node == nullptr)
            return std::string();
    
        Json::Value jdoc;
        Json::Value& jroot = jdoc[node->name()];
    
        parse_node_tree(node, jroot);
    
        Json::FastWriter fast_writer;
        return fast_writer.write(jdoc);
    }
    
    int main(int argc, char** argv)
    {
        // xml
        std::string test_xml = "<anagrafica><testata><nomemercato id='007'>Mercato di test</nomemercato><data>Giovedi 18 dicembre 2003 16.05.29</data></testata><record><codice_cliente>5</codice_cliente><rag_soc>Miami American Cafe</rag_soc><codice_fiscale>IT07654930130</codice_fiscale><indirizzo tipo='casa'>Viale Carlo Espinasse 5, Como</indirizzo><num_prodotti>13</num_prodotti></record><record><codice_cliente>302</codice_cliente><rag_soc>Filiberto Gilardi</rag_soc><codice_fiscale>IT87654770157</codice_fiscale>    <indirizzo tipo='ufficio'>Via Biancospini 20, Messina</indirizzo><num_prodotti>8</num_prodotti> </record><record><codice_cliente>1302</codice_cliente><rag_soc>Eidon</rag_soc><codice_fiscale>IT887511231</codice_fiscale><indirizzo tipo='ufficio'>Via Bassini 17/2, Milano</indirizzo><num_prodotti>18</num_prodotti> </record><record><codice_cliente>202</codice_cliente><rag_soc>SkillNet</rag_soc><codice_fiscale>IT887642131</codice_fiscale><indirizzo tipo='ufficio'>Via Chiasserini 11A, Milano</indirizzo><num_prodotti>24</num_prodotti></record><record><codice_cliente>12</codice_cliente><rag_soc>Eidon</rag_soc><codice_fiscale>IT04835710965</codice_fiscale><indirizzo tipo='casa'>Via Cignoli 17/2, Roma</indirizzo><num_prodotti>1112</num_prodotti></record><record><codice_cliente>5</codice_cliente><rag_soc>Miami American Cafe</rag_soc><codice_fiscale>IT07654930130</codice_fiscale><indirizzo tipo='casa'>Viale Carlo Espinasse 5, Como</indirizzo><num_prodotti>13</num_prodotti></record><record><codice_cliente>302</codice_cliente><rag_soc>Filiberto Gilardi</rag_soc><codice_fiscale>IT87654770157</codice_fiscale><indirizzo tipo='ufficio'>Via Biancospini 20, Messina</indirizzo><num_prodotti>8</num_prodotti></record><record><codice_cliente>1302</codice_cliente><rag_soc>Eidon</rag_soc><codice_fiscale>IT887511231</codice_fiscale><indirizzo tipo='ufficio'>Via Bassini 17/2, Milano</indirizzo><num_prodotti>18</num_prodotti></record><record><codice_cliente>202</codice_cliente><rag_soc>SkillNet</rag_soc><codice_fiscale>IT887642131</codice_fiscale><indirizzo tipo='ufficio'>Via Chiasserini 11A, Milano</indirizzo><num_prodotti>24</num_prodotti></record><record><codice_cliente>202</codice_cliente><rag_soc>SkillNet</rag_soc><codice_fiscale>IT887642131</codice_fiscale><indirizzo tipo='ufficio'>Via Chiasserini 11A, Milano</indirizzo><num_prodotti>24</num_prodotti></record><record><codice_cliente>12</codice_cliente><rag_soc>Eidon</rag_soc><codice_fiscale>IT04835710965</codice_fiscale><indirizzo tipo='casa'>Via Cignoli 17/2, Roma</indirizzo><num_prodotti>1112</num_prodotti></record></anagrafica>";
    
        // convert
        std::string json = xmltojson(test_xml);
    
        // log
        std::cout << test_xml << std::endl;
        std::cout << std::endl;
        std::cout << json << std::endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-24 08:51

    Please head to http://json.org. You may find something of interest there. It lists many libraries available for JSON processing in C++ and many other languages.

    But for the thing you linked to [pdhcc] you may have to do some work yourself to port it to C++.

    0 讨论(0)
提交回复
热议问题