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