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 ?
printf. First learn the basics of C++, and learn awayprintfor 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.