使用 et 树 python 从 xml 获取 child 的 child 值
fetch child's child value from xml using et tree python
如何使用 et 树 python 从 xml 中获取所有 child 的 child 值。
这是相同的数据
<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>
如何获取所有区号和地址值。
由于没有提供开始的基本代码,有很多方法可以实现这一点。下面是一种可能的方法,首先找到所有 address
元素,然后从每个 address
打印 areacode
和 peraddr
值:
>>> 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
如何使用 et 树 python 从 xml 中获取所有 child 的 child 值。 这是相同的数据
<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>
如何获取所有区号和地址值。
由于没有提供开始的基本代码,有很多方法可以实现这一点。下面是一种可能的方法,首先找到所有 address
元素,然后从每个 address
打印 areacode
和 peraddr
值:
>>> 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