def tostring(element):
rv = []<EOL>finalText = None<EOL>def serializeElement(element):<EOL><INDENT>if not hasattr(element, "<STR_LIT>"):<EOL><INDENT>if element.docinfo.internalDTD:<EOL><INDENT>if element.docinfo.doctype:<EOL><INDENT>dtd_str = element.docinfo.doctype<EOL><DEDENT>else:<EOL><INDENT>dtd_str = "<STR_LIT>" % element.docinfo.root_name<EOL><DEDENT>rv.append(dtd_str)<EOL><DEDENT>serializeElement(element.getroot())<EOL><DEDENT>elif element.tag == comment_type:<EOL><INDENT>rv.append("<STR_LIT>" % (element.text,))<EOL><DEDENT>else:<EOL><INDENT>if not element.attrib:<EOL><INDENT>rv.append("<STR_LIT>" % (element.tag,))<EOL><DEDENT>else:<EOL><INDENT>attr = "<STR_LIT:U+0020>".join(["<STR_LIT>" % (name, value)<EOL>for name, value in element.attrib.items()])<EOL>rv.append("<STR_LIT>" % (element.tag, attr))<EOL><DEDENT>if element.text:<EOL><INDENT>rv.append(element.text)<EOL><DEDENT>for child in element:<EOL><INDENT>serializeElement(child)<EOL><DEDENT>rv.append("<STR_LIT>" % (element.tag,))<EOL><DEDENT>if hasattr(element, "<STR_LIT>") and element.tail:<EOL><INDENT>rv.append(element.tail)<EOL><DEDENT><DEDENT>serializeElement(element)<EOL>if finalText is not None:<EOL><INDENT>rv.append("<STR_LIT>" % ('<STR_LIT:U+0020>' * <NUM_LIT:2>, finalText))<EOL><DEDENT>return "<STR_LIT>".join(rv)<EOL>
Serialize an element and its child nodes to a string