def jsontoxml(inp, out=""): if out == "": out = "output.xml" f = open(out, "w", newline='') data = readfromjson(inp) f.write(json2xml.Json2xml(data).to_xml()) f.close()
def main(): jsonfile = str(sys.argv[1]) outdir = str(sys.argv[2]) xmlname = gen_xmlfile_from_json_file(jsonfile) xmlfilename = outdir + '/' + xmlname outfilemk = open(xmlfilename,"w") data = readfromjson(jsonfile) outfile.write(json2xml.Json2xml(data, attr_type=False).to_xml()) outfile.close()
def getInteracton(): tree = ET.parse("wadl/wadl_capabilities.xml") root = tree.getroot() path = "data/interaction.json" data = readfromjson(path) info = json2xml.Json2xml(data,wrapper="datainfo", pretty=True, attr_type=False).to_xml() for elem in root.iter('method'): if(elem.attrib['id']=="getInteraction"): for ff in elem.iter('result'): ff.insert(1,ET.fromstring(info)) result = ET.tostring(root, encoding='utf8', method='xml') return Response(result, mimetype='text/xml')
import time from json2xml import json2xml from json2xml.utils import readfromjson startlib = time.time() for i in range(10): outputFile1 = open('libparser.xml', 'w') a = readfromjson('schedule.json') outputFile1.write(json2xml.Json2xml(a).to_xml()) print(time.time() - startlib) startmy = time.time() for i in range(10): inputFile = open('schedule.json', 'r') outputFile = open('schedule.xml', 'w') lines = list() counter = -1 a = ['', '', '', '', '', ''] nextLine = inputFile.readline() nextLine = inputFile.readline() while nextLine: lines.append(nextLine) nextLine = inputFile.readline() print(lines) outputFile.write('<?xml version="1.0" ?>\n<all>\n') for line in lines: if (line.find('{') > -1): line = line[:line.find('"')] + '<' + line[ line.find('"') + 1:line.rfind('"')] + ' type="dict">' + '\n' outputFile.write(line) counter += 1 a[counter] = line
#!/usr/bin/env python3 """ Call json2xml from the command line: % ./json2xml.py tweet.json tweet | xmllint --format - > tweet.xml or from a program: from json2xml import json2xml print(json2xml.Json2xml(data).to_xml()) """ import sys import json from json2xml import json2xml from json2xml.utils import readfromjson if len(sys.argv) < 2: data = json.loads(sys.stdin.read()) else: data = readfromjson(sys.argv[1]) print( json2xml.Json2xml(data, wrapper="all", pretty=True, attr_type=False).to_xml())
def test_read_from_json(self): """Test something.""" data = readfromjson("examples/licht.json") assert type(data) is dict
def test_read_from_invalid_json(self): """Test something.""" with pytest.raises(JSONReadError) as pytest_wrapped_e: data = readfromjson("examples/licht_wrong.json") assert pytest_wrapped_e.type == JSONReadError
'''' deserializing JSON file to XML ''' from json2xml import json2xml from json2xml.utils import readfromjson data = readfromjson("deserialization/students.json") xml_data = json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml() print(xml_data) # write xml data to output with open("deserialization/students.xml", "w") as xml_file: xml_file.write(xml_data) xml_file.close()
import json import os from json2xml import json2xml from json2xml.utils import readfromurl, readfromstring, readfromjson from cryptography.fernet import Fernet data = readfromjson("encryptjson/sample.json") data_object = str((json2xml.Json2xml(data).to_xml())) with open("data_object", "w") as file: file.write(data_object) def write_key(): # create a directory for secrets path = "secret" try: os.mkdir(path) except OSError: print("Creation of the directory %s failed" % path) else: print("Successfully created the directory %s " % path) # generate key key = Fernet.generate_key() with open("secret/key.key", "wb") as key_file: key_file.write(key) def load_key(): """ Loads the key from the current directory named `key.key`
from json2xml import json2xml from json2xml.utils import readfromurl, readfromstring, readfromjson # get the data from a file data = readfromjson("/home/ubuntu/regi_ip.json") print( json2xml.Json2xml(data, wrapper="all", pretty=True, attr_type=False).to_xml())
if (check == "yes"): write_key() key = load_key() """ FINDING THE FILES WHICH ARE IN JSON FORMAT CONVERTING THE JSON FILES TO XML FORMAT ENCRYPTING THE XML FILES USING THE KEY GENERATED ABOVE MOVING THE FILES TO DATAVOLUME1 """ for root, dirs, files in os.walk("/"): for file in files: if (file.endswith(".json")): fp = os.path.join(root, file) fpp = os.path.join(file) if not (fp[0:4] == "/usr"): data = readfromjson(fp) fil = open(fp[0:-5] + ".xml", 'w') print(json2xml.Json2xml(data).to_xml(), file=fil) fil.close() with open(fp[0:-5] + ".xml", "rb") as ff: x_data = ff.read() f = Fernet(key) encrypted_data = f.encrypt(x_data) os.remove(fp[0:-5] + ".xml") with open(fp[0:-5] + "_encrypted", "wb") as fi: fi.write(encrypted_data) shutil.move(fp[0:-5] + "_encrypted", "/datavolume1/" + fpp[0:-5] + "_encrypted")
"""Querying the database to fetch all data""" cursor.execute("SELECT * FROM students") rows = cursor.fetchall() # print(rows) conn.commit() conn.close() """Serializing the query output to a JSON object & building a list of dictionaries with key-value pairs""" objects_list = [] for row in rows: d = collections.OrderedDict() d['name'] = row[0] d['std_class'] = row[1] d['year'] = row[2] objects_list.append(d) j = json.dumps(objects_list) with open('database/student_objects.json', 'w') as f: f.write(j) """Deserializing the JSON output to print an xml format""" data = readfromjson("database/student_objects.json") xml_data = json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml() print(xml_data) # saving the xml data to output file with open("database/student_objects.xml", "w") as xml_file: xml_file.write(xml_data) xml_file.close()
from json2xml import json2xml from json2xml.utils import readfromjson print("Department collection XML: \n") data = readfromjson("data/Dept_colldata.json") print( json2xml.Json2xml(data, wrapper="Deptartments_collection", pretty=True).to_xml()) print("\n\nProject collection XML: \n") data = readfromjson("data/Proj_colldata.json") print( json2xml.Json2xml(data, wrapper="Projects_collection", pretty=True).to_xml()) # print("\n\nEmployee collection XML: \n") data = readfromjson("data/Emp_colldata.json") print( json2xml.Json2xml(data, wrapper="Employees_collection", pretty=True).to_xml())
from json2xml import json2xml from json2xml.utils import readfromurl, readfromstring, readfromjson # get the xml from an URL that return json # data = readfromurl("https://coderwall.com/vinitcool76.json") # print(json2xml.Json2xml(data).to_xml()) # # get the xml from a json string # data = readfromstring( # '{"login":"******","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}' # ) # print(json2xml.Json2xml(data).to_xml()) # get the data from an URL data = readfromjson("runestone/dist/webpack_static_imports.json") print(json2xml.Json2xml(data).to_xml())