Hello, I made a program that takes one main XML file and update there attribute values = "Fail" based on other XML file attribute values. It works fine but I have one issue:
After parsing XML file it deletes whitespaces. Not only from the updated values but also from the main file. Is there any way to avoid it?
There is a picture showing someway what is my concerne:
![[Image: KTrht.png]](https://i.stack.imgur.com/KTrht.png)
I want to keep these values with more than one line.
Here is my code:
After parsing XML file it deletes whitespaces. Not only from the updated values but also from the main file. Is there any way to avoid it?
There is a picture showing someway what is my concerne:
![[Image: KTrht.png]](https://i.stack.imgur.com/KTrht.png)
I want to keep these values with more than one line.
Here is my code:
import xml.etree.ElementTree as ET
Mainfile = 'Mainfile_1.xml'
tree = ET.parse(Mainfile)
root = tree.getroot()
fixfile = 'fixfile_1.xml'
tree2 = ET.parse(fixfile)
root2 = tree2.getroot()
for objects in root.iter('object'):
objid = objects.attrib.get('id')
for attributes in objects.getchildren():
name = attributes.attrib.get('name')
value = attributes.attrib.get('value')
if value == 'FAIL':
for objects2 in root2.iter('object'):
objid2 = objects2.attrib.get('id')
for attributes2 in objects2.getchildren():
name2 = attributes2.attrib.get('name')
value2 = attributes2.attrib.get('value')
if objid2 == objid:
if name == name2:
attributes.set('value', value2)
tree.write('Mainfile_1updated.xml',xml_declaration=True, encoding='UTF-8')Here is MainXML:<?xml version='1.0' encoding='UTF-8'?>
<Module bs='Mainfile_1'>
<object name='namex' number='1' id='1000'>
<item name='item0' value='100'/>
<item name='item00' value='100'/>
</object>
<object name='namey' number='2' id='1001'>
<item name='item1' value='100'/>
<item name='item00' value='100'/>
</object>
<object name='name1' number='3' id='1234'>
<item name='item1' value='FAIL'/>
<item name='item2' value='233
233'/>
<item name='item3' value='233'/>
<item name='item4' value='FAIL'/>
</object>
<object name='name2' number='4' id='1238'>
<item name='item8' value='FAIL'/>
<item name='item9' value='233'/>
</object>
<object name='name32' number='5' id='2345'>
<item name='item1' value='111'/>
<item name='item2' value='FAIL'/>
</object>
<object name='name4' number='6' id='2347'>
<item name='item1' value='FAIL'/>
<item name='item2' value='FAIL'/>
<item name='item3' value='233'/>
<item name='item4' value='FAIL'/>
</object>
</Module>And here is fix file:<?xml version='1.0' encoding='UTF-8'?>
<Module bs='Mainfile_1'>
<object id='1234'>
<item name='item1' value='something
something111'/>
<item name='item4' value='something
1something'/>
</object>
<object id='1238'>
<item name='item8' value='something12
1something'/>
</object>
<object id='2345'>
<item name='item2' value='something
12something'/>
</object>
<object id='2347'>
<item name='item1' value='something14
13of something'/>
<item name='item2' value='something
11something'/>
<item name='item4' value='something14
something14
something12
13something'/>
</object>
</Module>
