I have one main xml file (Mainfile_1.xml) where some items show value = 'FAIL'. I want to replace those Fail values with correct values from another XML file (Fixfile_1.xml). It should look like on the picture below:
![[Image: tYIVy.png]](https://i.stack.imgur.com/tYIVy.png)
So as you can see, values from Fixfile_1.xml should replace 'FAIL' values in Mainfile_1.xml for coresponding Item name and object id.
So far I wrote a code where I read both xml files and print only data related with Fail values. My MAIN PROBLEM is how to save it to a file so the failed values would be overwriten by values from fixfile_1.xml. "Tree.write" only delete "<?xml version='1.0' encoding='UTF-8'?>" line for some reason.
There is my code:
![[Image: tYIVy.png]](https://i.stack.imgur.com/tYIVy.png)
So as you can see, values from Fixfile_1.xml should replace 'FAIL' values in Mainfile_1.xml for coresponding Item name and object id.
So far I wrote a code where I read both xml files and print only data related with Fail values. My MAIN PROBLEM is how to save it to a file so the failed values would be overwriten by values from fixfile_1.xml. "Tree.write" only delete "<?xml version='1.0' encoding='UTF-8'?>" line for some reason.
There 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'): objabsno = objects.attrib.get('absno') for attributes in objects.getchildren(): name = attributes.attrib.get('name') value = attributes.attrib.get('value') if value == 'FAIL': for objects2 in root2.iter('object'): objabsno2 = objects2.attrib.get('absno') for attributes2 in objects2.getchildren(): name2 = attributes2.attrib.get('name') value2 = attributes2.attrib.get('value') if objabsno2 == objabsno: if name == name2: print(name,name2,value,value2) tree.write('newMainfile_1.xml')There is Mainfile_1.xml
<?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'/> <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 there is Fixfile_1.xml
<?xml version='1.0' encoding='UTF-8'?> <Module bs='Mainfile_1'> <object id='1234'> <item name='item1' value='something more of something'/> <item name='item4' value='something more of something'/> </object> <object id='1238'> <item name='item8' value='something12 more of something'/> </object> <object id='2345'> <item name='item2' value='something more of something'/> </object> <object id='2347'> <item name='item1' value='something14 more of something'/> <item name='item2' value='something more of something'/> <item name='item4' value='something14 something14 something12 more of something'/> </object> </Module>And there is one more thing!! Because I have a lot of coresponding files like that (Mainfile_1.xml - Fixfile_1.xml, Mainfile_2.xml - Fixfile_2.xml,Mainfile_3.xml - Fixfile_3.xml, etc.) is there a way to open and overwrite them all at once?