addiing some xml utility code
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 31 Aug 2005 19:02:01 +0000 (19:02 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 31 Aug 2005 19:02:01 +0000 (19:02 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1782 dcc99617-32d9-48b4-a31d-7c20da2025e4

OpenSRF/src/utils/xml_utils.c [new file with mode: 0644]
OpenSRF/src/utils/xml_utils.h [new file with mode: 0644]

diff --git a/OpenSRF/src/utils/xml_utils.c b/OpenSRF/src/utils/xml_utils.c
new file mode 100644 (file)
index 0000000..52071d4
--- /dev/null
@@ -0,0 +1,87 @@
+#include "xml_utils.h"
+
+
+void recurse_doc( xmlNodePtr node ) {
+       if( node == NULL ) return;
+       printf("Recurse: %s =>  %s", node->name, node->content );
+       xmlNodePtr t = node->children;
+       while(t) {
+               recurse_doc(t);
+               t = t->next;
+       }
+}
+
+
+
+jsonObject* xmlDocToJSON(xmlDocPtr doc) {
+       if(!doc) return NULL;
+       return _xmlToJSON(xmlDocGetRootElement(doc), NULL);
+}
+
+jsonObject* _xmlToJSON(xmlNodePtr node, jsonObject* obj) {
+
+       if(!node) return NULL;
+       if(xmlIsBlankNode(node)) return NULL;
+       if(obj == NULL) obj = jsonNewObject(NULL);
+
+       if(node->type == XML_TEXT_NODE) {
+               jsonObjectSetString(obj, (char*) node->content);        
+
+       } else if(node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE ) {
+
+               jsonObject* new_obj = jsonNewObject(NULL);
+
+               jsonObject* old;
+
+               /* do the duplicate node / array shuffle */
+               if( (old = jsonObjectGetKey(obj, (char*) node->name)) ) {
+                       if(old->type == JSON_ARRAY ) {
+                               jsonObjectPush(old, new_obj);
+                       } else {
+                               jsonObject* arr = jsonNewObject(NULL);
+                               jsonObjectPush(arr, jsonObjectClone(old));
+                               jsonObjectPush(arr, new_obj);
+                               jsonObjectSetKey(obj, (char*) node->name, arr);
+                       }
+               } else {
+                       jsonObjectSetKey(obj, (char*) node->name, new_obj);
+               }
+
+               xmlNodePtr child = node->children;
+               while(child) {
+                       _xmlToJSON(child, new_obj);
+                       child = child->next;
+               }       
+       }       
+
+       return obj;
+}
+
+
+char* xmlDocToString(xmlDocPtr doc, int full) {
+
+       if(!doc) return NULL;
+
+       char* xml;
+
+       if(full) {
+
+               xmlChar* xmlbuf;
+               int size;
+               xmlDocDumpMemory(doc, &xmlbuf, &size);
+               xml = strdup((char*) (xmlbuf));
+               xmlFree(xmlbuf);
+               return xml;
+
+       } else {
+
+               xmlBufferPtr xmlbuf = xmlBufferCreate();
+               xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
+               xml = strdup((char*) (xmlBufferContent(xmlbuf)));
+               xmlBufferFree(xmlbuf);
+               return xml;
+
+       }
+}
+
+
diff --git a/OpenSRF/src/utils/xml_utils.h b/OpenSRF/src/utils/xml_utils.h
new file mode 100644 (file)
index 0000000..3b6c936
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _XML_UTILS_H
+#define _XML_UTILS_H
+
+#include "objson/object.h"
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+jsonObject* xmlDocToJSON(xmlDocPtr doc);
+
+/* helper function */
+jsonObject* _xmlToJSON(xmlNodePtr node, jsonObject*);
+
+/* debug function, prints each node and content */
+void recurse_doc( xmlNodePtr node );
+
+
+/* turns an XML doc into a char*.  
+       User is responsible for freeing the returned char*
+       if(full), then we return the whole doc (xml declaration, etc.)
+       else we return the doc from the root node down
+       */
+char* xmlDocToString(xmlDocPtr doc, int full);
+
+#endif