title: Python version 2.7: XML ElementTree: How to iterate through certain elements of a child element in order to find a match
body: <p>I'm a programming novice and only rarely use python so please bear with me as I try to explain what I am trying to do :)</p>
  
  <p>I have the following XML:</p>
  
  <pre><code>&lt;?xml version = "1.0" encoding = "utf-8"?&gt;
  &lt;Patients&gt;
      &lt;Patient&gt;
                 &lt;PatientCharacteristics&gt;
                     &lt;patientCode&gt;3&lt;/patientCode&gt;
                 &lt;/PatientCharacteristics&gt;
                 &lt;Visits&gt;
                     &lt;Visit&gt;
                            &lt;DAS&gt;
                                 &lt;CRP&gt;14&lt;/CRP&gt;
                                 &lt;ESR/&gt;
                                 &lt;Joints&gt;
                                         &lt;DAS_PROFILE&gt;28/28&lt;/DAS_PROFILE&gt;
                                         &lt;SWOL28&gt;20&lt;/SWOL28&gt;
                                         &lt;TEN28&gt;20&lt;/TEN28&gt;
                                 &lt;/Joints&gt;
                            &lt;/DAS&gt;
                            &lt;VisitDate&gt;2010-02-17&lt;/VisitDate&gt;
                     &lt;/Visit&gt;
                     &lt;Visit&gt;
                            &lt;DAS&gt;
                                 &lt;CRP&gt;10&lt;/CRP&gt;
                                 &lt;ESR/&gt;
                                 &lt;Joints&gt;
                                         &lt;DAS_PROFILE&gt;28/28&lt;/DAS_PROFILE&gt;
                                         &lt;SWOL28&gt;15&lt;/SWOL28&gt;
                                         &lt;TEN28&gt;20&lt;/TEN28&gt;
                                 &lt;/Joints&gt;
                            &lt;/DAS&gt;
                            &lt;VisitDate&gt;2010-02-10&lt;/VisitDate&gt;
                     &lt;/Visit&gt;
                 &lt;/Visits&gt;
      &lt;/Patient&gt;
      &lt;Patient&gt;
          &lt;PatientCharacteristics&gt;
                     &lt;patientCode&gt;3&lt;/patientCode&gt;
          &lt;/PatientCharacteristics&gt;
                 &lt;Visits&gt;
                     &lt;Visit&gt;
                            &lt;DAS&gt;
                                 &lt;CRP&gt;14&lt;/CRP&gt;
                                 &lt;ESR/&gt;
                                 &lt;Joints&gt;
                                         &lt;DAS_PROFILE&gt;28/28&lt;/DAS_PROFILE&gt;
                                         &lt;SWOL28&gt;34&lt;/SWOL28&gt;
                                         &lt;TEN28&gt;0&lt;/TEN28&gt;
                                 &lt;/Joints&gt;
                            &lt;/DAS&gt;
                            &lt;VisitDate&gt;2010-08-17&lt;/VisitDate&gt;
                     &lt;/Visit&gt;
                     &lt;Visit&gt;
                            &lt;DAS&gt;
                                 &lt;CRP&gt;10&lt;/CRP&gt;
                                 &lt;ESR/&gt;
                                 &lt;Joints&gt;
                                         &lt;DAS_PROFILE&gt;28/28&lt;/DAS_PROFILE&gt;
                                         &lt;SWOL28&gt;&lt;/SWOL28&gt;
                                         &lt;TEN28&gt;2&lt;/TEN28&gt;
                                 &lt;/Joints&gt;
                            &lt;/DAS&gt;
                            &lt;VisitDate&gt;2010-07-10&lt;/VisitDate&gt;
                     &lt;/Visit&gt;
                     &lt;Visit&gt;
                            &lt;DAS&gt;
                                 &lt;CRP&gt;9&lt;/CRP&gt;
                                 &lt;ESR/&gt;
                                 &lt;Joints&gt;
                                         &lt;DAS_PROFILE&gt;28/28&lt;/DAS_PROFILE&gt;
                                         &lt;SWOL28&gt;56&lt;/SWOL28&gt;
                                         &lt;TEN28&gt;6&lt;/TEN28&gt;
                                 &lt;/Joints&gt;
                            &lt;/DAS&gt;
                            &lt;VisitDate&gt;2009-07-10&lt;/VisitDate&gt;
                     &lt;/Visit&gt;
                 &lt;/Visits&gt;
  
      &lt;/Patient&gt;
  &lt;/Patients&gt;
  </code></pre>
  
  <p>All I want to do here is update certain 'SWOL28' values if they match the patientCode and VisitDate that I have stored in a text file. As I understand, elementtree does not include a parent reference, as if it did, I could just use findall() from the root and work backwards from there. As it stands here is my psuedocode:</p>
  
  <ol>
  <li>For each line in the text file: </li>
  <li>Put Visit_Date Patient_Code New_SWOL28 into variables</li>
  <li>For each patient element:</li>
  <li>If patientCode = Patient_Code</li>
  <li>For each Visit element:</li>
  <li>If VisitDate = Visit_Date</li>
  <li>If SWOL28 element exists for this visit</li>
  <li>Update SWOL28 to New_SWOL28</li>
  </ol>
  
  <p>But I am stuck at step number 5. How do I get a list of visits to iterated through? Apologies if this is a very dumb question but I have searched high and low for an answer I assure you! I have stripped down my code to the bare example of the part I need to fix below:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  tree = ET.parse('DB3.xml')
  root = tree.getroot()
  for child in root: # THIS GETS ME ALL THE PATIENT ATTRIBUTES
      print child.tag 
      for x in child/Visit: # THIS IS WHAT I CANNOT FIND THE CORRECT SYNTAX FOR
          # I WOULD THEN PERFORM STEPS 6, 7 AND 8 HERE
  </code></pre>
  
  <p>I would be deeply appreciative of any ideas any of you may have on this. I am not a programming natural that's for sure!</p>
  
  <p>Thanks in advance,
  Sarah</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>On the advice of SVK below I tried the following:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  tree = ET.parse('Untitled.xml')
  root = tree.getroot()
  for child in root:
      print child.tag 
      child.find( "visits" )
      for x in child.iter("visit"):
          print x.tag, x.text
  </code></pre>
  
  <p>But the only output I get is:
  Patient
  Patient
  and none of the lower tags. Any ideas?</p>