title: Python: Parsing through XML with mini dom
body: <p>I'm parsing through a decent sized xml file, and I ran into a problem. For some reason I cannot extract data even though I have done the <em>exact same thing</em> on different xml files before.</p>
  
  <p>Here's a snippet of my code: (rest of the program, I've tested and they work fine)
  EDIT: changed to include a testing try&amp;except block</p>
  
  <pre><code>def parseXML():
      file = open(str(options.drugxml),'r')
      data = file.read()
      file.close()
      dom = parseString(data)
      druglist = dom.getElementsByTagName('drug')
  
      with codecs.open(str(options.csvdata),'w','utf-8') as csvout, open('DrugTargetRel.csv','w') as dtout:
          for entry in druglist:
          count = count + 1
          try:
              drugtype = entry.attributes['type'].value
              print count
          except:
              print count
              print entry
              drugidObj = entry.getElementsByTagName('drugbank-id')[0]
              drugid = drugidObj.childNodes[0].nodeValue
              drugnameObj = entry.getElementsByTagName('name')[0]
              drugname = drugnameObj.childNodes[0].nodeValue
  
              targetlist = entry.getElementsByTagName('target')
              for target in targetlist:
                  targetid = target.attributes['partner'].value
                  dtout.write((','.join((drugid,targetid)))+'\n')
  
              csvout.write((','.join((drugid,drugname,drugtype)))+'\n')
  </code></pre>
  
  <p>In case you're wondering what the XML file's schema roughly looks like, here's a rough god-awful sketch of the levels:</p>
  
  <pre><code>&lt;drugs&gt;
     &lt;drug type='something' ...&gt;
        &lt;drugbank-id&gt;
        &lt;name&gt;
        ...
        &lt;targets&gt;
           &lt;target partner='something'&gt;
  </code></pre>
  
  <p>Those that I typed in here, I need to extract from the XML file and stick it in csv files (as the code above shows), and the code has worked for different xml files before, not sure why it's not working on this one. I've gotten KeyError on 'type', I've also gotten indexing errors on line that extracts drugid even though EVERY drug has a drugid. What am I screwing up here?</p>
  
  <p>EDIT: the stuff I'm extracting are guaranteed to be in each drug.</p>
  
  <p>For anyone who cares, here's the link to the XML file I'm parsing:
  <a href="http://www.drugbank.ca/system/downloads/current/drugbank.xml.zip" rel="nofollow">http://www.drugbank.ca/system/downloads/current/drugbank.xml.zip</a></p>
  
  <p>EDIT: After implementing a try &amp; except block (see above) here's what I found out:
  In the schema, there are sections called "drug interactions" that <em>also</em> have a subfield called <em>drug</em>. So like this:</p>
  
  <pre><code> &lt;drugs&gt;
         &lt;drug type='something' ...&gt;
            &lt;drugbank-id&gt;
            &lt;name&gt;
            ...
            &lt;targets&gt;
               &lt;target partner='something'&gt;
            &lt;drug-interactions&gt;
               &lt;drug&gt;
  </code></pre>
  
  <p>I think that my line druglist = dom.getElementsByTagName('drug') is unintentionally picking those up as well -- I don't know how I could fix this... any suggestions?</p>