From: Sapan Bhatia Date: Sat, 11 Jul 2009 21:03:15 +0000 (+0000) Subject: Backing out my XML parser. Will remerge later today. X-Git-Tag: sfa-0.9-0@14641~74 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=85e92f0dc03488adbb5603f9b3c1149fedd20161;p=sfa.git Backing out my XML parser. Will remerge later today. --- diff --git a/sfa/util/rspec.py b/sfa/util/rspec.py index 55fa44e5..1b05803f 100644 --- a/sfa/util/rspec.py +++ b/sfa/util/rspec.py @@ -77,7 +77,7 @@ class Rspec: dict[key]=[value] return dict - def toDict(self, nodeDom=None, parentdict=None, 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 @@ -97,7 +97,7 @@ class Rspec: if (child.nodeValue): siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, child.nodeValue) else: - childdict = self.toDict(child, None, childdict, curNodeName) + childdict = self.toGenDict(child, None, childdict, curNodeName) child = nodeDom.childNodes[-1] if (child.nodeValue): @@ -105,7 +105,7 @@ class Rspec: if (childdict): siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, childdict) else: - siblingdict = self.toDict(child, siblingdict, childdict, curNodeName) + siblingdict = self.toGenDict(child, siblingdict, childdict, curNodeName) else: childdict={} for attribute in nodeDom.attributes.keys(): @@ -121,40 +121,40 @@ class Rspec: -# def toDict(self, nodeDom = None): -# """ -# convert this rspec to a dict and return it. -# """ -# node = {} -# if not nodeDom: -# nodeDom = self.rootNode -# -# elementName = nodeDom.nodeName -# if elementName and not elementName.startswith("#"): -# # attributes have tags and values. get {tag: value}, else {type: value} -# node[elementName] = self._attributeDict(nodeDom) -# # resolve the child nodes. -# if nodeDom.hasChildNodes(): -# for child in nodeDom.childNodes: -# childName = self._getName(child) -# # skip null children -# if not childName: -# continue -# # initialize the possible array of children -# if not node[elementName].has_key(childName): -# node[elementName][childName] = [] -# # if child node has text child nodes -# # append the children to the array as strings -# if child.hasChildNodes() and isinstance(child.childNodes[0], minidom.Text): -# for nextchild in child.childNodes: -# node[elementName][childName].append(nextchild.data) -# # convert element child node to dict -# else: -# childdict = self.toDict(child) -# for value in childdict.values(): -# node[elementName][childName].append(value) -# #node[childName].append(self.toDict(child)) -# return node + def toDict(self, nodeDom = None): + """ + convert this rspec to a dict and return it. + """ + node = {} + if not nodeDom: + nodeDom = self.rootNode + + elementName = nodeDom.nodeName + if elementName and not elementName.startswith("#"): + # attributes have tags and values. get {tag: value}, else {type: value} + node[elementName] = self._attributeDict(nodeDom) + # resolve the child nodes. + if nodeDom.hasChildNodes(): + for child in nodeDom.childNodes: + childName = self._getName(child) + # skip null children + if not childName: + continue + # initialize the possible array of children + if not node[elementName].has_key(childName): + node[elementName][childName] = [] + # if child node has text child nodes + # append the children to the array as strings + if child.hasChildNodes() and isinstance(child.childNodes[0], minidom.Text): + for nextchild in child.childNodes: + node[elementName][childName].append(nextchild.data) + # convert element child node to dict + else: + childdict = self.toDict(child) + for value in childdict.values(): + node[elementName][childName].append(value) + #node[childName].append(self.toDict(child)) + return node def toxml(self):