code: def elementtree_to_dict(element):
      
      d = dict()
      if hasattr(element, 'text') and element.text is not None:
          d['text'] = element.text
  
      d.update(element.items())  # element's attributes
  
      for c in list(element):  # element's children
          if c.tag not in d:
              d[c.tag] = elementtree_to_dict(c)
          # an element with the same tag was already in the dict
          else:
              # if it's not a list already, convert it to a list and append
              if not isinstance(d[c.tag], list):
                  d[c.tag] = [d[c.tag], elementtree_to_dict(c)]
             # append to the list
              else:
                  d[c.tag].append(elementtree_to_dict(c))
      return d
docs: Convert an xml ElementTree to a dictionary.