-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2jsonqt.cpp
136 lines (114 loc) · 3.37 KB
/
xml2jsonqt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "xml2jsonqt.h"
#include <iostream>
#include <string>
#ifdef WITH_QT
#include <QFile>
#include <QDomDocument>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMap>
#include <QVariant>
using namespace std;
class Xml2JsonQtPrivate{
public:
QVariantMap getAttrName(const QDomNamedNodeMap &attr)
{
QMap<QString, QVariant> res;
for(int i = 0; i < attr.size(); ++i){
QString n = attr.item(i).nodeName();
QString v = attr.item(i).nodeValue();
res[n] = QList<QVariant>() << v;
}
return res;
}
void parseDom(const QDomNode &node, QMap<QString, QVariant> &out)
{
QString nodeName;
auto it = node.firstChild();
while(!it.isNull()){
nodeName = it.nodeName();
auto attr = getAttrName(it.attributes());
auto lst = out[nodeName].toList();
if(!it.firstChild().isText()){
QMap<QString, QVariant> params;
parseDom(it, params);
lst.append(params);
}else{
lst.append(it.firstChild().nodeValue());
}
if(!attr.empty()){
if(lst.last().type() == QVariant::Map){
auto map = lst.last().toMap();
attr.insert(map);
lst.last() = attr;
}
}
out[nodeName]= lst;
it = it.nextSibling();
}
}
void writeToJson(QJsonObject &json, const QString &key, const QVariant &val)
{
if(val.type() == QVariant::Map){
writeToJson(json, val.toMap());
}else{
json[key] = val.toString();
}
}
void writeToJson(QJsonObject &json, const QMap<QString, QVariant> ¶ms)
{
if(params.isEmpty())
return;
for(auto it = params.begin(); it != params.end(); ++it){
QString key = it.key();
QVariant value = it.value();
auto lst = value.toList();
if(lst.size() == 1){
auto val = lst[0];
writeToJson(json, key, val);
}else{
QJsonArray arr;
for(auto a: lst){
QJsonObject obj;
writeToJson(obj, key, a);
arr.append(QJsonValue(obj));
}
json[key] = arr;
}
}
}
};
void Xml2JsonQt::parseXml(const string &input, const string &output)
{
Xml2JsonQtPrivate xml2json;
QDomDocument doc;
QFile file(QString::fromStdString(input));
if(!file.open(QIODevice::ReadOnly)){
cout << "File not open\n";
return;
}
QString errorMsg;
int errorLine, errorColumn;
if(!doc.setContent(&file, &errorMsg, &errorLine, &errorColumn)){
cout << errorMsg.toStdString() << ": "
<< errorLine << ", "
<< errorColumn << endl;
return;
}
file.close();
QMap<QString, QVariant> res;
xml2json.parseDom(doc, res);
if(res.empty())
return;
QJsonObject json;
xml2json.writeToJson(json, res);
if(!json.empty()){
QJsonDocument doc(json);
QFile out(QString::fromStdString(output));
out.open(QIODevice::WriteOnly);
out.write(doc.toJson(QJsonDocument::Indented));
out.close();
}
}
#endif