From 0d9fa514c44016db7e73955e49bf4ea7f116ebd2 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/OpenSRF/trunk@513 9efc2488-bf62-4759-914b-345cdb29e865 --- src/utils/xml_utils.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils/xml_utils.h | 24 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/utils/xml_utils.c create mode 100644 src/utils/xml_utils.h diff --git a/src/utils/xml_utils.c b/src/utils/xml_utils.c new file mode 100644 index 0000000..52071d4 --- /dev/null +++ b/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/src/utils/xml_utils.h b/src/utils/xml_utils.h new file mode 100644 index 0000000..3b6c936 --- /dev/null +++ b/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.11.0