title: Parsing XML file with elementtree: 'NoneType' object has no attribute 'text'
body: <p>I'm trying to parse information from an XML file using ElementTree in Python 2.7.</p>
  
  <p>The XML file has the following structure:</p>
  
  <pre><code> &lt;EXPERIMENT_PACKAGE_SET&gt;
      &lt;EXPERIMENT_PACKAGE&gt;
          &lt;SAMPLE&gt;
              &lt;IDENTIFIERS&gt;
                  &lt;PRIMARY_ID&gt;ERS1486582&lt;/PRIMARY_ID&gt;
                  &lt;EXTERNAL_ID&gt;SAMEA36350668&lt;/EXTERNAL_ID&gt;
              &lt;/IDENTIFIERS&gt;
              &lt;SAMPLE_ATTRIBUTES&gt;
                  &lt;SAMPLE_ATTRIBUTE&gt;
                      &lt;TAG&gt;collection date&lt;/TAG&gt;
                      &lt;VALUE&gt;2011&lt;/VALUE&gt;
                  &lt;/SAMPLE_ATTRIBUTE&gt;
                  &lt;SAMPLE_ATTRIBUTE&gt;
                      &lt;TAG&gt;geographic location&lt;/TAG&gt;
                      &lt;VALUE&gt;USA&lt;/VALUE&gt;
                  &lt;/SAMPLE_ATTRIBUTE&gt;
                  ...
              &lt;/SAMPLE_ATTRIBUTES&gt;
          &lt;/SAMPLE&gt;
      ...
      &lt;/EXPERIMENT_PACKAGE&gt;
  &lt;/EXPERIMENT_PACKAGE_SET&gt;
  </code></pre>
  
  <p>It contains many "Sample" modules that I want to loop over, parse some information from the "Identifiers" module and then loop again within their list of "Sample attributes" to store them in a file, including for each of them both their "Tag" and "Value". These data are not standardized (ie the attributes' tags don't always match) so I want to output all of them for each Sample. I wrote this piece of code that worked with other XML files with the same structure:</p>
  
  <pre><code># import the XML file
  e = xml.etree.ElementTree.parse('file.xml').getroot()
  # create output file
  file = open('output.txt','w')
  # loop over all "Sample" modules
  for i in range(0,len(e.findall('EXPERIMENT_PACKAGE/SAMPLE'))):
      node = e.findall('EXPERIMENT_PACKAGE/SAMPLE')[i]
      accession = node.find('IDENTIFIERS/PRIMARY_ID').text
      sample = node.find('IDENTIFIERS/EXTERNAL_ID').text                
      # loop over the list of attributes and print 'accession', 'sample', and for each attribute, 'tag' and 'value'   
      for attribute in node.findall('SAMPLE_ATTRIBUTES/SAMPLE_ATTRIBUTE'):
             file.write(accession + '\t' + sample + '\t' + str(attribute.find('TAG').text) + '\t' + 
             str(attribute.find('VALUE').text) + '\n')
  file.close()
  </code></pre>
  
  <p>This code worked in the past, and it should give an output such as:</p>
  
  <pre><code>&gt; ERS1486582    SAMEA36350668   collection date 2011
  &gt; ERS1486582    SAMEA36350668   geographic location USA 
  &gt; ...
  </code></pre>
  
  <p>But with this particular XML file is giving me an error:</p>
  
  <blockquote>
    <p>AttributeError: 'NoneType' object has no attribute 'text'</p>
  </blockquote>
  
  <p>I guess some element has None value? How could I try to check if the elements are not NoneType before writing out their values? I've tried adding an 'if' condition like: </p>
  
  <pre><code>if attribute.find('VALUE').text is not None:
  </code></pre>
  
  <p>within the second 'for' loop (before writing in the output file) but it's not working either. Thanks very much for your help and sorry if I didn't express myself clearly.</p>