-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_analysis.py
76 lines (62 loc) · 2.1 KB
/
xml_analysis.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
from lxml import etree
def get_report():
report = """
<CompteRendu>
<MetaDonnees>
<DateSeance>12 oct 2029</DateSeance>
</MetaDonnees>
<Contenu>
<paragraphe>
<Orateur>
<NOM>Monsieur Gentil</NOM>
</Orateur>
<Texte>
Je suis gentil, et vous êtes méchant!
</Texte>
</paragraphe>
<paragraphe>
<Orateur>
<NOM>Monsieur Mechant</NOM>
</Orateur>
<Texte>
Je suis méchant oui, mais j'assume!
</Texte>
</paragraphe>
</Contenu>
<UneAutreBalise>
Blablabla 1
</UneAutreBalise>
<EncoreUneAutreBalise>
Blablabla 2
<br/>
Blablabla 3
<italic>
Blablabla 4
</italic>
</EncoreUneAutreBalise>
</CompteRendu>
"""
return report
def search():
report_str = get_report()
report = etree.XML(report_str)
# A report should always have 2 children:
# - a 'MetaDonnees' tag
# - a 'Contenu' tag
# In case it would contain more than these 2 children, we collect these
# other parts in the list *other_parts (which will normally always be an empty list)
metadata, content, *other_parts = report
print("Date : {}".format(metadata.find("DateSeance").text))
for paragraph in content.iterchildren():
speaker = paragraph[0]
text = paragraph[1]
print("-" * 80)
print("New paragraph. Speaker : {}".format(speaker.find("NOM").text))
print(text.text)
print("-" * 80)
for other_part in other_parts:
print("Other parts were found :")
for text in other_part.itertext():
print(text)
if __name__ == '__main__':
search()