Python XML AttributeError: 'NoneType' object has no attribute 'text'
<p>I am having a problem figuring out why I receive the error below</p>

<pre><code>AttributeError: 'NoneType' object has no attribute 'text'
</code></pre>

<p>I am trying to import a XML file using Python 2.7. Below is what my XML file looks like.</p>

<pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;!DOCTYPE dblp SYSTEM "file.dtd"&gt;

&lt;top&gt;
    &lt;blue key="2343998978"&gt;
        &lt;animal&gt;lion&lt;/animal&gt;
        &lt;animal&gt;seal&lt;/animal&gt;
        &lt;state&gt;california&lt;/state&gt;
        &lt;zoo&gt;san diego&lt;/zoo&gt;
        &lt;year&gt;2015&lt;/year&gt;
    &lt;/blue&gt;

    &lt;red key="9383893838739"&gt;
        &lt;elem_a&gt;jennifer&lt;/elem_a&gt;
        &lt;elem_a&gt;paul&lt;/elem_a&gt;
        &lt;elem_a&gt;carl&lt;/elem_a&gt;
        &lt;elem_b&gt;kansas&lt;/elem_b&gt;
        &lt;elem_d&gt;australia&lt;/elem_d&gt;
    &lt;/red&gt;

    &lt;yellow key="83963277272"&gt;
        &lt;car&gt;chevy&lt;/car&gt;
        &lt;car&gt;dodge&lt;/car&gt;
        &lt;cap&gt;baseball&lt;/cap&gt;
        &lt;cat&gt;tabby&lt;/cat&gt;
    &lt;/yellow&gt;

    &lt;red key="9383893838739"&gt;
        &lt;elem_a&gt;greg&lt;/elem_a&gt;
        &lt;elem_a&gt;chris&lt;/elem_a&gt;
        &lt;elem_a&gt;john&lt;/elem_a&gt;
        &lt;elem_b&gt;arkansas&lt;/elem_b&gt;
        &lt;elem_c&gt;ice cream&lt;/elem_c&gt;
    &lt;/red&gt;

    &lt;yellow key="84748346734"&gt;
        &lt;car&gt;toyota&lt;/car&gt;
        &lt;car&gt;honda&lt;/car&gt;
        &lt;cap&gt;football&lt;/cap&gt;
    &lt;/yellow&gt;
&lt;/top&gt;
</code></pre>

<p>I am new to Python but created the script below to import the XML file above and that is when I receive the error above. Below is my code.</p>

<pre><code>import xml.etree.ElementTree as ET

myfile = 'C:/Users/user1/Desktop/file.xml'

tree = ET.parse(myfile)
root = tree.getroot()

for x in root.findall('blue'):
    animal = x.find('animal').text
    key1 = x.attrib['key']
    state = x.find('state').text
    zoo = x.find('zoo').text
    year = x.find('year').text
    print animal, key1, state, zoo, year

for y in root.findall('red'):
    elem_a = y.find('elem_a').text
    key2 = y.attrib['key']
    elem_b = y.find('elem_b').text
    elem_c = y.find('elem_c').text
    elem_d = y.find('elem_d').text
    print elem_a, key2, elem_b, elem_c, elem_d

for z in root.findall('yellow'):
    car = z.find('car').text
    key3 = z.attrib['key']
    cap = z.find('cap').text
    cat = z.find('cat').text
    print car, key3, cap, cat
</code></pre>

<p>In the XML file there are three main element types: blue, red and yellow. One of the problems specific child elements exist for some parent elements are not for others. For example, in the sample XML file above, one "yellow" element has three child elements including "car", "cat" and "cap" but not each "yellow" element has all three child elements. In the XML below the first "yellow" element has the "cat" child node and the second "yellow" element does not have the "cat" child element but in the full XML file the "yellow" elements could have any one, two or three of the "cat", "cap" and "car" child elements. I know this is causing the error but I do not know how to resolve it. Does anyone have any ideas or tips as to how to resolve this error? Thank you.</p>