Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Attributes XML in python and Make it into a dictionary

Tags:

python

dom

xml

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?

like image 754
user1513192 Avatar asked Jul 14 '12 16:07

user1513192


2 Answers

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
like image 55
Simeon Visser Avatar answered Nov 10 '22 15:11

Simeon Visser


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'}
like image 23
pepr Avatar answered Nov 10 '22 14:11

pepr