Пример #1
0
def convert_df_xml(df):
    list_dict = []
    for index, row in list(df.iterrows()):
        row['date'] = row['date'].__str__()
        list_dict.append(dict(row))
    json_dict = readfromstring(str(json.dumps(list_dict, ensure_ascii=False)))
    return json2xml.Json2xml(json_dict, wrapper="all", pretty=True).to_xml()
Пример #2
0
def faker_datagen():

    faker = Faker()
    count = 0
    while count < no_of_records:
        profile = faker.simple_profile()
        message = json.dumps(profile, cls=DatetimeEncoder)
        # get the xml from a json string
        data = readfromstring(message)
        message_xml = json2xml.Json2xml(data, pretty=True,
                                        attr_type=False).to_xml()
        key = str(profile['username'])

        s3_key = "snowflake-xml-demo/" + s3_prefix + "/year=" + str(
            datetime.now().strftime('%Y')) + "/month=" + str(
                datetime.now().strftime('%m')) + "/day=" + str(
                    datetime.now().strftime('%d')) + "/hour=" + str(
                        datetime.now().strftime('%H')) + "/" + key + ".xml"

        uploaded = upload_to_aws(data=message_xml,
                                 bucket=BUCKET_NAME,
                                 s3_file=s3_key)

        count += 1
        print("Sleep Counter                : " + str(count))
        time.sleep(loop_delay)
    print("Finished Processing")
Пример #3
0
 def test_json_to_xml_conversion(self):
     data = readfromstring(
         '{"login":"******","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
     )
     xmldata = json2xml.Json2xml(data).to_xml()
     dict_from_xml = xmltodict.parse(xmldata)
     assert type(dict_from_xml["all"]) == OrderedDict
def faker_datagen():
    conf = {
        'bootstrap.servers': bootstrap_servers,
        'client.id': socket.gethostname()
    }

    producer = Producer(conf)
    faker = Faker()
    count = 0
    while count < no_of_records:
        profile = faker.simple_profile()
        message = json.dumps(profile, cls=DatetimeEncoder)
        # get the xml from a json string
        data = readfromstring(message)
        message_xml = json2xml.Json2xml(data, pretty=True,
                                        attr_type=False).to_xml()
        key = str(profile['username'])
        producer.produce(topic=topic_name, value=message_xml, key=key)
        producer.flush()

        s3_key = "snowflake-xml-demo/" + topic_name + "/year=" + str(
            datetime.now().strftime('%Y')) + "/month=" + str(
                datetime.now().strftime('%m')) + "/day=" + str(
                    datetime.now().strftime('%d')) + "/hour=" + str(
                        datetime.now().strftime('%H')) + "/" + key + ".xml"

        uploaded = upload_to_aws(data=message_xml,
                                 bucket=BUCKET_NAME,
                                 s3_file=s3_key)

        count += 1
        print("Sleep Seconds                : " + str(loop_delay))
        time.sleep(loop_delay)
    print("Finished Processing")
Пример #5
0
def json_file_to_xml_string(json_file):
    """takes a json file and returns an xml string"""
    json_string = json_file.read()

    data = readfromstring(json_string)
    xml_string = json2xml.Json2xml(data).to_xml()

    return et.fromstring(xml_string)[
        0]  # [0] because json2xml adds an unneeded wrapper
Пример #6
0
 def test_custom_wrapper_and_indent(self):
     data = readfromstring(
         '{"login":"******","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
     )
     xmldata = json2xml.Json2xml(data, wrapper="test", pretty=True).to_xml()
     old_dict = xmltodict.parse(xmldata)
     # test must be present, snce it is the wrpper
     assert "test" in old_dict.keys()
     # reverse test, say a wrapper called ramdom won't be present
     assert "random" not in old_dict.keys()
Пример #7
0
def convert(input_file, output_file):
    with open(input_file, 'r') as json_file:
        json_lines = json_file.read()
        data = readfromstring(json_lines)
        xml_result = json2xml.Json2xml(data).to_xml()
        if output_file:
            with open(output_file, 'w') as out:
                out.writelines(xml_result)
        else:
            print(xml_result)
Пример #8
0
 async def xml_json_convertor(self, convertto, data):
     try:
         if convertto == "json":
             ans = xmltodict.parse(data)
             json_data = json.dumps(ans)
             return json_data
         else:
             ans = readfromstring(data)
             return json2xml.Json2xml(ans, wrapper="all", pretty=True).to_xml()
     except Exception as e:
         return e
Пример #9
0
def export_data(request):
    logger.info("  ** Reports -> views.export_data method")
    if request.method == 'POST':
        logger.info("  **   POST method")

        # Get option from form
        file_format = request.POST['file-format']
        part_resource = PartResource()
        dataset = part_resource.export()

        if file_format == 'CSV':
            logger.info("  **     Generate CSV file...")
            response = HttpResponse(dataset.csv, content_type='text/csv')
            response[
                'Content-Disposition'] = 'attachment; filename="part_exported_data.csv"'
            return response
        elif file_format == 'JSON':
            logger.info("  **     Generate JSON file...")
            response = HttpResponse(dataset.json,
                                    content_type='application/json')
            response[
                'Content-Disposition'] = 'attachment; filename="part_exported_data.json"'
            return response
        elif file_format == 'XLS':
            logger.info("  **     Generate XLS file...")
            response = HttpResponse(dataset.xls,
                                    content_type='application/vnd.ms-excel')
            response[
                'Content-Disposition'] = 'attachment; filename="part_exported_data.xls"'
            return response
        elif file_format == 'XML':
            logger.info("  **     Generate XML file...")
            # This step is using json2xml library
            # To do so, the code converts to JSON to convert (again) to XML
            logger.info("  **     Convert to XML file using json2xml...")
            xml_output = json2xml.Json2xml(readfromstring(
                dataset.json)).to_xml()

            response = HttpResponse(xml_output, content_type='application/xml')
            response[
                'Content-Disposition'] = 'attachment; filename="part_exported_data.xml"'
            return response

    return render(request, 'forms/export.html')
Пример #10
0
def convertToXML():
    global inputJson
    global resultXml

    # check if there was inserted a json file; if not, show a pop up error message
    if (not inputJson):
        resultXml = ""
        errorWindow = Tk()
        errorWindow.title('Convert ERROR')
        errorLabel = Label(errorWindow,
                           text="No JSON to convert!",
                           fg='red',
                           font=("Helvetica", 16))
        errorLabel.place(x=60, y=50)
        errorWindow.geometry("300x100+350+200")
        errorWindow.mainloop()

        return

    # tries to convert the json file into xml; if the json is not well formated it gives out an error message

    with open(inputJson, 'r') as myfile:
        strJson = myfile.read()
    try:
        json_object = json.loads(strJson)
    except Exception as e:
        resultXml = ""
        errorWindow = Tk()
        errorWindow.title('Convert ERROR')
        errorLabel = Label(errorWindow,
                           text="Bad file!",
                           fg='red',
                           font=("Helvetica", 16))
        errorLabel.place(x=60, y=50)
        errorWindow.geometry("200x100+350+200")
        errorWindow.mainloop()
        return

    jsonData = readfromstring(strJson)
    xml = json2xml.Json2xml(jsonData).to_xml()
    resultXml = xml

    return
Пример #11
0
    def post(self):
        data = self.reqparse.parse_args()

        if data['json']:
            try:
                # readjson = ""
                # readjson = data['json']
                # print(data['json'])
                readjson = readfromstring(data['json'])
                returning = j2x.Json2xml(readjson, wrapper="all",
                                         pretty=True).to_xml()
                print(returning)
                headers = {'Content-Type': 'text/xml'}
                return make_response(returning, 200, headers)
            except:
                return {'json': 'Not json given!'}

        else:
            return {'json': 'Something when wrong!'}
Пример #12
0
def update_tree(number_of_times_button_has_clicked):
    cmd = C4_5_COMMAND + " " + UPLOAD_DIRECTORY + "/file"  # + " > " + RESULT_FILE_NAME

    os.popen("chmod +x bin/average")
    os.popen("chmod +x bin/c45")
    os.popen("chmod +x bin/c4.5rules")
    os.popen("chmod +x bin/consult")
    os.popen("chmod +x bin/consultr")
    lines = os.popen(cmd).readlines()
    print(lines)

    json_source = """{
    {
        "root": {
        }
    }
    """

    xml_source = """
    <?xml version="1.0" ?>
    <all>
    	<root type="dict">
    	</root>
    </all>
    """

    try:
        tree = parser.Tree()
        select_lines = parser.get_tree_lines(lines)
        tree.root = parser.division_tree(select_lines, 0)
        dot_source = str(parser.generate_graphviz(tree))

        json_source = json.dumps(tree, indent=2)
        data = readfromstring(json_source)
        xml_source = json2xml.Json2xml(data).to_xml()

        return [dot_source, dot_source, json_source, xml_source]
    except:
        return [dot_source_error, dot_source_error, json_source, xml_source]

    return [dot_source, dot_source, json_source, xml_source]
Пример #13
0
 def test_read_from_jsonstring(self):
     data = readfromstring(
         '{"login":"******","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
     )
     assert type(data) is dict
Пример #14
0
def parse_json2xml(string, name_file):
    file_to_write = open(name_file, 'w')
    data = readfromstring(string)
    print(json2xml.Json2xml(data).to_xml(), file=file_to_write)
Пример #15
0
    if event == 'Generate':
        
        reportName = values['-reportName-']
        reportDescription = values['-reportDescription-']
        
    
        for i, x in enumerate(reportList):
            
            query = {"_id": x}
            mydoc = collectionScan.find(query)

            for c in mydoc:
               data = c["XML"]
            
            #convert back to xml
            doc2 = readfromstring(data)
            doc3 = json2xml.Json2xml(doc2).to_xml()

            #save to file
            f = open(reportName + '_' + reportDescription + str(i) + '_.xml', "w")
            
            f.write(doc3)
            f.close()

            reportList = []
    
    #if button clicked is run all
    if event == 'Run All':
        
        #getting all scans from table
        tableElement = window['-SCANTABLE-'].get()
payload = []

content = {}

for row in rv:
    content = {
        'STOP_ID': row[0],
        'NAME': row[1],
        'ADDRESS': row[2],
        'LAT': row[3],
        'LNG': row[4],
        'TYPE': row[5]
    }
    payload.append(content)
    content = {}
stud_json = json.dumps(payload)
data = readfromstring(stud_json)
xml_data = json2xml.Json2xml(data, wrapper="all", pretty=True,
                             attr_type=False).to_xml()

#for loc in range():
loc = str(xml_data)
file_path = './liv_loc/loc' + '.xml'

file = open(file_path, 'w')

with open(file_path, 'w') as file:
    file.write(loc)

file.close()
Пример #17
0
# Neueda Task Python script.
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

import json
from cryptography.fernet import Fernet
from json2xml import json2xml
from json2xml.utils import readfromstring

# Opening JSON file
f = open("./data/json_file.json", "r")

# returns JSON object as a dictionary
data = json.load(f)
#convert dict to string
data2= json.dumps(data)
data3 = readfromstring(data2)
#convert the string to xml and write to file on namedvolume /outputdata
with open("./outputdata/json_to_xml.xml", "w") as f:
    f.write(json2xml.Json2xml(data3).to_xml())

def write_key():
    """Generates a key and save it into a file on named volume /outputdata"""
    key = Fernet.generate_key()
    with open("./outputdata/key.key", "wb") as key_file:
        key_file.write(key)

def load_key():
    """Loads the key from the named volume /outputdata named `key.key`"""
    return open("./outputdata/key.key", "rb").read()

def encrypt(filename, key):
Пример #18
0
def json_2_xml(data):
    data = readfromstring(data)
    response = json2xml.Json2xml(data).to_xml()
    return ET.tostring(ET.fromstring(response))
Пример #19
0
 def w(*a, **kw):
     text = f(*a, **kw) or "{}"  # prevent json2xml crashing on empty input
     return Json2xml(readfromstring(text)).to_xml()
Пример #20
0
#!/usr/bin/env/ python


#import statements
#pip install json2xml if you're missing it

from json2xml import json2xml
from json2xml.utils import readfromstring
#import regular expressions

import re

#name of .json file to be read#
filepath = 'annotations.json'
#get all non-whitespace characters
list_to_parse = re.findall(open(filepath).read(), '\S')
string_to_parse = ""
for s in list_to_parse:
	string_to_parse = string_to_parse +s

data = readfromstring(string_to_parse)

#write to output file
file_output = open("output.xml", 'w')

file_output.write(json2xml.Json2xml(file_output).to_xml())

Пример #21
0
 def generate_xml():
     js = dataframe.to_json(orient='table')
     data = readfromstring(js)
     xml_data = json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml()
     with open('./TgtFiles/xmlout.xml', 'w+') as f:
         f.write(xml_data)
Пример #22
0
 def formato_xml(self, formato="xml"):
     data = readfromstring(self.formato_json()) # obtener el xml de una cadena json
     return ''.join(json2xml.Json2xml(data).to_xml().split()) #retorna el xml
    key = os.getenv('KEY_ENCRYPT')
    key = Fernet.generate_key()
    with open("/shared/secret.key", "wb") as key_file:
        key_file.write(key)

    try:
        # reading the content of file
        json_files = open('json_list.txt', 'r')
        fernet = Fernet(key)
        for file_json in json_files.readlines():
            if not file_json.startswith("#"):
                try:
                    opened_json = open(file_json.strip())
                    json_out = str(json.load(opened_json)).replace('\'', '\"')
                    data = readfromstring(json_out)
                    xml_out = json2xml.Json2xml(data,
                                                wrapper="all",
                                                pretty=True,
                                                attr_type=False).to_xml()
                    xml_out_encode = xml_out.encode()
                    xml_encrypted = fernet.encrypt(xml_out_encode)
                    xml_content['id'] = str(count_files)
                    xml_content['file'] = str(xml_encrypted)
                    xml_content['filename'] = file_json.strip().replace(
                        'json', 'xml')
                    xml_content_arr.append(xml_content.copy())
                    count_files = count_files + 1
                except:
                    print("File - " + file_json + " cannot be opened")
                    exit()
Пример #24
0
 def test_read_from_invalid_jsonstring(self):
     with pytest.raises(StringReadError) as pytest_wrapped_e:
         data = readfromstring(
             '{"login":"******","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"'
         )
     assert pytest_wrapped_e.type == StringReadError