xml.etree.ElementTree.tostring()
xml.etree.ElementTree.tostring() is a Python function in the XML Module category. Serializes an Element to bytes (XML string). The syntax is xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml", *, xml_declaration=None, default_namespace=None, short_empty_elements=True). Returns: XML bytes or str. A typical example: root = ET.Element("root")
child = ET.SubElement(root, "item", {"id": "1"})
child.text = "Hello"
ET.tostring(root, encoding="unicode")
# "- Hello
". A close sibling is xml.etree.ElementTree.parse(), which parses an XML file and returns an ElementTree object. A close sibling is xml.etree.ElementTree.fromstring(), which parses XML from a string and returns an Element. A close sibling is xml.etree.ElementTree.Element(), which creates a new XML element with the given tag and attributes. A close sibling is xml.etree.ElementTree.SubElement(), which creates a new XML element as a child of the given parent. More about this category: XML processing — ElementTree, parse, fromstring, tostring. Related XML Module entries: xml.etree.ElementTree.parse(), xml.etree.ElementTree.fromstring(), xml.etree.ElementTree.Element(), xml.etree.ElementTree.SubElement(), Element.find(), Element.findall(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.