forked from hfreye/RLP-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlToJson3.py
172 lines (159 loc) · 5.62 KB
/
xmlToJson3.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import xml.etree.ElementTree as ET
import json
#from uuid import UUID
import html
import uuid
from functools import reduce
demo=input("Give me your Poor, your huddled XML-File_Path: ")
fileInput = demo[20:29]
fileInput = fileInput.rstrip(".xml")
print(fileInput)
f = open(demo, "r")
#for file name configure rest
#ask which of the 4 this is
#skills
tree = ET.parse(f)
#print("competence")
nodes = tree.findall('./subarea/')
fachType = tree.getroot()
print(fachType[1].text)
varFach = fachType[1].text
#varCategory = input("Which category is this? (Logik, Sprache, Soziales, Bewegung)")
correctNode = nodes
skillUUID = ""
#print(skillUUID)
skillId = 1
skillName = ""
skillAltName = ""
skillComp = ""
sampleNode = ""
f = 2
allSkills=[]
#print(correctNode.findall('./'))
for item in correctNode:
for subitem in item:
print(subitem.tag)
if subitem.tag == 'name':
print(subitem.text)
for subsubitem in subitem:
#if subsubitem.tag == 'name':
# category = subsubitem.text
print(subsubitem.tag)
for skill in subsubitem:
print(skill.text)
for subskill in skill:
if subskill.tag == 'level':
skillComp = subskill.text
#print(subskill.text)
for subsubskill in subskill:
#print(subsubskill.text)
if subsubskill.tag == 'id':
skillAltName = subsubskill.text
#print(skillId)
elif subsubskill.tag == 'content':
skillName = html.escape(subsubskill.text)
skillUUID = uuid.uuid4()
#print(skillUUID)
outputComp={
"@type":"competence",
"id": str(skillUUID),
"name":skillName,
"fach": varFach,
"level":skillComp,
"alter":"",
"schultyp":"",
"bundesland": "Berlin-Brandenburg",
"alternateName": [skillName, skillAltName],
"category": varCategory,
"competence":category,
"description":"",
"klasse":"",
"url":""
}
#skillId += 1
#print(outputComp)
allSkills.append(outputComp)
#print(allSkills[2]['name'])
#print("end competence")
#print("skills")
nodes = tree.findall('./c3/themainhalt/')
skillName = ""
skillAltName = ""
comp1 = ""
for node in nodes:
#print(node.tag)
for item in node:
if item.tag == 'name':
comp1 = item.text
print(item.tag)
#print(item.tag)
for subitem in item:
#print(subitem.tag)
#print(comp1)
for skill in subitem:
#print(skill.tag)
#print(skill.text)
if skill.tag=='id':
skillAltName = skill.text
elif skill.tag=='content':
skillName = skill.text
else:
continue
if type(skillName) == type(None):
continue
#mask all Sonderzeichen!!
outputSkill={
"@type":"skill",
"id": str(skillUUID),
"name":html.escape(skillName),
"fach":varFach,
"level":"",
"alter":"",
"schultyp":"",
"bundesland": "Berlin-Brandenburg",
"alternateName": [skillName, skillAltName],
"category":"varCategory",
"competence": comp1,
"description":"",
"klasse":"",
"url":""
}
#skillId2 = str(skillId)
allSkills.append(outputSkill)
skillId += 1
#print(allSkills[50])
varReturn = []
varNode = []
sampleNode = ""
sampleReturn = ""
createCypher = ""
enumSkill = 1
for n in allSkills:
varNode.append('(u'+str(enumSkill)+':Skill{uuid:"'+n['id']+'", name:"' +n['name'] + '", fach:"'+n['fach']+'", level:"'+n['level']+'", bundesland:"'+n['bundesland']+'", competence:"'+n['competence']+'"})')
#varReturn2 = 'u'+str(n['id'])+' '
varReturn.append('u'+str(enumSkill))
#sampleNode='Create (u'+str(n['id'])+':Skill{name:"' +n['name'] +'"}) SET u.id="'+str(n['id'])+'", u.fach="'+n['fach']+'", u.level="'+n['level']+'", u.bundesland="'+n['bundesland']+'", u.subcategory="'+n['subcategory']+'";\n'
enumSkill += 1
varNode = str(varNode).strip("[]")
varReturn = str(varReturn).strip("[]")
varNode = varNode.replace("'","")
varReturn = varReturn.replace("'","")
varNode = varNode.replace("\n","")
varReturn = varReturn.replace("\n","")
sampleNode = "Create "+varNode+"\n"
sampleReturn = 'return '+varReturn
createCypher = sampleNode + sampleReturn
print(createCypher)
'''MATCH
(a:Person),
(b:Person)
WHERE a.name = 'A' AND b.name = 'B'
CREATE (a)-[r:RELTYPE]->(b)
RETURN type(r)'''
dataname1 = "/home/maike/RLP-XML/" + fileInput + ".txt"
with open(dataname1, 'w') as d:
d.writelines(createCypher)
dataname2 = "/home/maike/RLP-XML/" + fileInput + ".json"
allSkills = json.dumps(allSkills)
with open(dataname2, 'w') as g:
g.writelines(allSkills)