-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xml_reader_minidom_vs_et.py
69 lines (50 loc) · 1.54 KB
/
test_xml_reader_minidom_vs_et.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from xml.dom import minidom
import xml.etree.ElementTree as ET
import utils
"""
Test Minidom versus Element Tree to read .xml file
Chosen: minidom
Source: http://stackabuse.com/reading-and-writing-xml-files-in-python/
"""
def test_minidom(filename):
# parse an xml file by name
mydoc = minidom.parse(filename)
items = mydoc.getElementsByTagName('item')
# one specific item attribute
print('Item #2 attribute:')
print(items[1].attributes['name'].value)
# all item attributes
print('\nAll attributes:')
for elem in items:
print(elem.attributes['name'].value)
# one specific item's data
print('\nItem #2 data:')
print(items[1].firstChild.data)
print(items[1].childNodes[0].data)
# all items data
print('\nAll item data:')
for elem in items:
print(elem.firstChild.data)
def test_element_tree(filename):
tree = ET.parse(filename)
root = tree.getroot()
# one specific item attribute
print('Item #2 attribute:')
print(root[0][1].attrib)
# all item attributes
print('\nAll attributes:')
for elem in root:
for subelem in elem:
print(subelem.attrib)
# one specific item's data
print('\nItem #2 data:')
print(root[0][1].text)
# all items data
print('\nAll item data:')
for elem in root:
for subelem in elem:
print(subelem.text)
if __name__ == '__main__':
filename = utils.project_dir_name() + 'data/test.xml'
# test_minidom(filename)
test_element_tree(filename)