Reputation: 73
I want to extract some elements from a xml file by multiple search condition using Python(2.7.5) ElementTree. the xml looks like:
<src name="BdsBoot.c">
<fn name="XXXXX" fn_cov="0" fn_total="1" cd_cov="0" cd_total="4">
<probe line="113" kind="condition" event="full"/>
<probe line="122" column="10" kind="condition" event="none" />
<probe line="124" column="9" kind="condition" event="full" />
</fn>
</src>
I want the probe elements which kind="condition" and event="full"
I have tried
root.findall(".//probe[@kind='condition' and @event='full']") —— error
root.findall(".//probe[@kind='condition'] and .//probe[@event='full']") —— nothing
I read the simple introduction here,it seems that elementtree does not support and operator now?
is there any way to achieve this goal?
Upvotes: 2
Views: 5842
Reputation: 8610
Use this one:
root.findall('.//probe[@kind="condition"][@event="full"]')
Demo:
>>> s
'<src name="BdsBoot.c">\n <fn name="XXXXX" fn_cov="0" fn_total="1" cd_cov="0" cd_total="4">\n <probe line="113" kind="condition" event="full"/>\n <probe line="122" column="10" kind="condition" event="none" />\n <probe line="124" column="9" kind="condition" event="full" />\n </fn>\n </src>'
>>> root = ET.fromstring(s)
>>> root.findall('.//probe[@kind="condition"]')
[<Element 'probe' at 0x7f8a5146ce10>, <Element 'probe' at 0x7f8a5146ce50>, <Element 'probe' at 0x7f8a5146ce90>]
>>> root.findall('.//probe[@kind="condition"][@event="full"]')
[<Element 'probe' at 0x7f8a5146ce10>, <Element 'probe' at 0x7f8a5146ce90>]
Upvotes: 4