Answer the question
In order to leave comments, you need to log in
Why is xml formatting messed up when using ElementTree.write()?
There is an xml config file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Cache>
<Index ID="DATE_INDEX">
<FieldName Name="id"/>
<FieldName Name="start_time"/>
<FieldName Name="stop_time"/>
</Index>
<Index ID="USER_INDEX">
<FieldName Name="id"/>
<FieldName Name="username"/>
</Index>
</Cache>
<Regions>
<Region ID="0" Folder="Unknown"/>
<Region ID="100" Folder="MSK"/>
<Region ID="200" Folder="SPB"/>
<Region ID="300" Folder="PTZ"/>
</Regions>
</Configuration>
config_file = 'config.xml'
xml_config = ET.parse(config_file)
root = xml_config.getroot()
root[1][1].attrib['Folder'] = 'Capital'
xml_config.write('out.xml')
<Regions>
<Region Folder="Unknown" ID="0" />
<Region Folder="Capital" ID="100" />
<Region Folder="SPB" ID="200" />
<Region Folder="PTZ" ID="300" />
</Regions>
ET.SubElement(root[1], 'Region', {'Folder':"Ural", "ID":"400"})
<Regions>
<Region Folder="Unknown" ID="0" />
<Region Folder="Capital" ID="100" />
<Region Folder="SPB" ID="200" />
<Region Folder="PTZ" ID="300" />
<Region Folder="Ural" ID="400" />
</Regions>
<Regions>
<Region Folder="Unknown" ID="0" />
<Region Folder="Capital" ID="100" />
<Region Folder="SPB" ID="200" />
<Region Folder="PTZ" ID="300" />
<Region Folder="Ural" ID="400" /></Regions>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question