Get all Attributes XML in python and Make it into a dictionary
dom, python, xml
Solution
The following code will create the dictionaries (no additional libraries are needed):
dicts = []
for item in itemlist:
d = {}
for a in item.attributes.values():
d[a.name] = a.value
dicts.append(d)
print dicts
Problem
XML: ``` <main> <item name="item1" image="a"></item> <item name="item2" image="b"></item> <item name="item3" image="c"></item> <item name="item4" image="d"></item> </main> ``` Python: ``` xmldoc = minidom.parse('blah.xml') itemlist = xmldoc.getElementsByTagName('item') for item in itemlist : #####I want to make a dictionary of each item ``` So I would get ``` {'name':'item1','image':'a'} {'name':'item2','image':'b'} {'name':'item3','image':'c'} {'name':'item4','image':'d'} ``` Does anyone know how to do this? Is there a function?