-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parser.py
78 lines (63 loc) · 2.11 KB
/
xml_parser.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
70
71
72
73
74
75
76
77
78
# encoding: utf-8
# module xml_parser
"""
XML parser for question.xml
"""
import xml.dom.minidom
from xml.dom.minidom import Node
def parse(path):
"""
Return special structure that can describe XML document.
Uses DOM parser.
"""
doc = xml.dom.minidom.parse(path)
parsed = {}
for item_tag in doc.getElementsByTagName("item"): # обход объекта DOM
# <item id type>
item = {}
item_id = item_tag.getAttribute("id")
item_type = item_tag.getAttribute("type")
item["id"] = item_id
item["type"] = item_type
# <question>
question_tag = item_tag.getElementsByTagName("question")
question = {}
# <text>
text_tag = question_tag[0].getElementsByTagName("text")
question_text = text_tag[0].childNodes[0].data
question["text"] = question_text
# <variants>
variants_tag = question_tag[0].getElementsByTagName("variants")
variants = {}
for variant_tag in variants_tag[0].getElementsByTagName("variant"):
# <variant id>
variant_id = variant_tag.getAttribute("id")
variants[variant_id] = variant_tag.childNodes[0].data
question["variants"] = variants
item["question"] = question
# <answers>
answers_tag = item_tag.getElementsByTagName("answers")
answers = {}
for answer_tag in answers_tag[0].getElementsByTagName("answer"):
# <answer id>
answer_id = answer_tag.getAttribute("id")
answers[answer_id] = answer_tag.childNodes[0].data
item["answers"] = answers
parsed[item_id] = item
return parsed
if __name__ == '__main__':
"""
Sandbox for testing the module.
"""
path = 'resourses/questions.xml'
parsed = parse(path)
for key, value in parsed.items():
# print("item id :", key)
for key2, value2 in value.items():
print("-" * 50)
print(key2)
print(value2)
print('=' * 50)
# return parsed
# for key, value in parsed.items():
# print(key, value)