Thursday, August 17, 2017

How to get plain text inside any XML tag using python

While coding in python, if you need to get plaintext inside a particular tag in XML, you can make use of text iterator provided by ElementTree. You can call itertext() on any XML element, and it returns an iterator which will walk you through text of each tag inside your current element.

The code is simple and self explanatory. You iterate over the text iterator, concatinating text of each element inside given element to some string variable. At the end of the loop, you return the combined text. 

No comments:

Post a Comment