I have an element in an XML file as:
<condition>
<comparison compare="and">
<operand idref="XXX" type="boolean" />
</comparison>
</condition>
I need to add two other child elements (child1 and child2) such as:
<condition>
<child1 compare='and'>
<child2 idref='False' type='int' />
<comparison compare="and">
<operand idref="XXX" type="boolean" />
</comparison>
</child1>
</condition>
I proceeded using lxml:
from lxml import etree
tree = etree.parse(xml_file)
condition_elem = tree.find("<path for the condition block in the xml>")
etree.SubElement(condition_elem, 'child1')
tree.write( 'newXML.xml', encoding='utf-8', xml_declaration=True)
This just adds the element child1 as a child of the element condition as below and did not satisfy my requirement:
<condition>
<child1></child1>
<comparison compare="and">
<operand idref="XXX" type="boolean" />
</comparison>
</condition>
Any ideas? Thanks