C++常用函数之XML JSON格式转换问题

数据格式在编程里面很常见,不同的系统都会有自己的标准。因为给有各的定义,每次做第三方开发系统对接的时候数据格式标准都是头疼的事情。

在开发过程中比较常见的比如有Json、XML、Key-Value等。这里我们就先看看Json和XML。两者的转换有很多开源的代码可以使用,而且也很完善,可以参考xml2jsonxsltjson

XML在Json出现前应用很广泛,灵活性好,应用语言也没有限制,发展了这么长时间后xml标准已经很臃肿。这里可以查看XML的标准XML标准。在C++里面解析和操作XML的库也有不少,tinyxml 就是个不错的选择,体积少、简单、高效的开源库,现在已经发布了TinyXml-2.

Json出来后立即被很多高级语言作为了标准推荐使用,如果想了解Json的定义请点击这里:JSON定义

XML2Json & Json2XML

接下来,我想做个简单的函数来转换。

<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>

上面的报文是在三方支付里面常见的报文,这次我们来实现对这段报文的Json格式的自由转换。

#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 ;
}

这次我们使用tinyxml2 和nlohmann json 做转换,需要将两者的头文件和源代码文件下载,并在编译中include。

nolhmann json 需要C++ 11 的支持,gcc版本需要在4.7以上。

可以使用下面命令编译:

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>

C++常用函数 XML JSON格式转换https://www.cppentry.com/benc...

开发资料https://www.cppentry.com

知识点扩展:C++常用的系统函数

数学<math.h>:

1 三角函数

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

2 反三角函数

double asin (double); 结果介于[-PI/2, PI/2]
double acos (double); 结果介于[0, PI]
double atan (double); 反正切(主值), 结果介于[-PI/2, PI/2]
double atan2 (double, double); 反正切(整圆值), 结果介于[-PI/2, PI/2]

3 双曲三角函数

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

4 指数与对数

double exp (double x); e的x次幂
double pow (double x, double y); x的y次幂
double sqrt (double);
double log (double x); 以e为底的对数,即ln x
double log10 (double x);log10(x) 以10为底。

没有以2为底的函数但是可以用换底公式解 决:log2(N)=log10(N)/log10(2)

5 取整

double ceil (double); 不小于x的最小整数
double floor (double); 不大于x的最大整数

6 绝对值

int abs(int);整型
long labs(long);长整型
double fabs (double);浮点型

7 标准化浮点数

double frexp (double f, int p); 标准化浮点数, f = x 2^p, 已知f求x, p ( x介于[0.5, 1] )
double ldexp (double x, int p); 与frexp相反, 已知x, p求f

8 取整与取余

double modf (double, double*); 将参数的整数部分通过指针回传, 返回小数部分
double fmod (double, double); 返回两参数相除的余数

9.平方根

double sqrt(double x);

字符<ctype.h>:

int isalpha(int c);c是否为字母
int isdigit(int c);c是否为数字
int tolower(int c);将c转换为小写字母
int toupper(int c);将c转换为大写字母

字符串<string.h>:

char strcpy(char sl,char s2);将字符串s2复制给s1,即覆盖
unsigned strlen(char sr);求字符串str长度

内存操作<memory.h>:

void memcpy(void d,void *s,int c);将s指向的内存区域的c个字节复制到d指向的区域

类型转换<stdlib.h>:

int atoi(char s);将字符串转化为整数
char itoa(int v,char *s,int x);将整数v按x进制转成字符串s

时间<time.h>:

time_t time(time_t *timer);返回1970/1/1零点到目前的秒数

其他<stdlib.h>:

srand(unsigned seed);设置随机数的种子
int rand();产生0-RAND_MAX的随机数
exit(int);终止正在执行的程序

keep going

到此这篇关于C++常用函数之XML JSON格式转换问题的文章就介绍到这了,更多相关c++ xml json格式转换内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。