Welcome, guest | Sign In | My Account | Store | Cart
from xml.dom.minidom import Document
import copy

class dict2xml(object):
    doc     = Document()

    def __init__(self, structure):
        if len(structure) == 1:
            rootName    = str(structure.keys()[0])
            self.root   = self.doc.createElement(rootName)

            self.doc.appendChild(self.root)
            self.build(self.root, structure[rootName])

    def build(self, father, structure):
        if type(structure) == dict:
            for k in structure:
                tag = self.doc.createElement(k)
                father.appendChild(tag)
                self.build(tag, structure[k])
        
        elif type(structure) == list:
            grandFather = father.parentNode
            tagName     = father.tagName
            grandFather.removeChild(father)
            for l in structure:
                tag = self.doc.createElement(tagName)
                self.build(tag, l)
                grandFather.appendChild(tag)
            
        else:
            data    = str(structure)
            tag     = self.doc.createTextNode(data)
            father.appendChild(tag)
    
    def display(self):
        print self.doc.toprettyxml(indent="  ")

if __name__ == '__main__':
    
    example = {'sibbling':{'couple':{'mother':'mom','father':'dad','children':[{'child':'foo'},
                                                          {'child':'bar'}]}}}
    xml = dict2xml(example)
    xml.display()

Diff to Previous Revision

--- revision 3 2011-06-07 15:42:14
+++ revision 4 2011-07-19 12:32:57
@@ -25,7 +25,7 @@
             grandFather.removeChild(father)
             for l in structure:
                 tag = self.doc.createElement(tagName)
-                self.__build(tag, l)
+                self.build(tag, l)
                 grandFather.appendChild(tag)
             
         else:
@@ -37,31 +37,8 @@
         print self.doc.toprettyxml(indent="  ")
 
 if __name__ == '__main__':
+    
     example = {'sibbling':{'couple':{'mother':'mom','father':'dad','children':[{'child':'foo'},
                                                           {'child':'bar'}]}}}
+    xml = dict2xml(example)
     xml.display()
-
-'''
->>>
-<?xml version="1.0" ?>
-<sibbling>
-  <couple>
-    <father>
-      dad
-    </father>
-    <children>
-      <child>
-        foo
-      </child>
-    </children>
-    <children>
-      <child>
-        bar
-      </child>
-    </children>
-    <mother>
-      mom
-    </mother>
-  </couple>
-</sibbling>
-'''

History