title: ElementTree iterparse strategy
body: <p>I have to handle xml documents that are big enough (up to 1GB) and parse them with python. I am using the <a href="http://effbot.org/zone/element-iterparse.htm" rel="noreferrer">iterparse()</a> function (SAX style parsing).</p>
  
  <p>My concern is the following, imagine you have an xml like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;families&gt;
    &lt;family&gt;
      &lt;name&gt;Simpson&lt;/name&gt;
      &lt;members&gt;
          &lt;name&gt;Homer&lt;/name&gt;
          &lt;name&gt;Marge&lt;/name&gt;
          &lt;name&gt;Bart&lt;/name&gt;
      &lt;/members&gt;
    &lt;/family&gt;
    &lt;family&gt;
      &lt;name&gt;Griffin&lt;/name&gt;
      &lt;members&gt;
          &lt;name&gt;Peter&lt;/name&gt;
          &lt;name&gt;Brian&lt;/name&gt;
          &lt;name&gt;Meg&lt;/name&gt;
      &lt;/members&gt;
    &lt;/family&gt;
  &lt;/families&gt;
  </code></pre>
  
  <p>The problem is, of course to know when I am getting a family name (as Simpsons) and when I am getting the name of one of that family member (for example Homer)</p>
  
  <p>What I have been doing so far is to use "switches" which will tell me if I am inside a "members" tag or not, the code will look like this</p>
  
  <pre><code>import xml.etree.cElementTree as ET
  
  __author__ = 'moriano'
  
  file_path = "test.xml"
  context = ET.iterparse(file_path, events=("start", "end"))
  
  # turn it into an iterator
  context = iter(context)
  on_members_tag = False
  for event, elem in context:
      tag = elem.tag
      value = elem.text
      if value :
          value = value.encode('utf-8').strip()
  
      if event == 'start' :
          if tag == "members" :
              on_members_tag = True
  
          elif tag == 'name' :
              if on_members_tag :
                  print "The member of the family is %s" % value
              else :
                  print "The family is %s " % value
  
      if event == 'end' and tag =='members' :
          on_members_tag = False
      elem.clear()
  </code></pre>
  
  <p>And this works fine as the output is </p>
  
  <pre><code>The family is Simpson 
  The member of the family is Homer
  The member of the family is Marge
  The member of the family is Bart
  The family is Griffin 
  The member of the family is Peter
  The member of the family is Brian
  The member of the family is Meg
  </code></pre>
  
  <p>My concern is that with this (simple) example i had to create an extra variable to know in which tag i was (on_members_tag) imagine with the true xml examples that I have to handle, they have more nested tags. </p>
  
  <p>Also note that this is a very reduced example, so you can assume that i may be facing an xml with more tags, more inner tags and trying to get different tag names, attributes and so on.</p>
  
  <p>So question is. Am I doing something horribly stupid here? I feel like there must be a more elegant solution to this.</p>