Mark Halls
Mark Halls

Reputation: 43

Working with xml child objects, ElementTree in Python

I'm working with a program generated piece of xml that has terrible tagging. I was able to parse out what I am looking for but I don't think it is very pythonic. The goal of the program is to total the totalLines entries, among others. I'm using elementtree to extract the values. Looking for suggestions on simplifying my syntax.

xml:

<?xml version="1.0"?>
<Status name="System Status">
<Bean name="SLMHandler" cn="com.open.eventCollector.OSLMHandler">
    <Status name="SLF File manager for SODLC2">
        <Prop name="totalLines">1105413065</Prop>
    </Status>
</Bean>

<Bean name="ThreadPool" cn="com.open.util.OThreadPoolBean">
    <Prop name="minThreads">5</Prop>
    <Prop name="maxThreads">25</Prop>
    <Prop name="checkedOutThreads">3</Prop>
    <Prop name="availableThreads">2</Prop>
    <Prop name="maxIdleTime">300000</Prop>
</Bean>

<Bean name="EventCollector" cn="com.open.eventCollector.OEventCollector">
    <Prop name="numUnmatchedLines">785319</Prop>

    <Status name="Adapters">

        <Status name="Logfile_EpilogWin_Generic">
            <Prop name="linesRead">0</Prop>
        </Status>
    </Status>
</Bean>

Python:

import xml.etree.cElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()

for bean in root.findall('Bean'):
    for status in bean.findall('Status'):
        if 'Adapters' in status.get('name'):
            for status2 in status.findall('Status'):
                for prop in status2.findall('Prop'):
                    if 'linesRead' in prop.get('name'):
                        print prop.text

Upvotes: 1

Views: 176

Answers (1)

alecxe
alecxe

Reputation: 474171

Not sure how do you want your end result to be printed, but you may make it in one go with a single XPath expression:

for lines in root.findall('.//Bean/Status[@name="Adapters"]/Status/Prop[@name="linesRead"]'):
    print(lines.text)

Prints 0 for your sample data.

Upvotes: 3

Related Questions