-
Notifications
You must be signed in to change notification settings - Fork 38
/
xml2json.c
47 lines (43 loc) · 1.28 KB
/
xml2json.c
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
#include "xml2json.h"
static struct json_object * _xml2json(xmlNodePtr xml) {
if(xml == NULL) return NULL;
xmlNodePtr child;
struct json_object * json = NULL;
switch(xml->type) {
case XML_ELEMENT_NODE:
child = xml->children;
if(xml->ns == NULL) {
child = xml;
// json_object_put(json);
json = json_object_new_object();
while(child != NULL) {
json_object_object_add(json, child->name, xml2json(child->children));
child = child->next;
}
} else if(!strcmp(xml->ns->prefix, "parsley")) {
if(!strcmp(xml->name, "groups")) {
// json_object_put(json);
json = json_object_new_array();
while(child != NULL) {
json_object_array_add(json, xml2json(child->children));
child = child->next;
}
} else if(!strcmp(xml->name, "group")) {
// Implicitly handled by parsley:groups handler
}
}
break;
case XML_TEXT_NODE:
json = json_object_new_string(xml->content);
break;
}
return json;
}
/**
* Handles a simplified xml
*/
struct json_object * xml2json(xmlNodePtr xml) {
struct json_object * json = _xml2json(xml);
if(json == NULL) json = json_object_new_object();
return json;
}