1 #include <opensrf/xml_utils.h>
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;
16 jsonObject* xmlDocToJSON(xmlDocPtr doc) {
18 return _xmlToJSON(xmlDocGetRootElement(doc), NULL);
21 jsonObject* _xmlToJSON(xmlNodePtr node, jsonObject* obj) {
23 if(!node) return NULL;
24 if(xmlIsBlankNode(node)) return NULL;
25 if(obj == NULL) obj = jsonNewObject(NULL);
27 if(node->type == XML_TEXT_NODE) {
28 jsonObjectSetString(obj, (char*) node->content);
30 } else if(node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE ) {
32 jsonObject* new_obj = jsonNewObject(NULL);
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);
41 jsonObject* arr = jsonNewObject(NULL);
42 jsonObjectPush(arr, jsonObjectClone(old));
43 jsonObjectPush(arr, new_obj);
44 jsonObjectSetKey(obj, (char*) node->name, arr);
47 jsonObjectSetKey(obj, (char*) node->name, new_obj);
50 xmlNodePtr child = node->children;
51 if (child) { // at least one...
52 if (child != node->last) { // more than one -- ignore TEXT nodes
54 if (child->type != XML_TEXT_NODE) _xmlToJSON(child, new_obj);
58 _xmlToJSON(child, new_obj);
67 char* xmlDocToString(xmlDocPtr doc, int full) {
77 xmlDocDumpMemory(doc, &xmlbuf, &size);
78 xml = strdup((char*) (xmlbuf));
84 xmlBufferPtr xmlbuf = xmlBufferCreate();
85 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
86 xml = strdup((char*) (xmlBufferContent(xmlbuf)));
87 xmlBufferFree(xmlbuf);
96 char* xmlSaxAttr( const xmlChar** atts, char* name ) {
99 for(i = 0; (atts[i] != NULL); i++) {
100 if(!strcmp((char*) atts[i], name)) {
101 if(atts[++i]) return (char*) atts[i];
109 int xmlAddAttrs( xmlNodePtr node, const xmlChar** atts ) {
112 for(i = 0; (atts[i] != NULL); i++) {
114 xmlSetProp(node, atts[i], atts[i+1]);