title: How to parse an XML file and retrieve child elements using ElementTree
body: <p>I have racked my brains and also searched in Stack Overflow, but it seems like mine is a different problem. Or maybe it is that I am a newbie to Python. Either way, I would be ever so thankful if you can help me. I have an XML file, excerpt given below, and I need to parse each element, and its children, and save them in a dict. I have tried many things, but I get different errors, and now I am at my wits end!</p>
  
  <p>I have given below the XML file, and a version of my code (after much stripping down after trial and error ways to retrieve child elements):</p>
  
  <pre><code>&lt;nmwg:message&gt;
      &lt;nmwg:parameters id="storeId"&gt;
          &lt;nmwg:parameter name="ID"&gt;NameA&lt;/nmwg:parameter&gt;
      &lt;/nmwg:parameters&gt;
  
      &lt;!--Metadata and Data--&gt;
      &lt;nmwg:metadata id="md1"&gt;
          &lt;nmwg:subject id="subject-port-A"&gt;
              &lt;nmwgtopo3:port id="urn:ogf:network:domaina.net:port:A"&gt;
                  &lt;nmwgtopo3:name type="logical"&gt;portA&lt;/nmwgtopo3:name&gt;
                  &lt;nmwgtopo3:country&gt;COUNTRY&lt;/nmwgtopo3:country&gt;              &lt;!--Optional, can be left empty--&gt;
                  &lt;nmwgtopo3:city&gt;CITY&lt;/nmwgtopo3:city&gt;                       &lt;!--Optional, can be left empty--&gt;
                  &lt;nmwgtopo3:institution&gt;INSTITUTION&lt;/nmwgtopo3:institution&gt;  &lt;!--Optional, can be left empty--&gt;
                  &lt;nmwgtopo3:latitude&gt;LATITUDE&lt;/nmwgtopo3:latitude&gt;           &lt;!--Optional, can be left empty--&gt;
                  &lt;nmwgtopo3:longitude&gt;LONGTITUDE&lt;/nmwgtopo3:longitude&gt;       &lt;!--Optional, can be left empty--&gt;
              &lt;/nmwgtopo3:port&gt;
          &lt;/nmwg:subject&gt;
      &lt;/nmwg:metadata&gt;
      &lt;nmwg:data id="d1" metadataIdRef="md1"&gt;
              &lt;ifevt:datum timeType="ISO" timeValue="2006-12-04T16:43:38.0+0000"&gt;                
                  &lt;ifevt:ifInOctets&gt;integer-number&lt;/ifevt:ifInOctets&gt;     &lt;!--Integer number in bytes --&gt;
                  &lt;ifevt:ifOutOctets&gt;integer-number&lt;/ifevt:ifOutOctets&gt;   &lt;!--Integer number in bytes --&gt;                
              &lt;/ifevt:datum&gt;
      &lt;/nmwg:data&gt;
  &lt;/nmwg:message&gt;
  </code></pre>
  
  <p>And here is my code:</p>
  
  <pre><code>from __future__ import print_function
  from pprint import pprint
  import ConfigParser, os
  import xml.etree.ElementTree as ET
  
  dataXMLFile = 'xmlFile.xml'
  data = ET.parse (dataXMLFile)
  root = data.getroot()
  
  for child in root:
      print ('Tag: ' + child.tag)
      print ('Attrib: ' + str(child.attrib))
  print()
  
  domainId = data.findall('{http://ggf.org/ns/nmwg/base/2.0/}parameters/*')
  for item in domainId:
      print ('Tag: ' + item.tag)
      print ('Attrib: ' + str(item.attrib))
  print()
  
  domainId = data.findall('{http://ggf.org/ns/nmwg/base/2.0/}metadata/*')
  for item in domainId:
      print ('Tag: ' + item.tag)
      print ('Attrib: ' + str(item.attrib))
  print()
  
  domainId = data.findall('{http://ggf.org/ns/nmwg/base/2.0/}metadata/*/*')
  for item in domainId:
      print ('Tag: ' + item.tag)
          print ('Attrib: ' + str(item.attrib))
  print()
  
  domainId = data.findall('{http://ggf.org/ns/nmwg/base/2.0/}metadata/*/*/*')
  for item in domainId:
      print ('Tag: ' + item.tag)
      print ('Attrib: ' + str(item.attrib))
  print()
  </code></pre>
  
  <p>I would like to parse the XML file such that, I can get <code>portA</code> value in <code>&lt;nmwgtopo3:name type="logical"&gt;</code>. I have tried, but I only get <code>type='logical'</code> bit from it. Similarly, I would like to extract <code>COUNTRY</code> from <code>&lt;nmwgtopo3:country&gt;</code>, <code>CITY</code> from <code>&lt;nmwgtopo3:city&gt;</code>, integer-number (which will be a proper integer value) from <code>&lt;ifevt:ifInOctets&gt;</code>, and so on.</p>
  
  <p>I prefer sticking to ElementTree and not use a 3rd party lib, and would appreciate your help in my question above.</p>
  
  <p>Thanks,
  Trupsster</p>