-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_tree.py
More file actions
executable file
·29 lines (23 loc) · 986 Bytes
/
xml_tree.py
File metadata and controls
executable file
·29 lines (23 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import xml.etree.ElementTree as ET
base_dir = os.path.abspath(os.path.dirname(__file__))
filename_path = os.path.join(base_dir, 'sample.xml')
# or ET.fromstring(string_with_xml), if xml is stored in string with xml
tree = ET.parse(filename_path)
root = tree.getroot()
countries = list(root)
directions = {'E': 'East', 'W': 'West', 'N': 'North', 'S': 'South'}
for country in countries:
country_name = country.attrib.get('name')
print(country_name)
country_child_elements = list(country.iter())
# remove first element because it's the root element [country] itself
country_child_elements = country_child_elements[1:]
for child_element in country_child_elements:
if child_element.tag != 'neighbor':
continue
print(' {} is in the {} of {}'.format(
child_element.attrib.get('name'),
directions.get(child_element.attrib.get('direction'), 'unknown'),
country_name))
print('-' * 50)