0

I stuck with additional tag in XML. I have following XML

<result>
 <job>
    <tenq>15:37:53</tenq>
    <tdeq>15:37:53</tdeq>
    <tlast>15:37:53</tlast>
    <status>FIN</status>
    <id>168</id>
  </job>
  <log>
    <logs count="20" progress="100">
      <entry logid="6476178463223293277">
        <domain>1</domain>
        <receive_time>2017/10/13 15:37:50</receive_time>
        <serial>001801035328</serial>
        <seqno>6291444553</seqno>
        <----SKIP---->
        <pkts_received>0</pkts_received>
        <session_end_reason>policy-deny</session_end_reason>
        <action_source>from-policy</action_source>
      </entry>
      <----SKIP---->
    </logs>
  </log>
  <meta>
    <devices>
      <entry name="localhost.localdomain">
        <hostname>localhost.localdomain</hostname>
        <vsys>
          <entry name="vsys1">
            <display-name>vsys1</display-name>
          </entry>
        </vsys>
      </entry>
    </devices>
  </meta>
</result>

To get info about status I do following

    xml_parsed = ET.fromstring(resp.text)
    response_parsed = xml_parsed[0]
    resp_elems = response_parsed.findall('job')

Then I should receive number of log records and read all log entry. To get logs I am again start with:

    response_parsed = xml_parsed[0]
    resp_elems = response_parsed.findall('log')
    job_status = resp_elems[0].find('entry')

How to get logs count ? Are more elegant way exists to parse info ?

1 Answer 1

1

You can use xml.etree.ElementTree.Element.items() to get the element attributes as a sequence of (name, value) pairs. Do keep in mind that the attributes are returned in an arbitrary order, so it is likely that you'd have to do some equality or membership checking to extract the info you need:

import xml.etree.ElementTree as ET

xml_str = """<result> ... </result>"""

tree = ET.fromstring(xml_str)

for x in tree.find('log'):
    print(x.items()) # [('count', '20'), ('progress', '100')]
    print(''.join(y[1] for y in x.items() if 'count' in y[0])) # 20
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.