0

I am trying to find a way to convert JSON string to XML document. For this we have evaluated PICOJSON to parse JSON and Pugixml to generate the XML document.

I know this is way easy in .Net and JAVA. My JSON is dynamic and based on the dynamic JSON I have to convert to XML.

I need some starting help on how to achieve this. Below code is sample to Parse JSON

#include <iostream>
#include "include/picojson.h";
#include "include/pugixml.hpp";
using namespace std;

int main() {
   
    const char* json =
        "{\"menu\": {"
        "\"id\": \"f\","
        "\"popup\": {"
        "  \"menuitem\": ["
        "    {\"v\": \"0\"},"
        "    {\"v\": \"1\"},"
        "    {\"v\": \"2\"}"
        "   ]"
        "  }"
        "}"
        "}";
    picojson::value v;
    std::string err;
    const char* json_end = picojson::parse(v, json, json + strlen(json), &err);
    
}

I think the initial approach would be to covert JSON String to JSON object(Not sure how to do that) and then recursively construct XML Will the below code convert JSON string to Object ?

picojson::parse(v, json, json + strlen(json), &err);
    if (!err.empty()) {
        std::cerr << err << std::endl;
    }

Can someone guide me with initial code on how to do it, how to construct XML from JSON ?

4
  • Do you know your expectation from given json? Commented May 17, 2021 at 12:38
  • 4
    "Completely new to C++" is probably not a good combination with using advanced libraries for JSON and XML. And it kind-of shows with your use of printf. First learn the basics of C++, and learn away printf or other "C-isms" like that. Then break down the somplex task of converting JSON to XML into smaller sub-problems, continue breaking down each sub-problem into smaller and smaller parts until it's not possible any more. Then implement each little and now simple problem, one by one. Commented May 17, 2021 at 12:40
  • Unrelated advice: use raw string literals to make JSONs inside CPP sources somewhat readable: godbolt.org/z/Ez937rf39 Commented May 17, 2021 at 13:31
  • The question here was more on the approach to solve it rather than asking complete code. Sometimes one will be in a situation to be Technology Agnostic and have to work on business needs of the organization. I can understand C++, sorry for the initial saying about completely new to C++. If there is some approach I will definitely try to dig deeper more on it. Commented May 17, 2021 at 14:16

1 Answer 1

0

If you're willing to use RapidJSON instead, you can make a SAX parser based on this example and map each JSON read event to an homologous XML write operation.

Hint: this probably means keeping a pugi::xml_node currentNode somewhere to keep track of the current node being worked on.


Edit
It seems POCO has SAX-like handlers after all, so you won't even need RapidJSON for this.
And if you want to minimize your dependencies, you can also use POCO.XML instead of pugixml.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the misleading, I have to use PICOJSON and PugiXML
Oh ok. According to their doc there's something similar with PicoJson too: github.com/kazuho/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.