Building and Parsing XML Document using Python

In this Blog Post, We will see how to create an XML document and parse XML Document using python.

Python supports to work with various forms of structured data markup. This includes modules to work with the Hypertext Markup Language (HTML), Extensible Markup Language (XML).

In addition to parsing XML, xml.etree.ElementTree used for creating well-formed documents from Element objects in an application. The Element class used when a document is parsed also knows how to generate a serialized form of its contents, which can then be written to a file.

Creating an XML Document:

For creating element instance, we need to use Element constructor or subelement() To create an element instance factory function.

import xml.etree.ElementTree as xml

filename = "/home/abc/Desktop/test_xml.xml"
root = xml.Element("Users")
userelement = xml.Element("user")
root.append(userelement)

When you run this you can see the output as below:

<Users>
    <user>
    </user>
</Users>

Let us add user children

uid = xml.SubElement(userelement, "uid")
uid.text = "1"
FirstName = xml.SubElement(userelement, "FirstName")
FirstName.text = "testuser"
LastName = xml.SubElement(userelement, "LastName")
LastName.text = "testuser"
Email = xml.SubElement(userelement, "Email")
Email.text = "testuser@test.com"
state = xml.SubElement(userelement, "state")
state.text = "xyz"
location = xml.SubElement(userelement, "location")
location.text = "abc"
tree = xml.ElementTree(root)
with open(filename, "w") as fh:
    tree.write(fh)


First we create the root element by using ElmeentTree’s Element function. Then we create an user element and append it to the root. For creating SubElement we will use Element object as first argument and name as second argument.. Then for each SubElement, we set its text property to give it a value.
 
If you run this the reponse will be as following

<Users>
    <user>
        <uid>1</uid>
        <FirstName>testuser</FirstName>
        <LastName>testuser</LastName>
        <Email>testuser@test.com</Email>
        <state>xyz</state>
        <location>abc</location>
    </user>
</Users>

Parsing the XML Document:

import xml.etree.ElementTree as ET
tree = ET.parse('Your_XML_file_path')
root = tree.getroot()

here getroot() will return the root Element of the XML document.

<Users version="1.0" language="SPA">
    <user>
        <uid>1</uid>
        <FirstName>testuser</FirstName>
        <LastName>testuser</LastName>
        <Email>testuser@test.com</Email>
        <state>xyz</state>
        <location>abc</location>
    </user>
</Users>

Let us take above example for parsing.

here getroot() will return "Users" Element as "<Element 'Users' at 0x7f9095365d90>"

root.tag --> This returns only the Tag, Here in our Example "Users".
root.attrib --> This returns the attributes of the root element, In our Example "{'version': '1.0', 'language': 'SPA'}"
root.getchildren() --> This Returns the children elements of that root in an array.
In our Example: [<Element 'uid' at 0x7f0c307eee10>, <Element 'FirstName' at 0x7f0c307eeed0>, <Element 'LastName' at 0x7f0c307eef90>, <Element 'Email' at 0x7f0c307f0150>, <Element 'state' at 0x7f0c307f0190>, <Element 'location' at 0x7f0c307f01d0>].

From this you can read the text of the required Element and then insert in to your database.

To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Create excel file, Insert image, Draw Bar Graphs in excel files in python using xlsxwriter Chaitanya Kattineni

Xlsxwriter is a python module through which we can write data to Excel 2007+ XLSX file format. In this blog post we learn to write ...

Continue Reading...
Python using yield and generators. Dinesh Deshmukh

Generators are memory efficient. They allow us to code with minimum intermediate arguments, less data structures. Generators are of two types, generator expressions and generator ...

Continue Reading...
Working with python collections part 1 Rakesh babu Podishetty

Python Collections - named tuple is to access by the names specified and deque is to append and pop the elements from both sides of ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties