Date created: Sunday, April 30, 2023 4:40:24 PM. Last modified: Tuesday, January 16, 2024 3:31:31 PM

XML-to-Dict

junos_xml_to_dit.py:

def xml_to_dict(xml):

   if len(xml) == 0:
       if xml.text:
           return xml.text.strip()
       else:
           return None
   else:
       return_dict = dict()
       for i in range(0, len(xml)):
           if return_dict.get(xml[i].tag, False):
               if isinstance(return_dict[xml[i].tag], list):
                   return_dict[xml[i].tag].append(xml_to_dict(xml[i]))
               else:
                   temp = return_dict[xml[i].tag]
                   return_dict[xml[i].tag] = []
                   return_dict[xml[i].tag].append(temp)
                   return_dict[xml[i].tag].append(xml_to_dict(xml[i]))
           else:
               return_dict[xml[i].tag] = xml_to_dict(xml[i])
       return return_dict

def junos_xml_to_python(device, response, **kwargs):
   return xml_to_dict(response)

 


Previous page: RegEx Examples
Next page: SQLite Notes