title: iterparse fails to parse a field, while other similar ones are fine
body: <p>I use Python's <code>iterparse</code> to parse the XML result of a nessus scan (.nessus file). The parsing fails on unexpected records, wile similar ones have been parsed correctly.</p>
  
  <p>The general structure of the XML file is a lot of records like the one below:</p>
  
  <pre><code>&lt;ReportHost&gt;
    &lt;ReportItem&gt;
      &lt;foo&gt;9.3&lt;/foo&gt;
      &lt;bar&gt;hello&lt;/bar&gt;
    &lt;/ReportItem&gt;
    &lt;ReportItem&gt;
       &lt;foo&gt;10.0&lt;/foo&gt;
       &lt;bar&gt;world&lt;/bar&gt;
  &lt;/ReportHost&gt;
  &lt;ReportHost&gt;
     ...
  &lt;/ReportHost&gt;
  </code></pre>
  
  <p>In other words a lot of hosts (<code>ReportHost</code>) with a lot of items to report (<code>ReportItem</code>), and the latter having several characteristics (<code>foo</code>, <code>bar</code>). I will be looking at generating one line per item, with its characteristics.</p>
  
  <p>The parsing fails in the middle of the file at a simple line (<code>foo</code> in that case being <code>cvss_base_score</code>)</p>
  
  <pre><code>&lt;cvss_base_score&gt;9.3&lt;/cvss_base_score&gt;
  </code></pre>
  
  <p>while ~200 similar lines have been parsed without problems.</p>
  
  <p>The relevant piece of code is below -- it sets context markers (<code>inReportHost</code> and <code>inReportEvent</code> which tell me where in the stricture of the XML file I am in, and either assign or print a value, depending on the context)</p>
  
  <pre><code>import xml.etree.cElementTree as ET
  inReportHost = False
  inReportItem = False
  
  for event, elem in ET.iterparse("test2.nessus", events=("start", "end")):
      if event == 'start' and elem.tag == "ReportHost":
          inReportHost = True
      if event == 'end' and elem.tag == "ReportHost":
          inReportHost = False
          elem.clear()
      if inReportHost:
          if event == 'start' and elem.tag == 'ReportItem':
              inReportItem = True
              cvss = ''
          if event == 'start' and inReportItem:
              if event == 'start' and elem.tag == 'cvss_base_score':
                  cvss = elem.text
          if event == 'end' and elem.tag == 'ReportItem':
              print cvss
              inReportItem = False
  </code></pre>
  
  <p><code>cvss</code> sometimes has the None value (after the <code>cvss = elem.text</code> assignment), even though identical entries have been parsed properely earlier in the file.</p>
  
  <p>If I add below the assignement something along the lines of</p>
  
  <pre><code>if cvss is None: cvss = "0"
  </code></pre>
  
  <p>then the parsing of many further <code>cvss</code> assign their proper values (and some other are None).</p>
  
  <p>When taking the <code>&lt;ReportHost&gt;...&lt;/reportHost&gt;</code> which causes the wrong parsing and running it through the program - it works fine (ie. <code>cvss</code> is assigned <code>9.3</code> as expected).</p>
  
  <p>I am lost at where I make a mistake in my code since, withing a large set of similar records, some apre processed correctly and some - not (some of the records are identical, and still are processed differently). I also cannot find anything particular about the records that fail - identical ones earlier and later are fine.</p>