forked from hfreye/RLP-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlToJson.py
180 lines (169 loc) · 6.37 KB
/
xmlToJson.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
import xml.etree.ElementTree as ET
import json
import html
import uuid
from functools import reduce
import os
import sqlite3
allSkills = []
allCompetences = []
def skillRLPschema (tree):
#skills with tags
correctNode = tree.findall('./{http://bsbb.eu}c2/')
skillUUID = ""
skillAltName = ""
comp1 = ""
skillName = []
for item in correctNode:
if item.tag == '{http://bsbb.eu}vorwort':
continue
for subitem in item:
#area
for subsubitem in subitem:
#id, name, subarea oder id, name, competence
if subsubitem.tag == '{http://bsbb.eu}name':
comp1 = subsubitem.text
print("Competence: "+comp1)
#if subsubitem.tag == '{http://bsbb.eu}stufe':
# if subsubitem.tag == "":
# continue
# else:
# comp1 = subsubitem.text
# print("Competence: "+comp1)
if subitem.tag == '{http://bsbb.eu}competence':
skillName = []
for subskill in subsubitem:
if subskill.tag == '{http://bsbb.eu}name':
print("other competence? "+subskill.text)
#id, name, stufe
#if subsubitem.tag == '{http://bsbb.eu}stufe':
#id, name, stufe
#if subskill.tag == '{http://bsbb.eu}id':
# varLevel = subskill.text
# print(varLevel)
if subskill.tag == '{http://bsbb.eu}standard':
for subsubskill in subskill:
if subsubskill.tag == '{http://bsbb.eu}id':
skillAltName = subsubskill.text
print(skillAltName)
if subsubskill.tag == '{http://bsbb.eu}content':
skillName.append(html.escape(subsubskill.text))
print(skillName)
skillUUID = uuid.uuid4()
outputSkill={
"@type":"competence",
"id": str(uuid.uuid4()),
"name":html.escape(comp1),
"fach":varFach,
"bundesland": "Berlin-Brandenburg",
"alternateName": skillAltName,
"category":varCategory,
"skills":skillName
}
allSkills.append(outputSkill)
correctNode = tree.findall('./{http://bsbb.eu}c3/{http://bsbb.eu}themainhalt/')
for item in correctNode:
print(item.tag)
if item.tag=='{http://bsbb.eu}id':
skillAltName = item.text
elif item.tag=='{http://bsbb.eu}title':
skillName = item.text
elif item.tag=='{http://bsbb.eu}content':
descrSkill = item.text
else:
continue
outputSkill={
"@type":"skill",
"id": str(skillUUID),
"name": html.escape(comp1),
"fach": varFach,
"bundesland": "Berlin-Brandenburg",
"alternateName": skillAltName,
"category": varCategory,
"competence": skillName
}
allSkills.append(outputSkill)
def saveToDB ():
connection = sqlite3.connect("/home/maike/RLP-XML/skills.db")
cursor = connection.cursor()
# uuid hinzu und id="k-1" speichern als verarbeitungsvariable
sql_command = """
CREATE TABLE competence (
uuid VARCHAR(36) PRIMARY KEY,
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
alternateName VARCHAR(128),
num_skills INT,
category VARCHAR(20));"""
cursor.execute(sql_command)
sql_command = """
CREATE TABLE skill (
uuid VARCHAR(36) PRIMARY KEY,
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
alternateName VARCHAR(128),
uuid_competence VARCHAR(36),
category VARCHAR(20));"""
cursor.execute(sql_command)
sql_command = """
CREATE TABLE topic (
uuid VARCHAR(36) PRIMARY KEY,
name VARCHAR(128),
fach VARCHAR(128),
bundesland VARCHAR(128),
content TEXT,
category VARCHAR(20));"""
cursor.execute(sql_command)
for ele in allCompetences:
'''
sql_command = """INSERT INTO subcategory (skillcat_uuid, category, finished_last, next_skill, last_skill)
VALUES (NULL, "William", "Shakespeare", "m", "1961-10-25");"""
cursor.execute(sql_command)
'''
cursor.execute(sql_command)
for ele in allSkills:
print(ele)
sql_command = """"""
cursor.execute(sql_command)
# never forget this, if you want the changes to be saved:
connection.commit()
connection.close()
def chooseRLPSchema (f):
tree = ET.parse(f)
decisionTree = tree.getroot()
print(decisionTree.tag)
if decisionTree.tag == '{http://bsbb.eu}fach':
print("with tags")
skillRLPschema(tree)
else:
print("without tags")
if decisionTree[2].tag == '{http://bsbb.eu}kapitel':
skillRLPschema2(tree)
elif decisionTree[2].tag == '{http://bsbb.eu}einleitung':
skillRLPschema1(tree)
print(decisionTree[2].tag)
else:
print("something went wrong")
print(allSkills)
basepath =input("Give me your Poor, your huddled XML-File_Path: ")
# flush database
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;""")
# List all files in a the using scandir()
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_file():
fileExtension = os.path.splitext(entry.name)
if fileExtension[1] == '.xml':
f = open(entry, "r")
fachType = tree.getroot()
print(fachType[1].text)
varFach = fachType[1].text
varCategory = input("Which category is this? (Logik, Sprache, Soziales, Bewegung)")
skillRLPschema(tree)
saveToDB()