简体   繁体   English

在XML文件中查找标签

[英]Find tag in XML-File

I have xml-files like: 我有像这样的xml文件:

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

My question is: Since "footnote" is not every time at the same position (ie after a text-tag; but always in "MainText"), I don't know how I can get this tag in general. 我的问题是:由于“脚注”并非每次都在同一位置(即,在文本标签之后;而是始终在“ MainText”中),所以我一般不知道如何获得此标签。 Can anybody show me how this is possible? 谁能告诉我这怎么可能? I tried it with "findall", but this does not work. 我使用“ findall”进行了尝试,但这不起作用。 Thanks for any help! 谢谢你的帮助! :) :)

Use .//MainText//footnote xpath expression. 使用.//MainText//footnote xpath表达式。 It will find footnote tag anywhere inside the MainText : 它将在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()

prints 1 . 打印1

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM