Data Persistence- XML Parsers in Python

Neha Kumawat

9 months ago

XML is a popular cross-platform language which is open source and portable to use very much like HTML or SGML. XML stands for eXtensible Markup Language and it is widely used in the World Wide Web Consortium (www).
XML is well-known for a data interchange format and it is used in a large number of applications such as web services, office tools, and Service-Oriented Architectures (SOA). It is used by different applications as XML format is very easy to read by both humans and machines.
Some of the XML packages consists of Python for XML processing are mentioned below-
XML Packages
XML Packages
In XML documents the data are arranged in a tree-like hierarchical format. It starts with the root and elements. Here each element represents a single node in the tree and has an attribute enclosed in <> and </> tags. We may also assign one or more sub-elements to each element.
Let’s see an example of the XML document-
<?xml version = "1.0"
encoding = "iso-8859-1"?>
Now let’s assume we want to use the ElementTree module. So, we have to follow some steps.
The first step is to set up the root element of the tree. Here each element consists of a tag and attrib which is a dict object. For the root element, attrib is like an empty dictionary.
import xml.etree.ElementTree as xml
Let’s see how we can add one or more elements under the root element. Here each element object may have, SubElements and each subelement has an attribute and text property.
a = xmlobj.SubElement(student, 'name')
subject = xmlobj.SubElement(student, 'subject')
marks = xmlobj.SubElement(student,'marks')
We can append a new element to the root using append() method as shown below.
We can also append as many elements as we wish to by using the above method. Then the root element object is written to a file as shown below.
tree = xmlobj.ElementTree(root)
file = open('studentlist.xml','wb')
Lets now see how to parse the XML file.
We have to construct a document tree giving its name as the file parameter in the ElemenTree constructor as shown below.
ele_tree = xmlobj.ElementTree(file='studentlist.xml')
To obtain a root element of the tree object it has a getroot() method and also getchildren() which returns a list of elements as shown below.
root = ele_tree.getroot()
children = root.getchildren()
We can find a dictionary object corresponding to each subelement which is constructed by iterating over sub-element collection of each child node as shown below.
for child in children:
pairs = child.getchildren()
for pair in pairs:
Now we can append each dictionary to a list which returns the original list of dictionary objects.
For more blogs/courses on data science, machine learning, artificial intelligence, and new technologies do visit us at InsideAIML.
Thanks for reading…      

Submit Review