import re


def get_text(xml_elem, tag_name, concat=True):
    """Extract text from a XML element, strip whitespace."""

    sub_elems = xml_elem.getElementsByTagName(tag_name)
    if not sub_elems:
        return None
    if not concat:
        sub_elems = [sub_elems[0]]
    text =  ' '.join([tn.data
                      for sub_elem in sub_elems
                      for tn in sub_elem.childNodes])
    text = re.sub('[\n\t]', ' ', text)
    text = re.sub(' +', ' ', text)
    text = text.strip()
    return text