title: Getting AttributeError: 'NoneType' object has no attribute 'strip' while reading XML
body: <p>I am reading XML and adding xml content in dictionary but getting AttributeError: 'NoneType' object has no attribute 'strip' whenever there are empty tags.</p>
  <p>a.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;?xml-stylesheet href=&quot;population.xsl&quot; type=&quot;text/xsl&quot;?&gt;
  &lt;!DOCTYPE catalog SYSTEM &quot;catalog.dtd&quot;&gt;
  &lt;population&gt;
     &lt;human description=&quot;male&quot; product_image=&quot;male.jpg&quot;&gt;
        &lt;gender sex=&quot;Men&quot;&gt;
           &lt;id_number&gt;RRX9856&lt;/id_number&gt;
           &lt;weight&gt;&lt;/weight&gt;
        &lt;/gender&gt;     
     &lt;/human&gt;
  &lt;/population&gt;
  </code></pre>
  <p>Code:
  Whenever this code is running, it is throwing mentioned attribute error as there are empty tags i.e x.text() is None.</p>
  <pre><code>from lxml import etree
  from collections import defaultdict
  
  root_1 = etree.parse('a.xml').getroot()
  
  d1 = []
  for node in root_1.findall('.//human '):
      item = defaultdict(list)
      for x in node.iter():
          if x.attrib:
              item[x.attrib.keys()[0]].append(x.attrib.values()[0])
          if x.text.strip():
              item[x.tag].append(x.text.strip())
      d1.append(dict(item))
  
  
  
  d1 = sorted(d1, key = lambda x: x['gender'])
  print(d1)
  
  </code></pre>
  <p>Solution I tried:</p>
  <p>I am reading above XML and replacing empty value between tags to None by using below code and saving it to some different b.xml. But this is double work where I am reading original a.xml and then replacing empty values with string None and then saving it to b.xml and then reading the new xml file.</p>
  <pre><code>&quot;%s&quot; % x.text()
  </code></pre>
  <p>Is there any other solution which can handle this error in original XML only and there won't be any problem in reading this XML and all the elements including empty values can be also fetched in the results?</p>