XML File:
<testcases>
<mode>PRESSURE_CONTROL</mode>
<category>ADULT</category>
<testcase id="1">
<parameter id="PEEP" value="1.000000">false</parameter>
<parameter id="CMV_FREQ" value="4.0">false</parameter>
<parameter id="PRESS_ABOVE_PEEP" value="0.0">true</parameter>
<parameter id="I_E_RATIO" value="0.100000">false</parameter>
</testcase>
</testcases>
Python code:
import xml.etree.ElementTree as ET
tree = ET.parse('Results.xml')
root = tree.getroot()
mode = root.find('Mode').text
category = root.find('Category').text
self.tag_invalid = ET.SubElement(root, 'invalid') # For adding new tag with attributes and values
for v in self.final_result:
self.tag_testcase = ET.SubElement(self.tag_invalid, 'testcase')
self.tag_testcase.attrib['id'] = 5
self.tag_testcase.attrib['parameter'] = 'IE'
self.tag_testcase.text = 100
tree.write('/home/AlAhAb65/Desktop/test.xml')
Output:
<testcases>
<mode>PRESSURE_CONTROL</mode>
<category>ADULT</category>
<testcase id="1">
<parameter id="PEEP" value="1.000000">false</parameter>
<parameter id="CMV_FREQ" value="4.0">false</parameter>
<parameter id="PRESS_ABOVE_PEEP" value="0.0">true</parameter>
<parameter id="I_E_RATIO" value="0.100000">false</parameter>
</testcase>
<invalid><testcase id="5" parameter="I_E_RATIO">100.0</testcase></invalid></testcases> # Extra line after python code running
The extra line is added in the XML file. But the problem is I cannot format it. That means I cannot add '\n', '\t' to maintain the hiererchy and format. Is there any rule for that? I tried tree.write(), ET.Element() functions. But those do not provide the desired result.
<a><b>...</b></a>is a different hierarchy than<a>.</a><b>..</b>.)