Skip to content

Commit

Permalink
Added initial XML script and recursive function to iterate though nes…
Browse files Browse the repository at this point in the history
…ted dictionary
  • Loading branch information
GersonEsquivel committed Nov 7, 2023
1 parent ef67df3 commit 16320d0
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
56 changes: 56 additions & 0 deletions XML_to_SON/Automated-XMLtoString-Schema.py
Original file line number Diff line number Diff line change
@@ -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)
181 changes: 181 additions & 0 deletions XML_to_SON/schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>

<element name="simulation">
<optional><element name="schematype"><text/></element></optional>
<interleave>

<optional><element name="ui"><text/></element></optional>

<element name ="control">
<interleave>
<optional>
<element name="simhandle"> <data type="string"/> </element>
</optional>
<element name="duration"> <data type="nonNegativeInteger"/> </element>
<element name="startmonth"> <data type="nonNegativeInteger"/> </element>
<element name="startyear"> <data type="nonNegativeInteger"/> </element>
<optional>
<element name="decay"> <text/> </element>
</optional>
<optional>
<element name="dt"><data type="nonNegativeInteger"/></element>
</optional>
<optional>
<element name="explicit_inventory"> <data type="boolean"/> </element>
</optional>
<optional>
<element name="explicit_inventory_compact"> <data type="boolean"/> </element>
</optional>
<optional>
<element name="tolerance_generic"><data type="double"/></element>
</optional>
<optional>
<element name="tolerance_resource"><data type="double"/></element>
</optional>
<optional>
<element name="solver">
<interleave>
<optional><element name="config">
<choice>
<element name="greedy">
<interleave>
<optional>
<element name="preconditioner"> <text/> </element>
</optional>
</interleave>
</element>
<element name="coin-or">
<interleave>
<optional>
<element name="timeout"> <data type="positiveInteger"/> </element>
</optional>
<optional><element name="verbose"><data type="boolean"/></element></optional>
<optional><element name="mps"><data type="boolean"/></element></optional>
</interleave>
</element>
</choice>
</element></optional>
<optional>
<element name="allow_exclusive_orders">
<data type="boolean" />
</element>
</optional>
<optional><!--deprecated. @TODO remove in release 1.5 -->
<element name="exclusive_orders_only">
<data type="boolean" />
</element>
</optional>
</interleave>
</element>
</optional>
</interleave>
</element>

<zeroOrMore>
<element name="commodity">
<interleave>
<element name="name"> <text/> </element>
<element name="solution_priority"> <data type="double"/> </element>
</interleave>
</element>
</zeroOrMore>

<element name="archetypes">
<oneOrMore>
<element name="spec">
<interleave>
<optional><element name="path"><text/></element></optional>
<optional><element name="lib"><text/></element></optional>
<element name="name"><text/></element>
<optional><element name="alias"><text/></element></optional>
</interleave>
</element>
</oneOrMore>
</element>

<oneOrMore>
<element name="facility">
<interleave>
<element name="name"> <text/> </element>
<optional>
<element name="lifetime"> <data type="nonNegativeInteger"/> </element>
</optional>

<element name="config">
<choice>
@Facility_REFS@
</choice>
</element>
</interleave>
</element>
</oneOrMore>

<oneOrMore>
<element name="region"> <interleave>
<element name="name"> <text/> </element>
<optional>
<element name="lifetime"> <data type="nonNegativeInteger"/> </element>
</optional>

<element name="config">
<choice>
@Region_REFS@
</choice>
</element>

<oneOrMore>
<element name="institution"> <interleave>
<element name="name"> <text/> </element>
<optional>
<element name="lifetime"> <data type="nonNegativeInteger"/> </element>
</optional>

<optional>
<element name="initialfacilitylist">
<oneOrMore>
<element name="entry">
<interleave>
<element name="prototype"> <text/> </element>
<element name="number"> <data type="nonNegativeInteger"/> </element>
</interleave>
</element>
</oneOrMore>
</element>
</optional>

<element name="config">
<choice>
@Inst_REFS@
</choice>
</element>
</interleave> </element>
</oneOrMore>

</interleave> </element>
</oneOrMore>

<zeroOrMore>
<element name="recipe">
<interleave>
<element name="name"><text/></element>
<element name="basis"><text/></element>
<oneOrMore>
<element name="nuclide">
<interleave>
<element name="id"><data type="string"/></element>
<element name="comp"><data type="double"/></element>
</interleave>
</element>
</oneOrMore>
</interleave>
</element>
</zeroOrMore>

</interleave> </element>

</start>

</grammar>

0 comments on commit 16320d0

Please sign in to comment.