From 475b3bc934f52d695dc8416324d0ddd7451bd491 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 31 Aug 2005 19:02:01 +0000 Subject: [PATCH] addiing some xml utility code git-svn-id: svn://svn.open-ils.org/ILS/trunk@1782 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- OpenSRF/src/utils/xml_utils.c | 87 +++++++++++++++++++++++++++++++++++ OpenSRF/src/utils/xml_utils.h | 24 ++++++++++ 2 files changed, 111 insertions(+) create mode 100644 OpenSRF/src/utils/xml_utils.c create mode 100644 OpenSRF/src/utils/xml_utils.h diff --git a/OpenSRF/src/utils/xml_utils.c b/OpenSRF/src/utils/xml_utils.c new file mode 100644 index 0000000000..52071d450c --- /dev/null +++ b/OpenSRF/src/utils/xml_utils.c @@ -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 index 0000000000..3b6c936e1b --- /dev/null +++ b/OpenSRF/src/utils/xml_utils.h @@ -0,0 +1,24 @@ +#ifndef _XML_UTILS_H +#define _XML_UTILS_H + +#include "objson/object.h" +#include +#include + +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 -- 2.43.2