1

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

1 Answer 1

3

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.

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

1 Comment

this works excellent, unfortunately the server I'm working with is completely segregated from the internet and only has python 2.7 which doesn't support xpaths apparently.

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.