forked from fberrizbeitia/wikidataEnricher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONtoXML.py
66 lines (52 loc) · 2.01 KB
/
JSONtoXML.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
from json2xml import json2xml
import json
import io
import os
import xml.etree.ElementTree as ET
import csv
path = 'enriched-publications.json'
target = 'enriched-publications.xml'
target_model='eprints-model.xml'
target_transformed= os.path.splitext(target)[0]+'-eprints-model.xml'
print ("opening: "+path)
with open(path,'r',encoding='utf-8') as jsonfile:
jsonText = jsonfile.read()
jsonRecords = json.loads(str(jsonText),strict=False)
mydata=json2xml.Json2xml(jsonRecords, attr_type=False ).to_xml()
print ("opening: "+target)
myfile = open(target, "w")
myfile.write(mydata)
print ("wrote XML to: "+target)
sourcetree = ET.parse(target)
targettree = ET.parse(target_model)
sourceroot = sourcetree.getroot()
targetroot = targettree.getroot()
attrib = {}
print ("wrote XMl file. transforming file...")
print ("source: "+target)
print ("target: "+target_transformed)
for elem in sourceroot.find("graph").findall("item"):
#print ("found item")
# adding an element to the root node
targetelement = targetroot.makeelement("eprint", attrib)
elementToAdd = elem.find('DOI')
targetelementid = targetroot.makeelement("eprintid", attrib)
targetelementid.text = elementToAdd.text
targetelement.append(targetelementid)
elementToAdd = elem.find('Year')
targetelementid = targetroot.makeelement("date", attrib)
targetelementid.text = elementToAdd.text
targetelement.append(targetelementid)
elementToAdd = elem.find('Conference')
targetelementid = targetroot.makeelement("publication", attrib)
targetelementid.text = elementToAdd.text
targetelement.append(targetelementid)
for subelem in elem.iter('concepts'):
combinedconcepts = subelem.makeelement("concepts", attrib)
for subsubelem in subelem.iter('item'):
combinedconcepts.append(subsubelem)
targetelement.append(combinedconcepts)
targetroot.append(targetelement)
targettree.write(target_transformed, encoding="UTF-8")
print ("wrote result to file: "+target_transformed)
print ("done.")