Turning a Dictionary into XML in Python Last Updated : 01 Sep, 2021 Comments Improve Suggest changes Like Article Like Report XML stands for Extensible Markup Language. XML was designed to be self-descriptive and to store and transport data. XML tags are used to identify, store and organize the data. The basic building block of an XML document is defined by tags. An element has a beginning tag and an ending tag. All elements in an XML are contained in an outermost element called as the root element. Example: <geeksforgeeks> <course>DSA</course> <price>2499/-</price> </geeksforgeeks> In the above example, geeksforgeeks is the root element and <course>, <price>, <price> are the elements. Now, let's see how to Turn a Dictionary into XML:For turning a Dictionary into XML in Python we will use xml.etree.ElementTree library. The xml.etree.ElementTree library is usually used for parsing and also utilized in creating XML documents. The ElementTree class is employed to wrap a component structure and convert it from and to XML. The result of this conversion is an Element. For I/O, it's easy to convert this to a byte string using the tostring() function in xml.etree.ElementTree. xml.etree.ElementTree.Element() Class: This Element class defines the Element interface, and provides a reference implementation of this interface. Syntax: Element(tag, attrib = {}, **extra) Parameter: tag: This is a string that identify what kind of data this element represents.attrib: this is an optional dictionary, containing element attributes.**extra: This contains additional attributes, given as keyword arguments. Return: Element object xml.etree.ElementTree.tostring() Function: This function Generates a string representation of an XML element. Syntax: tostring(element) Parameter: XML element Return: string representation of an XML element ElementObject.set() Method: This method Set the attribute key on the element to value. Syntax: set(key, value) Parameter: key: represent the attribute.value: represent value of attribute. Return: None Now, let's see the python program for Turning a Dictionary into XML: Python3 # import Element class, tostring function # from xml.etree.ElementTree library from xml.etree.ElementTree import Element,tostring # define a function to # convert a simple dictionary # of key/value pairs into XML def dict_to_xml(tag, d): elem = Element(tag) for key, val in d.items(): # create an Element # class object child = Element(key) child.text = str(val) elem.append(child) return elem # Driver Program s = { 'name': 'geeksforgeeks', 'city': 'noida', 'stock': 920 } # e stores the element instance e = dict_to_xml('company', s) # Element instance is different # every time you run the code print(e) # converting into a byte string print(tostring(e)) # We can attach attributes # to an element using # set() method e.set('_id','1000') print(tostring(e)) Output: <Element 'company' at 0x7f411a9bd048> b'<company><name>geeksforgeeks</name><city>noida</city><stock>920</stock></company>' b'<company _id="1000"><name>geeksforgeeks</name><city>noida</city><stock>920</stock></company>' Comment More infoAdvertise with us Next Article Turning a Dictionary into XML in Python ashishguru9803 Follow Improve Article Tags : Python python-modules Python-XML Practice Tags : python Similar Reads Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p 3 min read Iterate over a dictionary in Python In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl 6 min read Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to 4 min read Few mistakes when using Python dictionary Usually, A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. Each key-value pair in a Dictionary is separated by a 'colon', whereas each key is separated by a âcommaâ. Python3 1== my_dict = { 3 min read Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val 3 min read How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa 3 min read Serialize Python dictionary to XML XML is a markup language that is designed to transport data. It was made while keeping it self descriptive in mind. Syntax of XML is similar to HTML other than the fact that the tags in XML aren't pre-defined. This allows for data to be stored between custom tags where the tag contains details about 5 min read Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to 5 min read Convert Unicode String to Dictionary in Python Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert 2 min read Create a Nested Dictionary from Text File Using Python We are given a text file and our task is to create a nested dictionary using Python. In this article, we will see how we can create a nested dictionary from a text file in Python using different approaches. Create a Nested Dictionary from Text File Using PythonBelow are the ways to Create Nested Dic 3 min read Like