Here is my input file.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="so_project" id="Project-9999">
<schema name="database1">
<table name="table1">
<column name="foo" type="int"/>
<column name="bar" type="string"/>
<column name="details_resolution" type="array[object]">
<column name="timestamp" type="timestamp"/>
<column name="user_id" type="string"/>
<column name="user_name" type="string"/>
</column>
<column name="details_closure" type="array[object]">
<column name="timestamp" type="timestamp"/>
<column name="auto_closure" type="bool"/>
</column>
</table>
</schema>
<schema name="database2">
<table name="table1">
<column name="foo" type="int"/>
<column name="bar" type="string"/>
<column name="details" type="array[object]">
<column name="timestamp" type="timestamp"/>
<column name="value" type="float"/>
</column>
</table>
</schema>
</project>
.. and I’m trying to make this classical nested dict :
{
"database1": {
"table1": {
"foo": "int",
"bar": "string",
"details_resolution": {
"timestamp": "timestamp",
"user_id": "string",
"user_name": "string"
},
"details_closure": {
"timestamp": "timestamp",
"auto_closure": "bool"
}
}
},
"database2": {
"table1": {
"foo": "int",
"bar": "string",
"details": {
"timestamp": "timestamp",
"value": "float"
}
}
}
}
PS : Each database can eventually have more than one table.
I tried some AI codes but none of them gave me the expected result..
I’m sorry guys to not being able to show my attempts !
SO, any help would be greately appreciated.
3
Answers
You can use
xml.etree.ElementTree
Solution using beautifulsoup:
Prints:
The simple way to do it using
xmltodict
module.Output: