title: How to preserve namespaces when parsing xml via ElementTree in Python
body: <p>Assume that I've the following XML which I want to modify using Python's <code>ElementTree</code>:</p>
  <pre><code>&lt;root xmlns:prefix=&quot;URI&quot;&gt;
    &lt;child company:name=&quot;***&quot;/&gt;
    ...
  &lt;/root&gt; 
  </code></pre>
  <p>I'm doing some modification on the XML file like this:</p>
  <pre><code>import xml.etree.ElementTree as ET
  tree = ET.parse('filename.xml')
  # XML modification here
  # save the modifications
  tree.write('filename.xml')
  </code></pre>
  <p>Then the XML file looks like:</p>
  <pre><code>&lt;root xmlns:ns0=&quot;URI&quot;&gt;
    &lt;child ns0:name=&quot;***&quot;/&gt;
    ...
  &lt;/root&gt;
  </code></pre>
  <p>As you can see, the namepsace <code>prefix</code> changed to <code>ns0</code>. I'm aware of using <code>ET.register_namespace()</code> as mentioned <a href="https://stackoverflow.com/questions/25225934/xml-etree-elementtree-trouble-setting-xmlns">here</a>.</p>
  <p>The problem with <code>ET.register_namespace()</code> is that:</p>
  <ol>
  <li>You need to know <code>prefix</code> and <code>URI</code></li>
  <li>It can not be used with default namespace.</li>
  </ol>
  <p>e.g. If the xml looks like:</p>
  <pre><code>&lt;root xmlns=&quot;http://uri&quot;&gt;
      &lt;child name=&quot;name&quot;&gt;
      ...
      &lt;/child&gt;
  &lt;/root&gt;
  </code></pre>
  <p>It will be transfomed to something like:</p>
  <pre><code>&lt;ns0:root xmlns:ns0=&quot;http://uri&quot;&gt;
      &lt;ns0:child name=&quot;name&quot;&gt;
      ...
      &lt;/ns0:child&gt;
  &lt;/ns0:root&gt;
  </code></pre>
  <p>As you can see, the default namespace is changed to <code>ns0</code>.</p>
  <p>Is there any way to solve this problem with <code>ElementTree</code>?</p>