-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-to-json.py
executable file
·72 lines (61 loc) · 1.3 KB
/
xml-to-json.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
#!/usr/bin/env python3
import io
import json
import arrow
import xml.etree.ElementTree as ET
_input = './data/eponyms.xml'
_output = './export.json'
_main_elem = 'main'
def convert(source, target):
js = read_and_convert(source)
with io.open(target, 'w') as h:
json.dump(js, h)
def read_and_convert(source):
root = ET.parse(source).getroot()
tags = []
main = []
# author
andrew = {
'_id': 'andrewyee',
'type': 'author',
'name': 'Andrew J Yee',
}
# find categories
for category in root.iter('category'):
tag = {
'_id': category.get('tag').lower(),
'type': 'tag',
'content': {
'en': category.get('title'),
},
}
tags.append(tag)
# find eponyms
for eponym in root.iter('eponym'):
epo = {
'_id': eponym.get('id').lower(),
'type': _main_elem,
'tags': [cat.text.lower() for cat in eponym.iter('cat')],
'content': {
'en': {
'name': eponym.find('name').text,
'text': eponym.find('desc').text,
}
},
'audits': [{
'author': 'andrewyee',
'date': arrow.get(eponym.find('c').text).format('YYYY-MM-DD'),
'action': 'create',
}],
}
main.append(epo)
# concat
docs = [andrew]
docs.extend(tags)
docs.extend(main)
return {
'date': arrow.get().isoformat(),
'documents': docs,
}
if '__main__' == __name__:
convert(_input, _output)