title: Using Python 3.6 to parse XML how can I determine if an XML tag contains no data
body: <p>I am trying to learn Python by writing a script that will extract data from multiple records in an XML file. I have been able to find the answers to most of my questions by searching on the web, but I have not found a way to determine if an XML tag contains no data before the getElementsByTagName("<em>tagname</em>")[0].firstChild.data method is used and an AttributeError is thrown when no data is present. I realize that I could write my code with a try and handle the AttributeError but I would rather know that the tag is empty before I try to extract the data an not have to handle the exception.
  Here is an example of an XML file that contains two records one with data in the tags and one with an empty tag.</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt;
  &lt;records&gt;
    &lt;rec&gt;
      &lt;name&gt;ZYSRQPO&lt;/name&gt;
      &lt;state&gt;Washington&lt;/state&gt;
      &lt;country&gt;United States&lt;/country&gt;
    &lt;/rec&gt;
    &lt;rec&gt;
      &lt;name&gt;ZYXWVUT&lt;/name&gt;
      &lt;state&gt;&lt;/state&gt;
      &lt;country&gt;Mexico&lt;/country&gt;
    &lt;/rec&gt;
  &lt;/records&gt;
  </code></pre>
  
  <p>Here is a sample of the code that I might use to extract the data:</p>
  
  <pre><code>from xml.dom import minidom
  import sys
  
  mydoc  = minidom.parse('mydataFile.xml')
  records = mydoc.getElementsByTagName("rec")
  
  for rec in records:
      try:
          name = rec.getElementsByTagName("name")[0].firstChild.data
          state = rec.getElementsByTagName("state")[0].firstChild.data
          country = rec.getElementsByTagName("country")[0].firstChild.data
          print('{}\t{}\t{}'.format(name, state, country))
  
      except (AttributeError):
          print('AttributeError encountered in record {}'.format(name), file=sys.stderr)
          continue
  </code></pre>
  
  <p>When processing this file no information for the record named ZYXWVUT will be printed except that an exception was encountered. I would like to be able to have a null value for the state name used and the rest of the information printed about this record. Is there a method that can be used to do what I want, so that I could use an if statement to determine whether the  tag contained no data before using getElementsByTagName and encountering an error when no data is found?</p>