My 1st XML contain values as:
<Price>
<Id>100</Id>
<Id>102</Id>
<Id>104</Id>
<Id>103</Id></Price>
These value of Price ID which is present as child element needs to be searched in 2nd XML which has format as:
<List>
<Item>
<Number>1</Number>
<Price>
<Id>100</Id>
<Next_Item>
<Name>Apple</Name>
<Necessary/>
</Next_Item>
<Next_Item>
<Name>Orange</Name>
<Necessary/>
</Next_Item>
<Price>
</Item>
<Item>
<Number>2</Number>
<Price>
<Id>102</Id>
<Next_Item>
<Name>Apple</Name>
<Necessary/>
</Next_Item>
<Next_Item>
<Name>Orange</Name>
<Necessary/>
</Next_Item>
<Price>
</Item>
Upon finding the equivalent Price tag I need to change the child tag to -->
<List>
<Item>
<Number>1</Number>
<Price>
<Id>100</Id>
<Next_Item>
<Name>Apple</Name>
<!--<Necessary/>-->
</Next_Item>
<Next_Item>
<Name>Orange</Name>
<!--<Necessary/>-->
</Next_Item>
<Price>
</Item>
<Item>
<Number>2</Number>
<Price>
<Id>102</Id>
<Next_Item>
<Name>Apple</Name>
<!--<Necessary/>-->
</Next_Item>
<Next_Item>
<Name>Orange</Name>
<!--<Necessary/>-->
</Next_Item>
<Price>
</Item>
My Code:
from xml.etree import ElementTree as et
tree = et.parse('Sample.xml')
root = tree.getroot()
for Price in root:
print({x.tag for x in root.findall(Price.tag + "/*")})
tree.find('.//Necessary').text = '<Necessary/>'
tree.find('.//Necessary').text = '<!--Necessary-->'
tree.write('Output1.xml')
In my code I am not able to find value from another file to my 2nd file and I am not able to change format of all the equivalent Ids to <!--Necessary-->
I would be grateful if anyone can help me.