title: Issues Parsing Multi Nested Childs in XML using lxml
body: <p>Im having issues parsing out each child node within an xml file. The number of nodes can change per Instrument_Root. For instance, Instrument_Watch is NULL here, but will be populated in other instances after this. My goal is to have each child node parsed individually (Instrument_Ratings, Instrument_Attribute_Ratings, Instrument_Organization, Instrument_Supports, etc.)</p>
  
  <p>I tried doing the following, but it just returned the first intance repeatedly - there are 3700 Instrument_Root in the file, and Instrument_Rating for this one Instrument_Root was repeated 3700 times. I also ran into errors with etree due to the namespace. </p>
  
  <pre><code>from lxml import objectify
  
  xml = objectify.parse(file)
  root = xml.getroot()
  
  tree1 = []
  tree2 = []
  tree3 = []
  tree4 = []
  for children in range(len(root.getchildren())):
      tree1.append([child.text for child in root.getchildren()[children].iterchildren()])
      for children2 in root.Instrument_Root.Instrument_Ratings.Instrument_Rating.getchildren():
          tree2.append([child2.text for child2 in  root.Instrument_Root.Instrument_Ratings.Instrument_Rating.getchildren()])
          for children3 in root.Instrument_Root.Instrument_Ratings.Instrument_Rating.Instrument_Rating_Attributes.Instrument_Rating_Attribute.getchildren():
              tree3.append([child3.text for child3 in root.Instrument_Root.Instrument_Ratings.Instrument_Rating.Instrument_Rating_Attributes.Instrument_Rating_Attribute.getchildren()])
              for children4 in root.Instrument_Root.Instrument_Organizations.Instrument_Organization.getchildren():
                  tree4.append([child4.text for child4 in root.Instrument_Root.Instrument_Organizations.Instrument_Organization.getchildren()])
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;              
  &lt;Instrument_Roots xmlns="http://www.XXXXX.com" xmlns:xsi="http://www.XXXXXXX.XMLSchema-instance" file_type="Baseline" frequency="Hourly-12" generation_time="2020-04-06T12:00:00Z"&gt;             
      &lt;Instrument_Root&gt;           
          &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;        
          &lt;Deal_number&gt;831275547&lt;/Deal_number&gt;        
          &lt;Class_Code&gt;18705&lt;/Class_Code&gt;      
          &lt;Class_Text&gt;Pass-Through&lt;/Class_Text&gt;       
          &lt;Class_Short_Description&gt;PAS&lt;/Class_Short_Description&gt;      
          &lt;Dated_Date&gt;2020-03-21T00:00:00&lt;/Dated_Date&gt;        
          &lt;ISO_Currency_Code&gt;AUD&lt;/ISO_Currency_Code&gt;      
          &lt;Currency_Multiple_Indicator&gt;N&lt;/Currency_Multiple_Indicator&gt;        
          &lt;Maturity_Date&gt;2051-03-21T00:00:00&lt;/Maturity_Date&gt;      
          &lt;Maturity_Year&gt;2051&lt;/Maturity_Year&gt;     
          &lt;Sale_Date&gt;2020-03-21T00:00:00&lt;/Sale_Date&gt;      
          &lt;Face_Amount_USD&gt;7.2534316791&lt;/Face_Amount_USD&gt;     
          &lt;Credit_Linked_Indicator&gt;N&lt;/Credit_Linked_Indicator&gt;        
          &lt;Takedown_Indicator&gt;N&lt;/Takedown_Indicator&gt;      
          &lt;Security_Description&gt;Class B&lt;/Security_Description&gt;        
          &lt;Instrument_Type_Code&gt;24657&lt;/Instrument_Type_Code&gt;      
          &lt;Instrument_Type_Text&gt;PASS-THRU CTFS&lt;/Instrument_Type_Text&gt;     
          &lt;Private_Placement_Code&gt;24922&lt;/Private_Placement_Code&gt;      
          &lt;Private_Placement_Text&gt;Not Applicable&lt;/Private_Placement_Text&gt;     
          &lt;Coupon_Type_Code&gt;26&lt;/Coupon_Type_Code&gt;     
          &lt;Coupon_Type_Text&gt;Floating&lt;/Coupon_Type_Text&gt;       
          &lt;Coupon_Type_Short_Description&gt;FLT&lt;/Coupon_Type_Short_Description&gt;      
          &lt;Coupon_Frequency_Code&gt;16&lt;/Coupon_Frequency_Code&gt;       
          &lt;Coupon_Frequency_Text&gt;Monthly&lt;/Coupon_Frequency_Text&gt;      
          &lt;Coupon_Frequency_Short_Description&gt;MON&lt;/Coupon_Frequency_Short_Description&gt;        
          &lt;Coupon_Rate xsi:nil="true"/&gt;       
          &lt;Instrument_Description xsi:nil="true"/&gt;        
          &lt;Product_Line_Description&gt;MBS - Prime&lt;/Product_Line_Description&gt;        
          &lt;Series_Class_Text&gt;Class B&lt;/Series_Class_Text&gt;      
          &lt;Market_Description&gt;AUSTRALIA&lt;/Market_Description&gt;      
          &lt;Face_Amount&gt;11.2500000000&lt;/Face_Amount&gt;        
          &lt;Structured_Finance_Indicator&gt;Y&lt;/Structured_Finance_Indicator&gt;      
          &lt;Structured_Finance_Sequence_Number&gt;3&lt;/Structured_Finance_Sequence_Number&gt;      
          &lt;Instrument_Ratings&gt;        
              &lt;Instrument_Rating&gt; 
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Rating_Class_Number&gt;37203&lt;/Rating_Class_Number&gt;
                  &lt;Rating_Date&gt;2020-03-02T01:30:03&lt;/Rating_Date&gt;
                  &lt;XXXX_Rating_ID&gt;831295958&lt;/XXXX_Rating_ID&gt;
                  &lt;Rating_Level&gt;I&lt;/Rating_Level&gt;
                  &lt;Rating_Class_Text&gt;Senior Secured&lt;/Rating_Class_Text&gt;
                  &lt;Security_Class_Code&gt;18705&lt;/Security_Class_Code&gt;
                  &lt;Security_Class_Text&gt;Pass-Through&lt;/Security_Class_Text&gt;
                  &lt;Security_Class_Short_Description&gt;PAS&lt;/Security_Class_Short_Description&gt;
                  &lt;Duration_Code&gt;25636&lt;/Duration_Code&gt;
                  &lt;Duration_Text&gt;Long-Term Debt Rating&lt;/Duration_Text&gt;
                  &lt;Duration_Short_Description&gt;LT&lt;/Duration_Short_Description&gt;
                  &lt;Seniority_Code&gt;18743&lt;/Seniority_Code&gt;
                  &lt;Seniority_Text&gt;Senior Secured&lt;/Seniority_Text&gt;
                  &lt;Seniority_Short_Description&gt;SS&lt;/Seniority_Short_Description&gt;
                  &lt;Evaluation_Type_Code&gt;25648&lt;/Evaluation_Type_Code&gt;
                  &lt;Evaluation_Type_Text&gt;Credit Risk&lt;/Evaluation_Type_Text&gt;
                  &lt;Shadow_Code&gt;5734&lt;/Shadow_Code&gt;
                  &lt;Shadow_Text&gt;Enhanced&lt;/Shadow_Text&gt;
                  &lt;Shadow_Short_Description&gt;ENH&lt;/Shadow_Short_Description&gt;
                  &lt;Rating_Subclass_Code xsi:nil="true"/&gt;
                  &lt;Rating_Subclass_Text xsi:nil="true"/&gt;
                  &lt;Currency_Capd_Code&gt;19142&lt;/Currency_Capd_Code&gt;
                  &lt;Currency_Capd_Text&gt;Local Currency&lt;/Currency_Capd_Text&gt;
                  &lt;Rating_Text&gt;NR&lt;/Rating_Text&gt;
                  &lt;Credit_Grade xsi:nil="true"/&gt;
                  &lt;Rating_Rank&gt;0&lt;/Rating_Rank&gt;
                  &lt;Rating_Direction_Code&gt;19102&lt;/Rating_Direction_Code&gt;
                  &lt;Rating_Direction_Text&gt;DECISION NOT TO RATE&lt;/Rating_Direction_Text&gt;
                  &lt;Rating_Direction_Short_Description&gt;NR&lt;/Rating_Direction_Short_Description&gt;
                  &lt;Rating_Type_Code&gt;534&lt;/Rating_Type_Code&gt;
                  &lt;Rating_Type_Text&gt;Long-Term Debt Rating&lt;/Rating_Type_Text&gt;
                  &lt;Rating_Type_Short_Description&gt;LT&lt;/Rating_Type_Short_Description&gt;
                  &lt;Rating_Enhancement_Level&gt;ENH&lt;/Rating_Enhancement_Level&gt;
                  &lt;Rating_Local_Date&gt;2020-03-02T17:30:03&lt;/Rating_Local_Date&gt;
                  &lt;Rating_Termination_Date xsi:nil="true"/&gt;
                  &lt;Rating_Termination_Local_Date xsi:nil="true"/&gt;
                  &lt;Rating_Reason_Code&gt;25530&lt;/Rating_Reason_Code&gt;
                  &lt;Rating_Reason_Text&gt;DECISION NOT TO RATE&lt;/Rating_Reason_Text&gt;
                  &lt;Rating_Currency_Code&gt;20525&lt;/Rating_Currency_Code&gt;
                  &lt;Rating_Currency_Text&gt;Australian Dollar&lt;/Rating_Currency_Text&gt;
                  &lt;Rating_Currency_ISO_Code&gt;AUD&lt;/Rating_Currency_ISO_Code&gt;
                  &lt;Rating_Monitor_Indicator&gt;1&lt;/Rating_Monitor_Indicator&gt;
                  &lt;Initial_Rating_Indicator&gt;Y&lt;/Initial_Rating_Indicator&gt;
                  &lt;Instrument_Watchlist xsi:nil="true"/&gt;
                  &lt;Instrument_Rating_Attributes&gt;
                  &lt;/Instrument_Rating_Attributes&gt;
              &lt;/Instrument_Rating&gt;    
          &lt;/Instrument_Ratings&gt;       
          &lt;Instrument_Supports xsi:nil="true"/&gt;       
          &lt;Instrument_Organizations&gt;      
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;88100&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;5156484&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Issuer Account Bank&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;435700&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600045234&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;25017&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Seller&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600045234&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;25020&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Servicer&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;530750&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;24998&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Arranger&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;540500&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;714325&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;192835&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600020914&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;831295948&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;129&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Issuer&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600018753&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;140&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Trustee&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600045234&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;25002&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Custodian&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;806942842&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;140&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Trustee&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;820688318&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;5155837&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Cash Manager&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;530750&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;141&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Underwriter&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
              &lt;Instrument_Organization&gt;   
                  &lt;Organization_ID&gt;600045234&lt;/Organization_ID&gt;
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Organization_Role_Code&gt;25012&lt;/Organization_Role_Code&gt;
                  &lt;Organization_Role_Text&gt;Originator&lt;/Organization_Role_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Organization&gt;  
          &lt;/Instrument_Organizations&gt;     
          &lt;Instrument_Identifiers xsi:nil="true"/&gt;        
          &lt;Instrument_Attributes&gt;     
              &lt;Instrument_Attribute&gt;  
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Rating_Attribute_Type_Code&gt;5156438&lt;/Rating_Attribute_Type_Code&gt;
                  &lt;Rating_Attribute_Type_Text&gt;SF Indicator&lt;/Rating_Attribute_Type_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
                  &lt;Rating_Attribute_Code&gt;5156439&lt;/Rating_Attribute_Code&gt;
                  &lt;Rating_Attribute_Text&gt;(sf)&lt;/Rating_Attribute_Text&gt;
              &lt;/Instrument_Attribute&gt; 
              &lt;Instrument_Attribute&gt;  
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Rating_Attribute_Type_Code&gt;5168187&lt;/Rating_Attribute_Type_Code&gt;
                  &lt;Rating_Attribute_Type_Text&gt;SEC Exempt&lt;/Rating_Attribute_Type_Text&gt;
                  &lt;Effective_Date&gt;2020-03-02T01:30:03&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
                  &lt;Rating_Attribute_Code&gt;5168185&lt;/Rating_Attribute_Code&gt;
                  &lt;Rating_Attribute_Text&gt;Exempt&lt;/Rating_Attribute_Text&gt;
              &lt;/Instrument_Attribute&gt; 
          &lt;/Instrument_Attributes&gt;        
          &lt;Instrument_Markets&gt;        
              &lt;Instrument_Market&gt; 
                  &lt;Instrument_ID&gt;831295951&lt;/Instrument_ID&gt;
                  &lt;Domain_Number&gt;75300&lt;/Domain_Number&gt;
                  &lt;Effective_Date&gt;2020-03-21T00:00:00&lt;/Effective_Date&gt;
                  &lt;Termination_Date xsi:nil="true"/&gt;
              &lt;/Instrument_Market&gt;    
          &lt;/Instrument_Markets&gt;       
      &lt;/Instrument_Root&gt;          
   &lt;/Instrument_Roots&gt;            
  </code></pre>
  
  <p>Any ideas on how to attack this would be greatly appreciated. Thanks. </p>