Frage

I have xml-files like:

<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 (i.e. after a text-tag; but always in "MainText"), I don't know how I can get this tag in general. Can anybody show me how this is possible? I tried it with "findall", but this does not work. Thanks for any help! :)

War es hilfreich?

Lösung

Use .//MainText//footnote xpath expression. It will find footnote tag anywhere inside the MainText:

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.

Hope that helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top