Fix parseDict()
[sfa.git] / sfa / util / rspec.py
index 1cd7edc..903365c 100644 (file)
@@ -77,7 +77,7 @@ class Rspec:
             dict[key]=[value]
         return dict
 
-    def toGenDict(self, nodeDom=None, parentdict={}, siblingdict={}, parent=None):
+    def toGenDict(self, nodeDom=None, parentdict=None, siblingdict={}, parent=None):
         """
         convert an XML to a nested dict:
           * Non-terminal nodes (elements with string children and attributes) are simple dictionaries
@@ -89,17 +89,32 @@ class Rspec:
 
         curNodeName = nodeDom.localName
 
-        if (nodeDom.nodeValue):
-            siblingdict = self.appendToDictOrCreate(siblingdict, parent, nodeDom.nodeValue)
-        elif (nodeDom.hasChildNodes()):
-            for child in nodeDom.childNodes:
-                 siblingdict = self.toGenDict(child, None, siblingdict,curNodeName)
-
+        if (nodeDom.hasChildNodes()):
+            childdict={}
             for attribute in nodeDom.attributes.keys():
-                parentdict = self.appendToDictOrCreate(parentdict, curNodeName, nodeDom.getAttribute(attribute))
+                childdict = self.appendToDictOrCreate(childdict, attribute, nodeDom.getAttribute(attribute))
+            for child in nodeDom.childNodes[:-1]:
+                if (child.nodeValue):
+                    siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, child.nodeValue)
+                else:
+                    childdict = self.toGenDict(child, None, childdict, curNodeName)
+
+            child = nodeDom.childNodes[-1]
+            if (child.nodeValue):
+                siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, child.nodeValue)
+                if (childdict):
+                    siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, childdict)
+            else:
+                siblingdict = self.toGenDict(child, siblingdict, childdict, curNodeName)
+        else:
+            childdict={}
+            for attribute in nodeDom.attributes.keys():
+                childdict = self.appendToDictOrCreate(childdict, attribute, nodeDom.getAttribute(attribute))
 
+            self.appendToDictOrCreate(siblingdict, curNodeName, childdict)
+            
         if (parentdict is not None):
-            parentdict = self.appendToDictOrCreate(parentdict, curNodeName, siblingdict)
+            parentdict = self.appendToDictOrCreate(parentdict, parent, siblingdict)
             return parentdict
         else:
             return siblingdict
@@ -168,7 +183,7 @@ class Rspec:
         """
         read an xml string and store it as a dom object.
         """
-        xml = xml.replace('\n', '').replace('\t', '').replace(' ', '').strip()
+        xml = xml.replace('\n', '').replace('\t', '').strip()
         dom = minidom.parseString(xml)
         self.rootNode = dom.childNodes[0]
 
@@ -259,7 +274,7 @@ class Rspec:
         """
         Convert a dictionary into a dom object and store it.
         """
-        self.rootNode = self.dict2dom(rdict, include_doc)
+        self.rootNode = self.dict2dom(rdict, include_doc).childNodes[0]
  
  
     def getDictsByTagName(self, tagname, dom = None):