I have this xml:
<office:body>
<office:text>
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
</text:sequence-decls>
<text:p text:style-name="Standard">
<office:annotation>...</office:annotation>
foobar
</text:p>
</office:text>
</office:body>
I want to find the text “foobar” with elementtree since instead of “foobar” can be any text?
Assume that the XML document looks like this (with declared namespaces):
You can then get the “foobar” string using this program:
Here, the
ElementTree.find()method is used to find theoffice:annotationelement and theElement.tailattribute returns the text content after the element’s end-tag.