擅长:python、mysql、java
<p>列表理解解决方案:</p>
<pre><code>xml_data='''<location>
<city>
<name> New York</name>
<type>non-capital</type>
</city>
<city>
<name> London</name>
<type>capital</type>
</city>
</location>'''
from lxml import etree as ET
parser = ET.XMLParser(recover=True)
tree = ET.fromstring(xml_data,parser)
print(tree.xpath('//city'))
cities = [[c.text for c in n if c.tail] for n in tree.xpath('//city')]
</code></pre>
<p>结果:</p>
<pre><code>[[' New York', 'non-capital'], [' London', 'capital']]
</code></pre>