Convert Deeply Nested XML to CSV in Python
<p>I'm new to Python and have heard that it is one of the best ways to parse fairly large XML files (150MB). I can't get my head around how to iterate through the tags and extract only the <code>&lt;hw&gt;</code> and <code>&lt;defunit&gt;</code> tags as it's fairly deeply nested.</p>

<p>I have some XML formatted as below, and I need to extract the "hw" and "defunit" tags from it using Python and convert them into a .csv format.</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;dps-data xmlns="urn:DPS2-metadata" project="SCRABBLELARGE" guid="7d6b7164fde1e064:34368a61:14306b637ab:-8000--4a25ae5c-c104-4c7a-bba5-b434dd4d9314"&gt;
    &lt;superentry xmlns="urn:COLL" xmlns:d="urn:COLL" xmlns:e="urn:IDMEE" e:id="u583c10bfdbd326ba.31865a51.12110e76de1.-336"&gt;
        &lt;entry publevel="1" id="a000001" e:id="u583c10bfdbd326ba.31865a51.12110e76de1.-335"&gt;
            &lt;hwblk&gt;
                &lt;hwgrp&gt;
                    &lt;hwunit&gt;
                        &lt;hw&gt;aa&lt;/hw&gt;
                        &lt;ulsrc&gt;edsh&lt;/ulsrc&gt;
                    &lt;/hwunit&gt;
                &lt;/hwgrp&gt;
            &lt;/hwblk&gt;
            &lt;datablk&gt;
                &lt;gramcat publevel="1" id="a000001.001"&gt;
                    &lt;pospgrp&gt;
                        &lt;pospunit&gt;
                            &lt;posp value="noun" /&gt;
                        &lt;/pospunit&gt;
                    &lt;/pospgrp&gt;
                    &lt;sensecat id="a000001.001.01" publevel="1"&gt;
                        &lt;defgrp&gt;
                            &lt;defunit&gt;
                                &lt;def&gt;volcanic rock&lt;/def&gt;
                            &lt;/defunit&gt;
                        &lt;/defgrp&gt;
                    &lt;/sensecat&gt;
                &lt;/gramcat&gt;
            &lt;/datablk&gt;
        &lt;/entry&gt;
    &lt;/superentry&gt;
  &lt;/dps-data&gt;
</code></pre>

<p>The .csv format I'd like to see it in is simply:</p>

<pre><code>hw, defunit
aa, volcanic rock
</code></pre>