1

I am trying to extract some data from a large XML output, could someone help me extract the TotalMilliseconds for just the 99 percentile.

I am trying to do this in Python using the ElementTree parser.

I am currently trying to do something like this which finds the 99th percentile and then tries to find the TotalMilliseconds from the root of that level.

But this returns nothing, in debug I can see it gets into the 99 clause but I am bit lost on where I go from there.

for item in root.findall('./TimeSpan/Latency/Bucket/Percentile'):
    if item.text == "99":
        totalMs = item.find('TotalMilliseconds').text
        print(totalMs)
<TimeSpan>
<Latency>
<Bucket>
<Percentile>96</Percentile>
<ReadMilliseconds>55.378</ReadMilliseconds>
<WriteMilliseconds>105.115</WriteMilliseconds>
<TotalMilliseconds>98.546</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>97</Percentile>
<ReadMilliseconds>59.552</ReadMilliseconds>
<WriteMilliseconds>109.733</WriteMilliseconds>
<TotalMilliseconds>104.649</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>98</Percentile>
<ReadMilliseconds>64.891</ReadMilliseconds>
<WriteMilliseconds>116.998</WriteMilliseconds>
<TotalMilliseconds>111.300</TotalMilliseconds>
</Bucket>
<Bucket>
<Percentile>99</Percentile>
<ReadMilliseconds>81.629</ReadMilliseconds>
<WriteMilliseconds>131.931</WriteMilliseconds>
<TotalMilliseconds>125.176</TotalMilliseconds>
</Bucket>
</Latency>
</TimeSpan>

2 Answers 2

2

Instead of searching and iterating over the Percentile elements, iterate over the Bucket elements. Then, for each Bucket, find and compare its Percentile and proceed as you do already:

import xml.etree.ElementTree as ET

xml = """
    YOUR XML
"""

tree = ET.fromstring(xml)

for item in tree.findall(".//Bucket"):
    if item.find("Percentile").text == "99":
        totalMs = item.find("TotalMilliseconds").text
        print(totalMs)

Output:

125.176
Sign up to request clarification or add additional context in comments.

Comments

2

See below

import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="UTF-8"?>
<TimeSpan>
   <Latency>
      <Bucket>
         <Percentile>96</Percentile>
         <ReadMilliseconds>55.378</ReadMilliseconds>
         <WriteMilliseconds>105.115</WriteMilliseconds>
         <TotalMilliseconds>98.546</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>97</Percentile>
         <ReadMilliseconds>59.552</ReadMilliseconds>
         <WriteMilliseconds>109.733</WriteMilliseconds>
         <TotalMilliseconds>104.649</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>98</Percentile>
         <ReadMilliseconds>64.891</ReadMilliseconds>
         <WriteMilliseconds>116.998</WriteMilliseconds>
         <TotalMilliseconds>111.300</TotalMilliseconds>
      </Bucket>
      <Bucket>
         <Percentile>99</Percentile>
         <ReadMilliseconds>81.629</ReadMilliseconds>
         <WriteMilliseconds>131.931</WriteMilliseconds>
         <TotalMilliseconds>125.176</TotalMilliseconds>
      </Bucket>
   </Latency>
</TimeSpan>"""

root = ET.fromstring(data)
# data is a list to support the case of many 99 Percentile
data = [e.find('TotalMilliseconds').text for e in root.findall('.//Bucket') if e.find('Percentile').text == '99']
print(data)

output

['125.176']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.