]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/xml_utils.c
moving to UNIVERSAL::require to suck in implementation modules
[Evergreen.git] / OpenSRF / src / utils / xml_utils.c
1 #include "xml_utils.h"
2
3
4 void recurse_doc( xmlNodePtr node ) {
5         if( node == NULL ) return;
6         printf("Recurse: %s =>  %s", node->name, node->content );
7         xmlNodePtr t = node->children;
8         while(t) {
9                 recurse_doc(t);
10                 t = t->next;
11         }
12 }
13
14
15
16 jsonObject* xmlDocToJSON(xmlDocPtr doc) {
17         if(!doc) return NULL;
18         return _xmlToJSON(xmlDocGetRootElement(doc), NULL);
19 }
20
21 jsonObject* _xmlToJSON(xmlNodePtr node, jsonObject* obj) {
22
23         if(!node) return NULL;
24         if(xmlIsBlankNode(node)) return NULL;
25         if(obj == NULL) obj = jsonNewObject(NULL);
26
27         if(node->type == XML_TEXT_NODE) {
28                 jsonObjectSetString(obj, (char*) node->content);        
29
30         } else if(node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE ) {
31
32                 jsonObject* new_obj = jsonNewObject(NULL);
33
34                 jsonObject* old;
35
36                 /* do the duplicate node / array shuffle */
37                 if( (old = jsonObjectGetKey(obj, (char*) node->name)) ) {
38                         if(old->type == JSON_ARRAY ) {
39                                 jsonObjectPush(old, new_obj);
40                         } else {
41                                 jsonObject* arr = jsonNewObject(NULL);
42                                 jsonObjectPush(arr, jsonObjectClone(old));
43                                 jsonObjectPush(arr, new_obj);
44                                 jsonObjectSetKey(obj, (char*) node->name, arr);
45                         }
46                 } else {
47                         jsonObjectSetKey(obj, (char*) node->name, new_obj);
48                 }
49
50                 xmlNodePtr child = node->children;
51                 while(child) {
52                         _xmlToJSON(child, new_obj);
53                         child = child->next;
54                 }       
55         }       
56
57         return obj;
58 }
59
60
61 char* xmlDocToString(xmlDocPtr doc, int full) {
62
63         if(!doc) return NULL;
64
65         char* xml;
66
67         if(full) {
68
69                 xmlChar* xmlbuf;
70                 int size;
71                 xmlDocDumpMemory(doc, &xmlbuf, &size);
72                 xml = strdup((char*) (xmlbuf));
73                 xmlFree(xmlbuf);
74                 return xml;
75
76         } else {
77
78                 xmlBufferPtr xmlbuf = xmlBufferCreate();
79                 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
80                 xml = strdup((char*) (xmlBufferContent(xmlbuf)));
81                 xmlBufferFree(xmlbuf);
82                 return xml;
83
84         }
85 }
86
87
88
89
90 char* xmlSaxAttr( const xmlChar** atts, char* name ) {
91         if( atts && name ) {
92                 int i;
93                 for(i = 0; (atts[i] != NULL); i++) {
94                         if(!strcmp(atts[i], name)) {
95                                 if(atts[++i]) return (char*) atts[i];
96                         }
97                 }
98         }
99         return NULL;
100 }
101
102
103 int xmlAddAttrs( xmlNodePtr node, const xmlChar** atts ) {
104         if( node && atts ) {
105                 int i;
106                 for(i = 0; (atts[i] != NULL); i++) {
107                         if(atts[i+1]) {
108                                 xmlSetProp(node, atts[i], atts[i+1]);
109                                 i++;
110                         }
111                 }
112         }
113         return 0;
114 }
115