-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2xml.h
160 lines (131 loc) · 4.16 KB
/
json2xml.h
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
#include <json/json.h>
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
void lastOp(json_object *jobj, char* key, xmlNodePtr root, int compressMode){
enum json_type type;
type = json_object_get_type(jobj); /*Getting the type of the json object*/
switch (type) {
case json_type_boolean:
break;
case json_type_double:
if(compressMode == 1)
{
xmlNewProp(root, BAD_CAST key, BAD_CAST json_object_get_string(jobj));
}
else
{
xmlNodePtr node1 = xmlNewChild(root, NULL, key, BAD_CAST json_object_get_string(jobj));
}
break;
case json_type_int:
if(compressMode == 1)
{
xmlNewProp(root, BAD_CAST key, BAD_CAST json_object_get_string(jobj));
}
else
{
xmlNodePtr node1 = xmlNewChild(root, NULL, key, BAD_CAST json_object_get_string(jobj));
}
break;
case json_type_string:
if(compressMode == 1)
{
xmlNewProp(root, BAD_CAST key, BAD_CAST json_object_get_string(jobj));
}
else
{
xmlNodePtr node1 = xmlNewChild(root, NULL, key, BAD_CAST json_object_get_string(jobj));
}
break;
}
}
void parse_array(json_object *jobj, char *key, xmlNodePtr root, int compressMode) {
void jsonParse(json_object * jobj, xmlNodePtr root, int compressMode);
enum json_type type;
json_object *jarray = jobj; /*Simply get the array*/
if(key) {
jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
}
int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
int i;
json_object * jvalue;
for (i=0; i< arraylen; i++){
jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
xmlNodePtr node = xmlNewChild(root, NULL, key, BAD_CAST NULL);
type = json_object_get_type(jvalue);
if (type == json_type_array) {
parse_array(jvalue, NULL, node, compressMode);
}
else if (type != json_type_object) {
lastOp(jvalue, key, node, compressMode);
}
else {
jsonParse(jvalue, node, compressMode);
}
}
}
/*Parsing the json object*/
void jsonParse(json_object * jobj, xmlNodePtr root, int compressMode) {
enum json_type type;
json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
type = json_object_get_type(val);
xmlNodePtr node;
switch (type) {
case json_type_boolean:
break;
case json_type_double:
break;
case json_type_int:
lastOp(val, key, root, compressMode);
break;
case json_type_string:
lastOp(val, key, root, compressMode);
break;
case json_type_object:
node = xmlNewChild(root, NULL, key, BAD_CAST NULL);
jobj = json_object_object_get(jobj, key);
jsonParse(jobj, node, compressMode);
break;
case json_type_array:
parse_array(jobj, key, root, compressMode);
break;
}
}
}
static void convertJsontoXml(char *in, char *out, int compressMode) {
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL;/* node pointers */
xmlDtdPtr dtd = NULL; /* DTD pointer */
char buff[2000];
/*
* Creates a new document, a node and set it as a root node
*/
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
char* data[2000];
char* token;
char buffer[2000];
FILE *fp = fopen(in,"r");
int i=0;
while (fgets(buffer, 2000, fp))
{
token = buffer;
data[i] = malloc(strlen(token));
strcpy(data[i],token);
json_object * jobj = json_tokener_parse(data[i]);
jsonParse(jobj, root_node, compressMode);
i++;
}
xmlSaveFormatFileEnc(out, doc, "UTF-8", 1);
/*free the document */
xmlFreeDoc(doc);
/*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser();
xmlMemoryDump();
}