-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathxml_reader.py
More file actions
33 lines (23 loc) · 868 Bytes
/
xml_reader.py
File metadata and controls
33 lines (23 loc) · 868 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
30
31
32
33
import xml.dom.minidom as minidom
xml_file = open('books.xml', 'r')
xml_data = xml_file.read()
dom = minidom.parseString(xml_data)
dom.normalize()
elements = dom.getElementsByTagName('book')
books_dict = {}
for node in elements:
for child in node.childNodes:
if child.nodeType == 1:
if child.tagName == 'title':
if child.firstChild.nodeType == 3:
title = child.firstChild.data
if child.tagName == 'price':
if child.firstChild.nodeType == 3:
price = float(child.firstChild.data)
books_dict[title] = price
if node.getAttribute('id') == 'bk106':
print(node.getElementsByTagName('title')[0].firstChild.data)
#for key in books_dict.keys():
#print(key, books_dict[key])
print(books_dict)
xml_file.close()