def etree_write(tree, stream):
    """
    Write XML ElementTree 'root' content into 'stream'.

    :param tree: XML ElementTree object
    :param stream: File or file-like object can write to
    """
    try:
        tree.write(stream, encoding="utf-8", xml_declaration=True)
    except TypeError:
        tree.write(stream, encoding="unicode", xml_declaration=True)
Write XML ElementTree 'root' content into 'stream'.

    :param tree: XML ElementTree object
    :param stream: File or file-like object can write to