How to create a simple XML file in Python

An XML (Extensible Markup Language) file contains a set of rules for encoding documents in a format that is both human and machine friendly. An XML file is represented as a Tree structure in Python.

Solution for How to create a simple XML file in Python : You can use the xml.etree.ElementTree API to create a XML file as a Tree Call xml.etree.ElementTree.Element(a_tag, an_attribute={}) to create an element instance with tag a_tag and attribute {}.
Call xml.etree.ElementTree.SubElement(a_parent, a_tag, an_attribute={}) to create a subelement with tag a_tag, attribute {}, and parent node a_parent.
Call xml.etree.ElementTree.ElementTree(root_node) to create an elementTree with root root_node.
Call xml.etree.ElementTree.tostring(element, encoding=”unicode”) to generate a string representation of an XML element, including all subelements.


how-to-create-a-simple-xml-file-in-python