title: python's lxml and iterparse method
body: <p>Say i have this sample XML.</p>
  
  <pre><code>&lt;result&gt;
      &lt;field k='field1'&gt;
          &lt;value h='1'&gt;&lt;text&gt;text_value1&lt;/text&gt;&lt;/value&gt;
      &lt;/field&gt;
      &lt;field k='field2'&gt;
          &lt;value&gt;&lt;text&gt;text_value2&lt;/text&gt;&lt;/value&gt;
      &lt;/field&gt;
      &lt;field k='field3'&gt;
          &lt;value&gt;&lt;text&gt;some_text&lt;/text&gt;&lt;/value&gt;
      &lt;/field&gt;
  &lt;/result&gt;
  </code></pre>
  
  <p>Using python's lxml, how can i get the value of each field for every result set?  So basically, i want to iterate over ever result set, then iterate over every field in that result set and print the text data.</p>
  
  <p>This is what i have so far:</p>
  
  <pre><code>context = etree.iterparse(contentBuffer, tag='result')
  for action, elem in context:
      print elem.tag, elem.data
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>EDIT</strong>
  Here is the code that i came up with.  It seems a bit clunky having to call getparent() twice to read the attribute of corresponding text value.  Is there a better way to do this?</p>
  
  <pre><code>for action, elem in context:
      list = elem.xpath('//text')
      print "result set:"
      for item in list:
          field = item.getparent().getparent().attrib['k']
          value = item.text
          print "\t%s = %s"%(field, value)
  </code></pre>