title: xml formatting with python Elementtree
body: <p>XML File:</p>
  
  <pre><code>&lt;testcases&gt;
      &lt;mode&gt;PRESSURE_CONTROL&lt;/mode&gt;
      &lt;category&gt;ADULT&lt;/category&gt;
      &lt;testcase id="1"&gt;
          &lt;parameter id="PEEP" value="1.000000"&gt;false&lt;/parameter&gt;
          &lt;parameter id="CMV_FREQ" value="4.0"&gt;false&lt;/parameter&gt;
          &lt;parameter id="PRESS_ABOVE_PEEP" value="0.0"&gt;true&lt;/parameter&gt;
          &lt;parameter id="I_E_RATIO" value="0.100000"&gt;false&lt;/parameter&gt;
      &lt;/testcase&gt;
  &lt;/testcases&gt;
  </code></pre>
  
  <p>Python code:</p>
  
  <pre><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')
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;testcases&gt;
      &lt;mode&gt;PRESSURE_CONTROL&lt;/mode&gt;
      &lt;category&gt;ADULT&lt;/category&gt;
      &lt;testcase id="1"&gt;
          &lt;parameter id="PEEP" value="1.000000"&gt;false&lt;/parameter&gt;
          &lt;parameter id="CMV_FREQ" value="4.0"&gt;false&lt;/parameter&gt;
          &lt;parameter id="PRESS_ABOVE_PEEP" value="0.0"&gt;true&lt;/parameter&gt;
          &lt;parameter id="I_E_RATIO" value="0.100000"&gt;false&lt;/parameter&gt;
      &lt;/testcase&gt;
  &lt;invalid&gt;&lt;testcase id="5" parameter="I_E_RATIO"&gt;100.0&lt;/testcase&gt;&lt;/invalid&gt;&lt;/testcases&gt;  # Extra line after python code running
  </code></pre>
  
  <p>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.</p>