1
|
|
|
""" |
2
|
|
|
Kerapu |
3
|
|
|
""" |
4
|
1 |
|
import csv |
5
|
|
|
|
6
|
|
|
|
7
|
1 |
|
class Shredder: |
8
|
|
|
""" |
9
|
|
|
Klasse voor het schreden van XML-bestanden en opslaan in CSV-formaat. |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
13
|
1 |
|
def __init__(self, io, target_dir): |
14
|
|
|
""" |
15
|
|
|
Object constructor. |
16
|
|
|
|
17
|
|
|
:param str target_dir: De folder waar de CSV-bestanden moeten worden opgeslagen. |
18
|
|
|
""" |
19
|
1 |
|
self._io = io |
20
|
|
|
""" |
21
|
|
|
The output decorator. |
22
|
|
|
|
23
|
|
|
:type: kerapu.style.KerapuStyle.KerapuStyle |
24
|
|
|
""" |
25
|
|
|
|
26
|
1 |
|
self.__target_dir = target_dir |
27
|
1 |
|
""" |
28
|
|
|
De folder waar de CSV-bestanden moeten worden opgeslagen. |
29
|
|
|
|
30
|
|
|
:type: str |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
34
|
1 |
|
@staticmethod |
35
|
|
|
def extract_field(element, tag): |
36
|
|
|
""" |
37
|
|
|
Extracts de waarde van een XML element. |
38
|
|
|
|
39
|
|
|
:param lxml.etree.Element element: Het parent XML element. |
40
|
|
|
:param str tag: De tag van het gevraagde XML-element. |
41
|
|
|
|
42
|
|
|
:rtype: str |
43
|
|
|
""" |
44
|
1 |
|
elements = element.xpath(tag, namespaces={'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/'}) |
45
|
1 |
|
if elements: |
46
|
1 |
|
return elements[0].text |
47
|
|
|
|
48
|
1 |
|
return '' |
49
|
|
|
|
50
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
51
|
1 |
|
def extract_table(self, table, filename, fields, xpaths): |
52
|
|
|
""" |
53
|
|
|
Extracts een groupertabel uit XML een slaat de tabel op in een CSV-bestand. |
54
|
|
|
|
55
|
|
|
:param Element table: De naam van de groupertabel. |
56
|
|
|
:param str filename: De filenaam van het CSV-bestaand. |
57
|
|
|
:param list fields: Een lijst met velden (d.w.z. kolomen in het CSV-bestand). |
58
|
|
|
:param list xpaths: Een lijst met xpath voor het extracten van de bovenstaande velden. |
59
|
|
|
""" |
60
|
|
|
# Sanity test. |
61
|
1 |
|
if len(fields) != len(xpaths): |
62
|
|
|
raise ValueError("fields and xpaths must have equal length") |
63
|
|
|
|
64
|
|
|
# Open the file and create CSV writer. |
65
|
1 |
|
file = open(self.__target_dir + '/' + filename, 'wt') |
66
|
1 |
|
writer = csv.writer(file) |
67
|
|
|
|
68
|
|
|
# Write header row. |
69
|
1 |
|
writer.writerow(fields) |
70
|
|
|
|
71
|
|
|
# Write all rows in the XML 'table'. |
72
|
1 |
|
row_count = 0 |
73
|
1 |
|
for element in table: |
74
|
1 |
|
row = [] |
75
|
1 |
|
for xpath in xpaths: |
76
|
1 |
|
row.append(Shredder.extract_field(element, xpath)) |
77
|
1 |
|
writer.writerow(row) |
78
|
1 |
|
row_count += 1 |
79
|
|
|
|
80
|
|
|
# Close the file. |
81
|
1 |
|
file.close() |
82
|
|
|
|
83
|
1 |
|
self._io.text('Wrote {:6d} rows to <fso>{}</fso>'.format(row_count, filename)) |
84
|
|
|
|
85
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
86
|
|
|
|