Reputation: 625
How to fetch all child's child value from xml using et tree python. Here is the same data
<try>
<place>
<c>place of the city <c>
</place>
<details>
<sex> male or female </sex>
<phone> phone no </phone>
<addresses>
<address>
<code>
<areacode> 91 </areacode>
</code>
<peraddr> the great city </peraddr>
</address>
<address>
<code>
<areacode> 9110 </areacode>
</code>
<peraddr> the great wall </peraddr>
</address>
</addresses>
</details>
</try>
How to fetch the all area code and peraddr value.
Upvotes: 0
Views: 326
Reputation: 89285
With no base code provided to starts with, there are many ways to accomplish this. Below is one possible way, which first find all address
elements, and then print areacode
and peraddr
value from each address
:
>>> raw = '''your xml string here'''
...
>>> from xml.etree import ElementTree as ET
>>> root = ET.fromstring(raw)
>>> for address in root.findall('.//address'):
... print address.find('.//areacode').text, address.find('./peraddr').text
...
91 the great city
9110 the great wall
Upvotes: 3