XML JSON format conversion of C + + common functions

Time:2021-9-24

Data format is very common in programming. Different systems will have their own standards. Because there are different definitions for each, the data format standard is a headache every time the third-party development system is docked.

Common in the development process, such as JSON, XML, key value, etc. Let’s take a look at JSON and XML first. There are many open source codes for the conversion between the two, and they are also very perfect. You can refer to xml2json and XSLT JSON.

Before the appearance of JSON, XML was widely used, with good flexibility and no restrictions on the application language. After such a long time of development, the XML standard has become very bloated. Here you can view the XML standard. There are also many libraries for parsing and operating XML in C + +. Tinyxml is a good choice. Tinyxml-2 is a small, simple and efficient open source library. Now tinyxml-2 has been released

JSON was recommended as a standard by many high-level languages immediately after it came out. If you want to know the definition of JSON, please click here: JSON definition

XML2Json & Json2XML

Next, I want to do a simple function to convert.


<xml>
  <appid>appid-value111111</appid>
  <mch_id>mch_id-value22222</mch_id>
  <nonce_str>nonce_str-value3333333</nonce_str>
  <transaction_id>transaction_id-value44444444</transaction_id>
  <sign>sign-value5555555555</sign>
</xml>

The above message is a common message in three-party payment. This time, we will realize the free conversion of the JSON format of this message.


#include <string>
#include <iostream>
#include "tinyxml2.h"
#include "nlohmann/json.hpp"

using json = nlohmann::json;
using namespace tinyxml2;
using namespace std;

string xml2json(string &src)
{
  XMLDocument doc;
  doc.Parse( src.c_str() );
  
  json root;
  XMLElement* rootElement = doc.RootElement();
  XMLElement* child = rootElement->FirstChildElement();
  while(child) {
    const char* title = child->Name() ;
    const char* value = child->GetText();
    child = child->NextSiblingElement();
    root[title]=value ;
  }
  return root.dump() ;
}

string json2xml(string& src)
{
  XMLDocument xmlDoc;
  XMLNode * pRoot = xmlDoc.NewElement("xml");
  xmlDoc.InsertFirstChild(pRoot);
  auto j3 = json::parse(src.c_str());
  for (json::iterator it = j3.begin(); it != j3.end(); ++it) {
    string key = it.key();
    string value = it.value() ;
    XMLElement * pElement = xmlDoc.NewElement(key.c_str()) ;
    pElement->SetText(value.c_str()) ;
    pRoot->InsertEndChild(pElement);
  }
  XMLPrinter printer;
  pRoot->Accept( &printer );
  return printer.CStr();
}

int main()
{
  string src = "<xml>\
          <appid>appid-value111111</appid>\
          <mch_id>mch_id-value22222</mch_id>\
          <nonce_str>nonce_str-value3333333</nonce_str>\
          <transaction_id>transaction_id-value44444444</transaction_id>\
          <sign>sign-value5555555555</sign>\
        </xml>" ;
  string json = xml2json(src) ;
  string xml = json2xml(json) ;

  cout << json ;
  cout << endl ;
  cout << xml ;
}

This time we use tinyxm2 and nlohmann JSON for conversion. We need to download their header files and source code files and include them in the compilation.

Nolhmann JSON needs the support of C + + 11, and the GCC version needs to be above 4.7.

You can compile using the following command:

g++ -std=c++11 xmljson.cpp tinyxml2.cpp -I./


./a.out
{"appid":"appid-value111111","mch_id":"mch_id-value22222","nonce_str":"nonce_str-value3333333","sign":"sign-value5555555555","transaction_id":"transaction_id-value44444444"}
<xml>
  <appid>appid-value111111</appid>
  <mch_id>mch_id-value22222</mch_id>
  <nonce_str>nonce_str-value3333333</nonce_str>
  <sign>sign-value5555555555</sign>
  <transaction_id>transaction_id-value44444444</transaction_id>
</xml>

XML JSON format conversion of C + + common functions https://www.cppentry.com/benc…

Development data https://www.cppentry.com

Knowledge point expansion: common system functions in C + +

Math < math. H >:

Trigonometric function

double sin (double);
double cos (double);
double tan (double);

2 inverse trigonometric function

double asin (double); The result is between [- pi / 2, PI / 2]
double acos (double); The result is between [0, PI]
double atan (double); Arctangent (principal value), the result is between [- pi / 2, PI / 2]
double atan2 (double, double); Arctangent (round value), the result is between [- pi / 2, PI / 2]

3 hyperbolic trigonometric function

double sinh (double);
double cosh (double);
double tanh (double);

4 exponent and logarithm

double exp (double x); The X-Power of e
double pow (double x, double y); Y power of X
double sqrt (double);
double log (double x); Logarithm based on e, i.e. ln x
double log10 (double x); Log10 (x) is based on 10.

There is no function based on 2, but it can be solved with the bottom changing formula: log2 (n) = log10 (n) / log10 (2)

5 rounding

double ceil (double); Minimum integer not less than x
double floor (double); Maximum integer not greater than x

6 absolute value

int abs(int); integer
long labs(long); Long integer
double fabs (double); float

7 standardized floating point number

double frexp (double f, int p); Normalized floating point number, f = x 2 ^ P, known f for X, P (x between [0.5, 1])
double ldexp (double x, int p); In contrast to frexp, X and P are known to find F

8 rounding and remainder

double modf (double, double*); Returns the integer part of the parameter through the pointer and the decimal part
double fmod (double, double); Returns the remainder of the division of two parameters

9. Square root

double sqrt(double x);

Characters < ctype. H >:

int isalpha(int c); Is c a letter
int isdigit(int c); Is c a number
int tolower(int c); Convert C to lowercase
int toupper(int c); Convert C to uppercase

String < string. H >:

char strcpy(char sl,char s2); Copy the string S2 to S1, that is, overwrite
unsigned strlen(char sr); Find string STR length

Memory operation < memory. H >:

void memcpy(void d,void *s,int c); Copy the C bytes of the memory area pointed to by s to the area pointed to by D

Type conversion < stdlib. H >:

int atoi(char s); Convert string to integer
char itoa(int v,char *s,int x); Convert integer V to string s in base x

Time < time. H >:

time_ t time(time_t *timer); Returns the number of seconds from 1970 / 1 / 1 zero to the present

Other < stdlib. H >:

srand(unsigned seed); Seed random numbers
int rand(); Generate 0-rand_ Max random number
exit(int); Terminate the program being executed

keep going

This is the end of this article on XML JSON format conversion of common C + + functions. For more information about C + + XML JSON format conversion, please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!