Building XML Using Python

XML Building

Building XML using Python programming language means creating an XML file or XML string using Python. You might have seen how to parse or read an existing XML file or string using Python in my previous tutorial. Here you will see how to create an XML file or string using Python from scratch. I will not only create the XML file but also do pretty print the XML data. I have defined one method for pretty printing the XML elements.

Extensible Markup Language (XML) are the most widely used formats for data, because this format is very well supported by modern applications, and is very well suited for further data manipulation and customization. Therefore it is sometimes required to generate XML data using Python or other programming languages.

Prerequisites

Python 3.6.6/3.11.5

Preparing Workspace

Preparing your workspace is one of the first things that you can do to make sure that you start off well. The first step is to check your working directory.

When you are working in the Python terminal, you need first navigate to the directory, where your file is located and then start up Python, i.e., you have to make sure that your file is located in the directory where you want to work from.

Let’s move on to the example…

Project Directory

In the below image you see I have opened a cmd (command line tool) prompt and navigated to the directory where I have to create Python script for building XML using Python.

python

Python Script

Now I will create a python script (xml-builder.py) that will read the attached XML file in the above link and display the content in the console.

XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. I will be parsing the XML data using xml.etree.ElementTree. ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.

Here in the below Python XML builder script I import the required module. Then I define a method that does the task of pretty printing of the XML structure otherwise all will be written in one line and it would a bi difficult to read the XML file.

Next I create the root element called bookstore with attribute speciality that has value novel. Then I create sub-element of the root element called book with attribute style that has value autobiography and so on.

Finally I write the whole XML document into a file under the current directory where the Python script resides. I also include XML declaration and encoding as the first line of the XML structure.

import xml.etree.ElementTree as ET

#pretty print method
def indent(elem, level=0):
    i = "\n" + level*"  "
    j = "\n" + (level-1)*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for subelem in elem:
            indent(subelem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = j
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = j
    return elem

#root element
root = ET.Element('bookstore', {'specialty':'novel'})

#book sub-element
book = ET.SubElement(root, 'book', {'style':'autobiography'})

author = ET.SubElement(book, 'author')

firstName = ET.SubElement(author, 'first-name')
firstName.text = 'Joe'

lastName = ET.SubElement(author, 'last-name')
lastName.text = 'Bob'

award = ET.SubElement(author, 'award')
award.text = 'Trenton Literary Review Honorable Mention'

price = ET.SubElement(book, 'price')
price.text = str(12)

#magazine sub-element
magazine = ET.SubElement(root, 'magazine', {'style':'glossy', 'frequency':'monthly'})

price = ET.SubElement(magazine, 'price')
price.text = str(12)

subscription = ET.SubElement(magazine, 'subscription', {'price':'24', 'per':'year'})

#write to file
tree = ET.ElementTree(indent(root))
tree.write('bookstore2.xml', xml_declaration=True, encoding='utf-8')

Testing the XML Building Program

Now it’s time to test for the example on building XML using Python.

Simply run the above script you should see the generated bookstore2.xml file in the current directory. here is the below screen-shot of the output XML file.

python xml

That’s all. Hope, you got idea on building XML using Python.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *