forked from hfreye/RLP-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlToStructure2.py
303 lines (294 loc) · 11 KB
/
xmlToStructure2.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#import json
import os
from xml.etree import ElementTree
import sqlite3
import html
categoryInput = dict([
("bio.xml","Logik"),
("jp.xml","Kommunikation"),
("teilb.xml","Soziales"),
("GEWI.xml","Soziales"),
("rlp110.xml","Soziales"),
("nw56.xml","Logik"),
("he.xml","Kommunikation"),
("el.xml","Kommunikation"),
("de.xml","Kommunikation"),
("pl.xml","Kommunikation"),
("gewi.xml","Soziales"),
("bcm.xml","Soziales"),
("PB.xml","Soziales"),
("zh.xml","Kommunikation"),
("it.xml","Kommunikation"),
("fr.xml","Kommunikation"),
("tr.xml","Kommunikation"),
("ru.xml","Kommunikation"),
("sw.xml","Kommunikation"),
("fs.xml","Kommunikation"),
("La.xml","Kommunikation"),
("es.xml","Kommunikation"),
("en.xml","Kommunikation"),
("agr.xml","Kommunikation"),
("bcs.xml","Kommunikation"),
("pt.xml","Kommunikation"),
("dgs.xml","Kommunikation"),
("Deutsch.xml","Kommunikation"),
("ch.xml","Logik"),
("AS.xml","Logik"),
("wat.xml","Logik"),
("ku.xml","Logik"),
("ma.xml","Logik"),
("GEO.xml","Logik"),
("geo.xml","Logik"),
("nw.xml","Logik"),
("Phil.xml","Logik"),
("Ph.xml","Logik"),
("MU.xml","Logik"),
("Inf.xml","Logik"),
("L-E-R.xml","Soziales"),
("Psy.xml","Soziales"),
("eth.xml","Soziales"),
("su.xml","Soziales"),
("ge.xml","Soziales"),
("teila.xml","Soziales"),
("sowiwiwi.xml","Soziales"),
("thea.xml","Bewegung"),
("Thea.xml","Bewegung")])
allCompetences = []
allSkills = []
allTopics = []
def flushDB ():
connection = sqlite3.connect("/home/maike/RLP-XML/skills.db")
cursor = connection.cursor()
cursor.execute("""DROP TABLE competence;""")
cursor.execute("""DROP TABLE skill;""")
cursor.execute("""DROP TABLE topic;""")
def createDB ():
connection = sqlite3.connect("/home/maike/RLP-XML/skills.db")
cursor = connection.cursor()
#insert unique again! primary key!
sql_command = """
CREATE TABLE competence (
alternateName VARCHAR(128),
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
category VARCHAR(20));"""
cursor.execute(sql_command)
sql_command = """
CREATE TABLE skill (
alternateName VARCHAR(128),
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
category VARCHAR(20));"""
cursor.execute(sql_command)
sql_command = """
CREATE TABLE topic (
alternateName VARCHAR(128),
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
content TEXT,
category VARCHAR(20));"""
cursor.execute(sql_command)
# never forget this, if you want the changes to be saved:
connection.commit()
connection.close()
def writetoDB(allwhateva, varTable):
try:
connection = sqlite3.connect("/home/maike/RLP-XML/skills.db")
cursor = connection.cursor()
print("Connected to SQLite")
sql_command = ""
if varTable == "competence":
#print("competence")
sql_command = """INSERT INTO competence
VALUES (?,?,?,?,?);"""
elif varTable == "skill":
#print("skill")
sql_command = """INSERT INTO skill
VALUES (?,?,?,?,?);"""
elif varTable == "topic":
#print("topic")
sql_command = """INSERT INTO topic
VALUES (?,?,?,?,?,?);"""
connection.executemany(sql_command, allwhateva)
connection.commit()
#print("Python Variables inserted successfully into SqliteDb_developers table")
"""
for row in cursor.execute("SELECT * FROM competence"):
print(row)
"""
connection.close()
except sqlite3.Error as error:
print("Failed to insert Python variable into sqlite table", error)
finally:
if connection:
connection.close()
print("The SQLite connection is closed")
#quit()
def goDeep(tree, varCategory, varFach, fileExceptionName):
competenceName= []
competenceId= []
skillId=[]
skillName=[]
topicId=[]
topicName=[]
topicContent=[]
tryNodes = tree.getroot()
if varFach == "Musik" or varFach == "Geschichte" or varFach == "Deutsche Gebärdensprache" \
or varFach == "Latein" or varFach == "Politische Bildung" or varFach == "Philosophie" \
or varFach == "Theater" or varFach == "Psychologie" or varFach == "Informatik" \
or varFach == "Altgriechisch" or varFach=="Lebensgestaltung-Ethik-Religionskunde" \
or varFach == "Deutsch" or varFach == "Kunst" or varFach == "Gesellschaftswissenschaften":
print("this is Music")
good_bot = tryNodes.findall('./c2/area/')
for item in good_bot:
#print(item.tag + ": " + item.text)
compId = ""
compName = ""
for ites in item:
#print(ites.tag + " 2: " + ites.text)
#print(iter.tag + ": " + iter.text)
if ites.tag == "name":
if ites.text is not None:
compName = html.escape(ites.text)
if ites.tag == "id":
if ites.text is not None:
compId = html.escape(ites.text)
if ites.tag == "stufe":
for stufe in ites:
skillsid=""
skillsname=""
for standard in stufe:
#print("worked")
if standard.tag == "id":
skillsid=html.escape(standard.text)
if standard.tag == "content":
skillsname=html.escape(standard.text)
if skillsid!="" and skillsname!="":
skillId.append(skillsid)
skillName.append(skillsname)
if compId != "" and compName != "":
competenceName.append(compName)
competenceId.append(compId)
#print("worked")
#print(compId + ": " + compName)
else:
good_bot = tryNodes.findall('./c2/area/')
for item in good_bot:
for ites in item:
compId = ""
compName = ""
for iter in ites:
if iter.tag == "name":
if iter.text is not None:
compName = html.escape(iter.text)
if iter.tag == "id":
if iter.text is not None:
compId = html.escape(iter.text)
if iter.tag == "stufe":
for stufe in iter:
skillsid=""
skillsname=""
for standard in stufe:
#print("worked")
if standard.tag == "id":
skillsid=html.escape(standard.text)
if standard.tag == "content":
skillsname=html.escape(standard.text)
if skillsid!="" and skillsname!="":
skillId.append(skillsid)
skillName.append(skillsname)
if compId != "" and compName != "":
competenceName.append(compName)
competenceId.append(compId)
#print("worked")
#print(compId + ": " + compName)
"""
allNodes = tryNodes.findall('./c2/area/competence/name')
#competence name
for item in allNodes:
competenceName.append(html.escape(item.text))
allNodes = tryNodes.findall('./c2/area/competence/id')
#competence id?
for item in allNodes:
competenceId.append(html.escape(item.text))
allNodes = tryNodes.findall('./c2/area/competence/stufe/standard/id')
#skill id
for item in allNodes:
skillId.append(html.escape(item.text))
allNodes = tryNodes.findall('./c2/area/competence/stufe/standard/content')
#skill name
for item in allNodes:
skillName.append(html.escape(item.text))
"""
if fileExceptionName == "GEO.xml" or fileExceptionName == "geo.xml":
return
if len(competenceName) == 0:
allNodes = tryNodes.findall('./c2/area/name')
for item in allNodes:
competenceName.append(html.escape(item.text))
if len(competenceId) == 0:
allNodes = tryNodes.findall('./c2/area/id')
for item in allNodes:
competenceName.append(html.escape(item.text))
print(len(competenceId))
print(competenceId)
print(len(competenceName))
print(competenceName)
"""
/bc/bc3/area[1]/competence[1]/name
"""
allNodes = tryNodes.findall('./c3/themainhalt/id')
for node in allNodes:
topicId.append(html.escape(node.text))
allNodes = tryNodes.findall('./c3/themainhalt/title')
for node in allNodes:
topicName.append(html.escape(node.text))
allNodes = tryNodes.findall('./c3/themainhalt/content')
for node in allNodes:
topicContent.append(html.escape(node.text))
if len(skillId) == len(skillName):
#print("basst scho")
for ele in range (0,len(skillId)):
element = (skillId[ele],skillName[ele], varFach, "Berlin-Brandenburg", varCategory)
allSkills.append(element)
if len(competenceId) == len(competenceName):
#print("basst scho, aber bei competence")
for ele in range (0,len(competenceId)):
element = (competenceId[ele],competenceName[ele], varFach, "Berlin-Brandenburg", varCategory)
allCompetences.append(element)
if len(topicId) == len(topicName):
#print("basst scho, aber bei topics, hier weiter")
if len(topicId) == len(topicContent):
for ele in range (0,len(topicId)):
element = (topicId[ele],topicName[ele], varFach, "Berlin-Brandenburg", topicContent[ele], varCategory)
allTopics.append(element)
flushDB()
createDB()
basepath = "/home/maike/RLP-XML/"
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_file():
fileExtension = os.path.splitext(entry.name)
print(entry.name)
if fileExtension[1] == '.xml':
f = open(entry, "r")
try:
tree = ElementTree.parse(f)
fachType = tree.getroot()
print(fachType[1].text)
except ElementTree.ParseError as e:
print(e)
varCategory=categoryInput[entry.name]
#print(varCategory)
varFach = fachType[1].text
goDeep(tree, varCategory, varFach, entry.name)
f.close()
writetoDB(allCompetences, "competence")
writetoDB(allSkills, "skill")
writetoDB(allTopics, "topic")
#print(allCompetences)
#print(allSkills)
#print(allTopics)