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;
52 _xmlToJSON(child, new_obj);
61 char* xmlDocToString(xmlDocPtr doc, int full) {
71 xmlDocDumpMemory(doc, &xmlbuf, &size);
72 xml = strdup((char*) (xmlbuf));
78 xmlBufferPtr xmlbuf = xmlBufferCreate();
79 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
80 xml = strdup((char*) (xmlBufferContent(xmlbuf)));
81 xmlBufferFree(xmlbuf);
90 char* xmlSaxAttr( const xmlChar** atts, char* name ) {
93 for(i = 0; (atts[i] != NULL); i++) {
94 if(!strcmp(atts[i], name)) {
95 if(atts[++i]) return (char*) atts[i];
103 int xmlAddAttrs( xmlNodePtr node, const xmlChar** atts ) {
106 for(i = 0; (atts[i] != NULL); i++) {
108 xmlSetProp(node, atts[i], atts[i+1]);