1

I have a xml file in such format:

<?xml version="1.0" encoding="UTF-8" ?>
<TAGA>
   <TAGB>TEXTB</TAGB>
   TEXTA
</TAGA>

I am using elementTree in parse this part of the file.

I can successfully get the "TEXTB" out, by using TagB.text.

My problem is with "TEXTA". I have tried TagA.text, which always returned None. Does anyone have any idea how I should get the "TEXTA" out?

1 Answer 1

2

Use tail attribute of xml.etree.ElementTree.Element object:

In this case, tail attribute will hold the text between the TAGB end tag and the next tag

...
tree = ET.parse("yourfile.xml")
root = tree.getroot()

print(root.find('TAGB').tail.strip())

The output:

TEXTA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.