Python 在XML文件中查找标记

Python 在XML文件中查找标记,python,xml,elementtree,Python,Xml,Elementtree,我有如下xml文件: <article> <MainText> <Big> HDhsdjdsd </Big> <Small> jdhdhksdj </Small> <Big><text> jsdhjsadh </text> <footnote> 1 </footnote> <text> hsdhsdh </text>

我有如下xml文件:

<article>
<MainText>
    <Big> HDhsdjdsd </Big>
    <Small> jdhdhksdj </Small>
    <Big><text> jsdhjsadh </text> <footnote> 1 </footnote>  <text> hsdhsdh </text> </Big>
</MainText>
</article>

HDhsdjdsd
jdhdhksdj
jsdhjsadh 1 hsdhsdh
我的问题是:由于“footnote”并非每次都在同一位置(即在文本标记之后;但总是在“MainText”中),我不知道通常如何获得此标记。 有人能告诉我这是怎么可能的吗?我用“findall”试过了,但没用。
谢谢你的帮助!:)

使用xpath表达式。它将在
MainText
中的任何位置找到
footnote
标记:

import xml.etree.ElementTree as ET

data = """<article>
<MainText>
    <Big> HDhsdjdsd </Big>
    <Small> jdhdhksdj </Small>
    <Big><text> jsdhjsadh </text> <footnote> 1 </footnote>  <text> hsdhsdh </text> </Big>
</MainText>
</article>"""

tree = ET.fromstring(data)

print tree.find('.//MainText//footnote').text.strip()
将xml.etree.ElementTree作为ET导入
data=”“”
HDhsdjdsd
jdhdhksdj
jsdhjsadh 1 hsdhsdh
"""
tree=ET.fromstring(数据)
打印树.find('.//MainText//footnote').text.strip()
打印
1


希望这能有所帮助。

如果您演示了如何使用
findall
,我们可能会说为什么尝试无效。我补充了一个问题;)@MarkF6这不是这里的工作方式。您添加的问题与初始问题无关。最好在这里单独提出一个问题。谢谢。好的,我来做。谢谢:)