0

我有一个看起来像这样的 xml:

<root>
<foo>
<a></a>
<b></b>
<c></c>
</foo>
<bars>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
</bars>
<root>

我想从所有的栏中提取有趣的文本。你能告诉我如何开始吗?我试过用

bars = etree.iterparse(xml_data, tag="bars")

但我无法遍历它。

4

1 回答 1

0

使用findall方法返回所有匹配的元素。

xml_data = '''<?xml version='1.0' encoding='ASCII' ?>
<root>
<foo>
<a></a>
<b></b>
<c></c>
</foo>
<bars>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
<bar>
<one>interesting</one>
<two>interesting</two>
<three>interesting</three>
</bar>
</bars>
</root>
'''

from lxml import etree

root = etree.fromstring(xml_data)
for bars in root.findall('.//bars'):
    print(etree.tostring(bars, method='text'))
于 2013-10-08T14:59:22.023 回答