title: Extracting Specific Lines of XML with Python ElementTree
body: <p>I am a bit stuck on a project I am doing which uses Python -which I am very new to. I have been told to use ElementTree and get specified data out of an incoming XML file. It sounds simple but I am not great at programming. Below is a (very!) tiny example of an incoming file along with the code I am trying to use. </p>
  
  <p>I would like any tips or places to go next with this. I have tried searching and following what other people have done but I can't seem to get the same results. My aim is to get the information contained in the "Active", "Room" and "Direction" but later on I will need to get much more information. </p>
  
  <p>I have tried using XPaths but it does not work too well, especially with the namespaces the xml uses and the fact that an XPath for everything I would need would become too large. I have simplified the example so I can understand the principle to do, as after this it must be extended to gain more information from an "AssetEquipment" and multiple instances of them. Then end goal would be all information from one equipment being saved to a dictionary so I can manipulate it later, with each new equipment in its own separate dictionary.</p>
  
  <p>Example XML:</p>
  
  <pre><code>&lt;AssetData&gt;
  &lt;Equipment&gt;
      &lt;AssetEquipment ID="3" name="PC960"&gt;
          &lt;Active&gt;Yes&lt;/Active&gt;
          &lt;Location&gt;
              &lt;RoomLocation&gt;
                  &lt;Room&gt;23&lt;/Room&gt;
                  &lt;Area&gt;
                      &lt;X-Area&gt;-1&lt;/X-Area&gt;
                      &lt;Y-Area&gt;2.4&lt;/Y-Area&gt;
                  &lt;/Area&gt;
              &lt;/RoomLocation&gt;
          &lt;/Location&gt;
          &lt;Direction&gt;Positive&lt;/Direction&gt;
          &lt;AssetSupport&gt;12&lt;/AssetSupport&gt;
      &lt;/AssetEquipment&gt;
  &lt;/Equipment&gt;
  </code></pre>
  
  <p></p>
  
  <p>Example Code:</p>
  
  <pre><code>tree = ET.parse('C:\Temp\Example.xml')
  root = tree.getroot()
  
  ns = "{http://namespace.co.uk}"
  
  for equipment in root.findall(ns + "Equipment//"):
      tagname = re.sub(r'\{.*?\}','',equipment.tag)
      name = equipment.get('name')
  
      if tagname == 'AssetEquipment':
          print "\tName: " + repr(name)
          for attributes in root.findall(ns + "Equipment/" + ns + "AssetEquipment//"):
              attname = re.sub(r'\{.*?\}','',attributes.tag)
              if tagname == 'Room': #This does not work but I need it to be found while
                                    #in this instance of "AssetEquipment" so it does not
                                    #call information from another asset instead.
                  room = equipment.text
                  print "\t\tRoom:", repr(room)
  </code></pre>