def element_to_string ( element , include_declaration = True , encoding = DEFAULT_ENCODING , method = 'xml' ) : if isinstance ( element , ElementTree ) : element = element . getroot ( ) elif not isinstance ( element , ElementType ) : element = get_element ( element ) if element is None : return u'' element_as_string = tostring ( element , encoding , method ) . decode ( encoding = encoding ) if include_declaration : return element_as_string else : return strip_xml_declaration ( element_as_string )
python xml elementtree to string
: return : the string value of the element or element tree
def element_to_string(element, include_declaration=True, encoding=DEFAULT_ENCODING, method='xml'):
    """ :return: the string value of the element or element tree """

    if isinstance(element, ElementTree):
        element = element.getroot()
    elif not isinstance(element, ElementType):
        element = get_element(element)

    if element is None:
        return u''

    element_as_string = tostring(element, encoding, method).decode(encoding=encoding)
    if include_declaration:
        return element_as_string
    else:
        return strip_xml_declaration(element_as_string)