-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2json.h
103 lines (80 loc) · 2.21 KB
/
xml2json.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
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <json/json.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
json_object * jobjs[200];
json_object *jarray;
int arrayFlag;
static void createJsonFromXml(xmlNode * a_node, json_object * jobj);
static void convertXmltoJson(char *in, char *out)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
const char *Filename = in;
doc = xmlReadFile(in, NULL, XML_PARSE_NOBLANKS);
if (doc == NULL)
{
printf("error: could not parse file %s\n", Filename);
}
else
{
/*Creating a json object*/
json_object * jobj = json_object_new_object();
jarray = json_object_new_array();
arrayFlag = 0;
/* Get the root element node*/
root_element = xmlDocGetRootElement(doc);
createJsonFromXml(root_element, jobj);
//printf ("The json object created: %s\n",json_object_to_json_string(jobj));
FILE *fpout = fopen(out,"w");
fprintf(fpout, "%s", json_object_to_json_string(jobj));
fclose(fpout);
/*free the document*/
xmlFreeDoc(doc);
}
/*Free the global variables that may have been allocated by the parser.*/
xmlCleanupParser();
}
/* Recursive function that prints the XML structure */
static void createJsonFromXml(xmlNode * a_node, json_object * jobj)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE && cur_node->children->content != NULL)
{
json_object *jstring1 = json_object_new_string(cur_node->children->content);
json_object_object_add(jobj, cur_node->name, jstring1);
}
else if(cur_node->type == XML_ELEMENT_NODE && cur_node->children->content == NULL)
{
int i = 0;
int flag = 0;
while(jobjs[i])
{
i++;
}
jobjs[i] = json_object_new_object();
if(cur_node->type == XML_ELEMENT_NODE && cur_node->children->children->content != NULL)
{
flag = 1;
if(arrayFlag == 0)
{
arrayFlag = 1;
json_object_object_add(jobj, cur_node->name, jarray);
}
}
createJsonFromXml(cur_node->children, jobjs[i]);
if(flag == 1)
{
json_object_array_add(jarray, jobjs[i]);
}
else
json_object_object_add(jobj, cur_node->name, jobjs[i]);
i=0;
}
}
}