parsing xml find last element with matching attributes
<p>I have some poorly written xml that I'm trying to loop through and extract the test name, start, and end time of each test. I use that data to append a column to a csv. My current implementation checks every element in the xml and seems to be slow. </p>

<p>Here is a an xml example</p>

<pre><code>&lt;ProcessStart Id="1" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
&lt;ProcessStart Id="2" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
&lt;ProcessStart Id="3" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
&lt;ProcessEnd Id="1" Tick="7553"/&gt;
&lt;ProcessEnd Id="2" Tick="7553"/&gt;
&lt;ProcessEnd Id="3" Tick="7553"/&gt;
&lt;ProcessStart Id="17" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8126" /&gt;
&lt;ProcessStart Id="18" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8127" /&gt;
&lt;ProcessStart Id="19" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8127" /&gt;
&lt;ProcessEnd Id="17" Tick="12873"/&gt;
&lt;ProcessEnd Id="18" Tick="12873"/&gt;
&lt;ProcessEnd Id="19" Tick="12873"/&gt;
</code></pre>

<p>Trying to figure out how to get the Id value for the processStart that matches the name and options. So far I have:</p>

<pre><code>root.find('ProcessStart[@Name="%s"][last()]' % test_name).get('Id')
</code></pre>

<p>but rather than checking the last element that matches the test name it check the last element of the processStart. How do I address this? Or would the fastest way to extract this information be reading the input file line by line?</p>

<p><strong><em>New Information</em></strong>
I want to return the Id value where Id=3. This is the last value where all ProcessStarts with matching Name and Options exist. The current count shown immediately references the last instances of ProcessStart then checks the name and options conditions. I am looking for a way to find the last ProcessStart with matching conditions. </p>

<p>Perhaps a better example would be to match by options as name is the same for this instance so:</p>

<pre><code>options=" /pi=5 /m=AA"
test_id=root.find('ProcessStart[@Options="%s"][last()]' % options).get('Id')
</code></pre>

<p>Using this example and this data set it will return an error 'NoneType' object has no attribute 'get' I believe this is because the last element doesnt match the options. However I'm trying to target the last ProcessStart with matching options.</p>

<p>COMPLETE CODE:</p>

<pre><code>import xml.etree.ElementTree as ET

#Read the xml file
tree = ET.parse('C:/Users/mkaminski/Desktop/sample.xml')
root = tree.getroot()

#get the first option
test_option=root.find('ProcessStart').get('Options')
test_id=root.find('ProcessStart[@Options="%s"][last()]' % test_option).get('Id')
</code></pre>

<p>COMPLETE XML FILE:</p>

<pre><code>&lt;AppLog App="RPx" Version="0.6.1" BaseVer="0.0.1" Time="20160208153547" Tick="0"&gt;
  &lt;RPxTest TestName="Tests/WriteRead" LongName="WriteRead_b=0_pi=5_m=AA_i=0" Instances="16" Memory="49534849024" Options=" /pi=5 /m=AA" IdRange="1-17" Tick="1233" /&gt;
  &lt;ProcessStart Id="1" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
  &lt;ProcessStart Id="2" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
  &lt;ProcessStart Id="3" Type="MemTest" Name="WriteRead" Options=" /pi=5 /m=AA" Tick="1233" /&gt;
  &lt;ProcessEnd Id="1" Tick="7553"/&gt;
  &lt;ProcessEnd Id="2" Tick="7553"/&gt;
  &lt;ProcessEnd Id="3" Tick="7553"/&gt;
  &lt;RPxTest TestName="Tests/WriteRead" LongName="WriteRead_b=0_pi=25_m=AA_i=0" Instances="16" Memory="49534849024" Options=" /pi=25 /m=AA" IdRange="17-33" Tick="8126" /&gt;
  &lt;ProcessStart Id="17" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8126" /&gt;
  &lt;ProcessStart Id="18" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8127" /&gt;
  &lt;ProcessStart Id="19" Type="MemTest" Name="WriteRead" Options=" /pi=25 /m=AA" Tick="8127" /&gt;
  &lt;ProcessEnd Id="17" Tick="12873"/&gt;
  &lt;ProcessEnd Id="18" Tick="12873"/&gt;
  &lt;ProcessEnd Id="19" Tick="12873"/&gt;
&lt;/AppLog&gt;
</code></pre>

<p>ERROR:</p>

<pre><code>File "C:/Anaconda3/Scripts/samle.py", line 9, in &lt;module&gt;
    test_id=root.find('ProcessStart[@Options="%s"][last()]' % test_option).get('Id')

AttributeError: 'NoneType' object has no attribute 'get'
</code></pre>