Enable Dark Mode!
By: Cybrosys Technologies

ElementTree XML API in Python


ElementTree is a useful python package that allows us to create, modify and parse XML documents. XML is a hierarchical data structure and is represented with a tree. The whole document is considered as a tree. The Element represents a single node or branch in this tree. Reading or writing to the files or from the files in the XML is done based on the ElementTree level and the interaction to the elements is on the basis of Element level.
This blog explains how we can create, modify and parse an XML document using ElementTree and also explains the most important Element objects and the ElementTree objects.

Create XML Files

Initially let’s see how we can create an XML file using ElementTree.
This tree-like hierarchical structure starts from the root followed by the subsequent elements and subelements under the root.
First of all, we have to create a root for the XML tree that we are going to create. For that we should import the ElementTree using the following command:
import xml.etree.ElementTree as ET
Usually, we use the alias of ET for the import operation.
The next step is to create the root which can be done by using the following command:
root = ET.Element("Root")
Now you can set up one more child element to be added under the root element. Moreover, under each child element we can add the sub elements which is indicated in the following set of codes:
root = ET.Element("Root")
m1 = ET.Element("child1")
b1 = ET.SubElement(m1, "subelement1")
b1.text = "value1"
b2 = ET.SubElement(m1, "subelement2")
b2.text = "value2"
m2 = ET.Element("child2")
c1 = ET.SubElement(m2, "subelement3")
c1.text = "value3"
c2 = ET.SubElement(m2, "subelement3")
c2.text = "value4"
tree = ET.ElementTree(root)
Similarly, we can add the child and subelements under the root and can append to the root using the append() function as mentioned in the above code. Moreover the output of this will be as given below.
The entire tree structure that we have created can be written to the file as in the following code:
with open(your_file_name.xml', "w") as files:
Suppose if we want to write it to the binary file you can use the following command:
f = open(your_file_name.xml', "wb")
Now let’s see some of the useful Element objects and ElementTree objects.
Element Objects
class xml.etree.ElementTree.Element(tag, attrib={}, **extra)Element class
This class is used to define the Element interface. Moreover, we can use byte strings or unicode strings for the element name and also the attribute name. The tag describes the element name and the attrib describes the element attributes.
tag: It is a string which represents the type of data that the element represents.
text: This one is used to hold the additional data of an element. Furthermore, their values can be string and are also application specific.
attrib: Attrib is a dictionary which includes the element’s attribute.
set(key, value): We can set the attribute key on the element using set.
append(subelement): This one is used to append the child element to the root or the sub elements to the main element.
find(match, namespaces=None): Find can be used to find the sub element which matches the match. Match can be any tag name or a path. The find will return only one element at an instance if there are any other it will return None. 
 findall(match, namespaces=None): Findall is also similar to the find as described above. But the findall() returns a list of sub elements which match the match.
These are the certain Element objects used in the XML file creation now let's look into the ElementTree objects used in the function.

ElementTree Objects
Some of the element objects are also available as the ElementTree objects eg:the element class, find, find all etc
_setroot(element): For replacing the root of a tree we can use this _setroot object. So it will replace the current tree with the new element that we have given, and discard the existing content of that tree.
getroot(): The getroot() will return the root element of the tree.
iter(tag=None): The iter() function will loop the over all elements of the tree. Just like it acts as an iterator of the root element.
write: Write function is used to write the element tree into a file as mentioned above.
These are the certain ElementTree objects used in the XML file creation function.

Odoo Training Package
Odoo Implementation Package

If you need any assistance in odoo, we are online, please chat with us.

cybrosys youtube


Leave a comment


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635


Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom


Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.


Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message