PageRenderTime 358ms CodeModel.GetById 108ms app.highlight 5ms RepoModel.GetById 141ms app.codeStats 1ms

/xml2json.c

http://github.com/fizx/parsley
C | 47 lines | 37 code | 4 blank | 6 comment | 13 complexity | dab46e52e7dccd55774f77e2034cf241 MD5 | raw file
 1#include "xml2json.h"
 2
 3static struct json_object * _xml2json(xmlNodePtr xml) {
 4  if(xml == NULL) return NULL;
 5  
 6  xmlNodePtr child;
 7  struct json_object * json = NULL;
 8  
 9  switch(xml->type) {
10    case XML_ELEMENT_NODE:
11      child = xml->children;
12      if(xml->ns == NULL) {
13        child = xml;
14        // json_object_put(json);
15        json = json_object_new_object();
16        while(child != NULL) {
17          json_object_object_add(json, child->name, xml2json(child->children));
18          child = child->next;
19        }
20      } else if(!strcmp(xml->ns->prefix, "parsley")) {
21        if(!strcmp(xml->name, "groups")) {
22          // json_object_put(json);
23          json = json_object_new_array();          
24          while(child != NULL) {
25            json_object_array_add(json, xml2json(child->children));
26            child = child->next;
27          }          
28        } else if(!strcmp(xml->name, "group")) {
29          // Implicitly handled by parsley:groups handler
30        }
31      }
32      break;
33    case XML_TEXT_NODE:  
34      json = json_object_new_string(xml->content);
35      break;
36  }
37  return json;
38}
39
40/**
41 * Handles a simplified xml
42 */
43struct json_object * xml2json(xmlNodePtr xml) {
44	struct json_object * json = _xml2json(xml);
45	if(json == NULL) json = json_object_new_object();
46	return json;
47}