From 16320d0d687e7715d5f7062cd5febb6720a2b0bf Mon Sep 17 00:00:00 2001 From: GersonEsquivel Date: Tue, 7 Nov 2023 15:59:17 -0600 Subject: [PATCH] Added initial XML script and recursive function to iterate though nested dictionary --- XML_to_SON/Automated-XMLtoString-Schema.py | 56 +++++++ XML_to_SON/schema.xml | 181 +++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 XML_to_SON/Automated-XMLtoString-Schema.py create mode 100644 XML_to_SON/schema.xml diff --git a/XML_to_SON/Automated-XMLtoString-Schema.py b/XML_to_SON/Automated-XMLtoString-Schema.py new file mode 100644 index 0000000..0c1c630 --- /dev/null +++ b/XML_to_SON/Automated-XMLtoString-Schema.py @@ -0,0 +1,56 @@ +# Import the required modules +import xmltodict +import pprint +import json + +# Open the file and read the contents +with open('schema.xml', 'r', encoding='utf-8') as file: + schema = file.read() + +# Use xmltodict to parse and convert the XML document +dictionary = xmltodict.parse(schema) +json_d = json.dumps(dictionary) + +def remove_at_symbol_from_keys(data): + if isinstance(data, dict): + updated_data = {} + for key, value in data.items(): + new_key = key[1:] if key.startswith('@') else key + updated_value = remove_at_symbol_from_keys(value) + updated_data[new_key] = updated_value + return updated_data + elif isinstance(data, list): + updated_list = [] + for item in data: + updated_item = remove_at_symbol_from_keys(item) + updated_list.append(updated_item) + return updated_list + else: + return data + +def reformat_tree(d, indent = 0): + d = remove_at_symbol_from_keys(d) + if isinstance(d, dict): + for key, value in d.items(): + match key: + case "@name": + key = "name" + case "@data": + key = "data" + case "@type": + key = "type" + case _: + key = key + if isinstance(value, (dict, list)): + print(' ' * indent + str(key) + ':') + reformat_tree(value, indent + 4) + else: + print(' ' * indent + str(key) + ': ' + str(value)) + elif isinstance(d, list): + for item in d: + if isinstance(item, (dict, list)): + reformat_tree(item, indent + 4) + else: + print(' ' * indent + '- ' + str(item)) + +reformat_tree(dictionary) diff --git a/XML_to_SON/schema.xml b/XML_to_SON/schema.xml new file mode 100644 index 0000000..78dd957 --- /dev/null +++ b/XML_to_SON/schema.xml @@ -0,0 +1,181 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @Facility_REFS@ + + + + + + + + + + + + + + + + @Region_REFS@ + + + + + + + + + + + + + + + + + + + + + + + + + + @Inst_REFS@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +