106def _xml2dict_recurse (node, dictclass):
107 nodedict = dictclass()
108
109 if len(node.items()) > 0:
110
111 nodedict.update(dict((k, _xml_unescape(v) if isinstance(v, str) else v)
112 for k,v in node.items()))
113
114 for child in node:
115
116 newitem = _xml2dict_recurse (child, dictclass)
117 if isinstance(newitem, str):
118 newitem = _xml_unescape(newitem)
119 if child.tag in nodedict:
120
121 if isinstance(nodedict[child.tag], list):
122
123 nodedict[child.tag].append(newitem)
124 else:
125
126 nodedict[child.tag] = [nodedict[child.tag], newitem]
127 else:
128
129 nodedict[child.tag] = newitem
130
131 if node.text is None:
132 text = ''
133 else:
134 text = node.text.strip()
135
136 if len(nodedict) > 0:
137
138
139 if len(text) > 0:
140 nodedict['_text'] = text
141 else:
142
143 if node.text: nodedict = node.text.strip()
144 else: nodedict = ""
145
146 return nodedict
147