0

I need to pull some values from an XML formatted report. The structure of the XML is fairly complex and I can't get my head around how to reference the different levels in the XML structure.

I've imported ElementTree and loaded the source file, and I can successfully pull values from the upper levels of the tree. Unfortunately, all the ElementTree tutorials use quite simple examples and I can't figure out the syntax needed to dig deeper into the structure, particularly when keys and values are duplicated.

For example, here I'm retrieving the "Total Calls" value:

tree = ET.parse('Z:/VSCode/Python/CallStats/calls.xml')
root = tree.getroot()

#Get Total Calls
for totcalls in root.iter('TotalCalls'):
    print(totcalls.text)

But this returns duplicate values, because the TotalCalls tag appears twice in the report, at different levels <Overview> and <KeyFacts> and I would like to only read the value from the <KeyFacts> level.

What is the syntax to retrieve the TotalCalls value from the <KeyFacts> level please?

Here is the source file:

<?xml version="1.0" encoding="UTF-8"?><Report version="1">
    <ReportConfig>
        <Name>Test01</Name>
        <BeginDate>Fri Jan 29 05:00:00 EST 2021</BeginDate>
        <EndDate>Fri Jan 29 05:14:59 EST 2021</EndDate>
        <GenerationTime>Fri Jan 29 05:15:01 EST 2021</GenerationTime>
        <DataSource>Live Traffic</DataSource>
        <AggregationType>Summary</AggregationType>
        <ReportPeriod>15 Minutes</ReportPeriod>
        <Class name="Service">
            <ResGroup description="" type="">All (Individual)</ResGroup>
            <Service description="VOIP Service">VoIP</Service>
        </Class>
    </ReportConfig>
    <DataAvailable>true</DataAvailable>
    <Aggregation name="Summary">
        <Resource name="TestInstance">
            <Overview>
                <AverageServiceHealth>4.1</AverageServiceHealth>
                <ReportOutcomeHistogram>
                    <Fail>16</Fail>
                    <Busy>0</Busy>
                    <NoAnswer>0</NoAnswer>
                    <Answered>2323</Answered>
                </ReportOutcomeHistogram>
                <AverageLossPct>0.1</AverageLossPct>
                <AverageJitterMS>3.3458</AverageJitterMS>
                <AverageDelayMS>91.6</AverageDelayMS>
                <AverageDurationSecs>312.8512</AverageDurationSecs>
                <TotalCalls>565</TotalCalls>
                <TotalCallMinutes>12112.5552</TotalCallMinutes>
                <AnswerSeizureRatio>99.3</AnswerSeizureRatio>
                <NetworkEffectivenessRatio>99.3</NetworkEffectivenessRatio>
            </Overview>
            <ServiceQuality xaxis="MOS-CQ" title="VoIP Service Quality">
                <AverageServiceHealth>4.1</AverageServiceHealth>
                <MOSHistogram title="MOS-CQ">
                    <Bin lower="1.0" higher="3.099">7</Bin>
                    <Bin lower="3.1" higher="3.199">15</Bin>
                    <Bin lower="3.2" higher="3.299">3</Bin>
                    <Bin lower="3.3" higher="3.399">0</Bin>
                    <Bin lower="3.4" higher="3.499">5</Bin>
                    <Bin lower="3.5" higher="3.599">7</Bin>
                    <Bin lower="3.6" higher="3.699">9</Bin>
                    <Bin lower="3.7" higher="3.799">15</Bin>
                    <Bin lower="3.8" higher="3.899">27</Bin>
                    <Bin lower="3.9" higher="3.999">332</Bin>
                    <Bin lower="4.0" higher="4.099">472</Bin>
                    <Bin lower="4.1" higher="4.199">55</Bin>
                    <Bin lower="4.2" higher="4.299">1378</Bin>
                    <Bin lower="4.3" higher="4.399">0</Bin>
                    <Bin lower="4.4" higher="5.000">0</Bin>
                </MOSHistogram>
            </ServiceQuality>
            <KeyFacts>
                <NumberOfCallAttempts>565</NumberOfCallAttempts>
                <TotalCalls>565</TotalCalls>
                <TotalRecords>2339</TotalRecords>
                <TotalCallMinutes>12112.5552</TotalCallMinutes>
            </KeyFacts>
            <CallStatistics>
                <AnswerSeizureRatio>99.3</AnswerSeizureRatio>
                <NetworkEffectivenessRatio>99.3</NetworkEffectivenessRatio>
                <AverageCallDurationSec>312.9</AverageCallDurationSec>
                <NumberOfCallAttempts>565</NumberOfCallAttempts>
                <ReportedOutcomes>
                    <Answered>2323</Answered>
                    <Unanswered>0</Unanswered>
                    <Busy>0</Busy>
                    <ReportedFailures>16</ReportedFailures>
                    <Dropped>12</Dropped>
                    <OneWayMedia>4</OneWayMedia>
                    <NoMedia>0</NoMedia>
                    <NotFound>0</NotFound>
                    <Unauthorized>0</Unauthorized>
                    <ServerBusy>0</ServerBusy>
                    <ServerError>0</ServerError>
                    <BadSignaling>0</BadSignaling>
                </ReportedOutcomes>
                <NumberOfRegistrationFailures>0</NumberOfRegistrationFailures>
            </CallStatistics>
            <SLA>
                <SLAFailCount>0</SLAFailCount>
                <RecordCount>2339</RecordCount>
                <ObservedPassRate>100.000</ObservedPassRate>
            </SLA>
            <AlertsGenerated>
                <CriticalCount>0</CriticalCount>
                <WarnCount>0</WarnCount>
            </AlertsGenerated>
        </Resource>
    </Aggregation>
</Report>

1 Answer 1

1

Iterate over KeyFacts elements and get the text value of their TotalCalls children.

import xml.etree.ElementTree as ET
 
tree = ET.parse('Z:/VSCode/Python/CallStats/calls.xml')
 
for kf in tree.iter('KeyFacts'):
    print(kf.findtext("TotalCalls"))
Sign up to request clarification or add additional context in comments.

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.