user2214162
user2214162

Reputation: 79

how to to use ElementTree to view subelements of a child in python

I have the following XML that I need to print out the content of members, I am trying to do this using elementree but I cannot figure out how to do this, below is the xml:

<response status="success"><result><entry name="TEST-ADDRESS-GROUP">
<static>
<member>TEST_1</member>
<member>TEST_2</member>   
</static> </entry></result></response>

Upvotes: 0

Views: 46

Answers (1)

lackti
lackti

Reputation: 156

Try this one :

import xml.etree.ElementTree as ET

xml_data = '''<response status="success"><result><entry name="TEST-ADDRESS-GROUP">
              <static>
              <member>TEST_1</member>
              <member>TEST_2</member>   
              </static> </entry></result></response>
           '''
tree = ET.fromstring(xml_data)
member_elements = tree.findall('.//member')
text_in_member_elements = [element.text for element in member_elements]

Upvotes: 1

Related Questions