1from xml.dom import minidom
2
3# parse an xml file by name
4mydoc = minidom.parse('items.xml')
5
6items = mydoc.getElementsByTagName('item')
7
8# one specific item attribute
9print('Item #2 attribute:')
10print(items[1].attributes['name'].value)
11
12# all item attributes
13print('\nAll attributes:')
14for elem in items:
15 print(elem.attributes['name'].value)
16
17# one specific item's data
18print('\nItem #2 data:')
19print(items[1].firstChild.data)
20print(items[1].childNodes[0].data)
21
22# all items data
23print('\nAll item data:')
24for elem in items:
25 print(elem.firstChild.data)
1import xml.etree.ElementTree as ET
2
3root = ET.fromstring(country_data_as_string)
1import xml.etree.ElementTree as ET
2
3tree = ET.parse('filename.xml') #this gets the file into a tree structure
4tree_root = tree.getroot() #this gives us the root element of the file