5

I have xml string below and trying to print text between tags domain, receive_time , serial and seqno for each entry tag.

xml="""
<response status="success" code="19"><result><msg><line>query job enqueued with jobid 19032</line></msg><job>19032</job></result></response>
19032
<response status="success"><result>
  <job>
    <tenq>14:10:09</tenq>
    <tdeq>14:10:09</tdeq>
    <tlast>19:00:00</tlast>
    <status>ACT</status>
    <id>19032</id>
    <cached-logs>64</cached-logs>
  </job>
  <log>
    <logs count="20" progress="29">
      <entry logid="2473601">
        <domain>1</domain>
        <receive_time>2017/11/26 14:10:08</receive_time>
        <serial>007901004140</serial>
        <seqno>10156449120</seqno>
      </entry>
      <entry logid="2473601">
        <domain>1</domain>
        <receive_time>2017/11/26 14:10:08</receive_time>
        <serial>007901004140</serial>
        <seqno>10156449120</seqno>
      </entry>
      </logs>
  </log>
</result></response>
"""

using xml.etree.ElementTree. To get what's between domain tag I was trying node.attrib.get('domain') or node.get('domain')..please advise

import xml.etree.ElementTree as ET
tree = ET.fromstring(xml)
for node in tree.iter('entry'):
        print node

It can be other python library too, does not have to be xml.etree. I do not want to print text between tags blindly, I need to print tag name followed by text so i.e.:

domain: 1
receive_time: 2017/11/26 14:10:08
serial: 007901004140
seqno: 10156449120

etc
3
  • 2
    Possible duplicate of How do I access text between tags with xml.etree.ElementTree Commented Nov 26, 2017 at 19:30
  • Not really..., the other one is printing text between tags blindly, I want to print tag name before text so i.e. domain: 1 Commented Nov 26, 2017 at 19:36
  • 1
    print node.find('domain').text should do it. By the way, your xml string in the example is not parsable. Had to remove some things before making it work. You might want to look into that if you are getting a ParseError Commented Nov 26, 2017 at 19:40

2 Answers 2

11

You find the domain tag using the find() method first. Then, the tag attribute and the text attribute should fetch the details you are looking for -

import xml.etree.ElementTree as ET
tree = ET.fromstring(xml)
for node in tree.iter('entry'):
    print('\n')
    for elem in node.iter():
        if not elem.tag==node.tag:
            print("{}: {}".format(elem.tag, elem.text))

Hope this helps!

Output -

domain: 1
receive_time: 2017/11/26 14:10:08
serial: 007901004140
seqno: 10156449120


domain: 1
receive_time: 2017/11/26 14:10:08
serial: 007901004140
seqno: 10156449120
Sign up to request clarification or add additional context in comments.

Comments

2

You can use SAX Streams to get the inner text content of the xml element. SAX is the better way to parse xml without reading the whole XML into the memory aka DOM Python SAX

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.