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?
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
I suggest to prefer the newer xml.etree.ElementTree
standard module to the xml.dom.minidom
. Try the following:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
for element in tree.getiterator('item'):
print element.attrib
It prints
{'image': 'a', 'name': 'item1'}
{'image': 'b', 'name': 'item2'}
{'image': 'c', 'name': 'item3'}
{'image': 'd', 'name': 'item4'}
Here the .getiterator('item')
traverses all elements of the tree and returns the elements named item
. The .attrib
of each element is a dictionary of the element attributes -- this is exactly what you want.
Actually, the elements behave as lists of subelements. With the above attributes are items in the dictionary, the ElemenTree fits much better with Python than the DOM approach.
Add the following code to the above sample:
print '----------------'
root = tree.getroot()
ET.dump(root)
print '----------------'
print root.tag
print root.attrib
for elem in root:
print elem.tag, elem.attrib
It prints:
----------------
<main>
<item image="a" name="item1" />
<item image="b" name="item2" />
<item image="c" name="item3" />
<item image="d" name="item4" />
</main>
----------------
main
{}
item {'image': 'a', 'name': 'item1'}
item {'image': 'b', 'name': 'item2'}
item {'image': 'c', 'name': 'item3'}
item {'image': 'd', 'name': 'item4'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With