Get all Attributes XML in python and Make it into a dictionary
13,978
Solution 1
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
Solution 2
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'}
Solution 3
Using this Python recipe:
from xml2obj import xml2obj
data = xml2obj(s)['item']
# data content:
>>> [{image:u'a', name:u'item1'},
>>> {image:u'b', name:u'item2'},
>>> {image:u'c', name:u'item3'},
>>> {image:u'd', name:u'item4'}]
Author by
user1513192
Updated on June 21, 2022Comments
-
user1513192 about 2 years
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?