-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2jsonlibxml.cpp
248 lines (206 loc) · 6.6 KB
/
xml2jsonlibxml.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "xml2jsonlibxml.h"
#include <iostream>
#include <queue>
#include <list>
#include <thread>
#include <functional>
#include <stack>
#include <fstream>
#include <mutex>
#if !defined(WITH_QT) && !defined(WITH_POCO)
#include <libxml2/libxml/xmlreader.h>
#include <jsoncpp/json/json.h>
#endif
#include "common.h"
using namespace std;
#if !defined(WITH_QT) && !defined(WITH_POCO)
class Xml2JsonLibxmlPrivate{
public:
queue< Val > mQueue;
bool mDone = false;
int mCurrentDepth = 0;
mutex mMutex;
void processNode(xmlNodePtr node, vmap& params, int depth = 0, const std::string& parent = ""){
string key, value, attrName, attrValue;
vmap attrs;
for(auto it = node; it; it = it->next){
if(it->type == XML_ELEMENT_NODE){
key = reinterpret_cast<const char*>(it->name);
xmlChar* xval = xmlNodeListGetString(it->doc, it->children, 1);
value = reinterpret_cast<const char*>(xval);
xmlFree(xval);
trim(value); // remove \n or \r
if(!value.empty()){
#ifdef TEST
params[key] = value;
#endif
lock_guard<mutex> lock(mMutex);
mQueue.push({key, value, depth, Val::NOCHANGE});
}
xmlAttr* attribute = it->properties;
while(attribute)
{
attrName = reinterpret_cast<const char*>(attribute->name);
xmlChar* value = xmlNodeListGetString(it->doc, attribute->children, 1);
attrValue= reinterpret_cast<const char*>(value);
//do something with value
xmlFree(value);
attribute = attribute->next;
attrs[attrName] = attrValue;
}
}
if(it->children && value.empty()){
{
lock_guard<mutex> lock(mMutex);
mQueue.push({key, "", depth, Val::IN}); // go deeper
}
vmap params2;
auto lst = params[key].toList();
processNode(it->children, params2, depth + 1, key);
#ifdef TEST
if(!attrs.empty()){
params2.insert(attrs.begin(), attrs.end());
}
if(depth != 0){
if(!params2.empty())
lst.push_back(params2);
if(!lst.empty()){
params[key] = lst;
}
}else{
params[key] = params2;
}
#endif
{
lock_guard<mutex> lock(mMutex);
mQueue.push({key, "", depth, Val::OUT, attrs}); // out of children node
}
}
}
}
void writeJson(const string& output){
Json::Value root;
#ifdef TEST
stack <vmap> _stack;
_stack.push(vmap());
#endif
stack < Json::Value> jstack;
jstack.push(Json::Value());
while(!mDone || !mQueue.empty()){
if(mQueue.empty()){
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
mMutex.lock();
auto val = mQueue.front();
mQueue.pop();
mMutex.unlock();
if(val.state == Val::IN){
#ifdef TEST
vmap m;
_stack.push(m);
#endif
Json::Value j;
jstack.push(j);
}
if(val.state == Val::OUT){
#ifdef TEST
/// test code
vmap t = _stack.top();
_stack.pop();
vmap& tn = _stack.top();
if(!Val.attrs.empty()){
t.insert(Val.attrs.begin(), Val.attrs.end());
}
if(contains(tn, Val.key) && !t.empty()){
auto obj = tn[Val.key];
vlist lst;
lst.push_back(obj);
lst.push_back(t);
tn[Val.key] = lst;
}else{
tn[Val.key] = t;
}
/// end test code
#endif
auto jt = jstack.top();
jstack.pop();
auto& jtn = jstack.top();
if(!val.attrs.empty()){
for(auto it: val.attrs){
jt[it.first] = it.second.toText();
}
}
if(!jtn[val.key]){
jtn[val.key] = jt;
}else{
// if key exists then need to create array
auto obj = jtn[val.key];
if(obj.isArray()){
obj[obj.size()] = jt;
}else{
Json::Value arr;
arr[0].copy(obj);
arr[1] = jt;
obj = arr;
}
jtn[val.key] = obj;
}
}
if(!val.value.empty()){
#ifdef TEST
vmap& t = _stack.top();
t[Val.key] = Val.value;
#endif
auto& jt = jstack.top();
jt[val.key] = val.value;
}else{
}
// printf("%d: %s, %s, %s\n", Val.depth, Val.key.c_str(), Val.value.c_str(), parent.c_str());
}
root = jstack.top();
std::ofstream file_id;
file_id.open(output);
Json::StyledWriter writer;
file_id << writer.write(root);
file_id.close();
printf("\n");
}
void parseXmlThr(const std::string &input, const std::string &output)
{
xmlInitParser();
auto doc = xmlParseFile(input.c_str());
if(!doc){
cout << "can not parse file\n";
return;
}
auto root_element = xmlDocGetRootElement(doc);
vmap params;
processNode(root_element, params);
xmlFreeDoc(doc);
xmlCleanupParser();
mDone = true;
}
void parseXml(const std::string &input, const std::string &output)
{
thread parser([this, input, output](){
parseXmlThr(input, output);
});
thread writer([this, output](){
writeJson(output);
});
parser.join();
writer.join();
}
};
#endif
Xml2JsonLibxml::Xml2JsonLibxml()
{
}
void Xml2JsonLibxml::parseXml(const std::string &input, const std::string &output)
{
#if !defined(WITH_QT) && !defined(WITH_POCO)
Xml2JsonLibxmlPrivate priv;
priv.parseXml(input, output);
#endif
}