Python

XML Pretty Print in Linux Bash and Python

XML is designed as a human-readable markup language, but only if it is pretty formatted. If you open an XML file, you may fail to understand the content, especially for a large XML file. However, you can print an XML on Bash or Python to make it easily human-readable. This guide presents the different ways to print XML in Linux Bash and Python pretty.

How to Pretty Print XML in Linux Bash and Python

When working with Python, you may get a case where you want to print XML. It could be an external XML file or the XML code that is included in the Python script. For Linux Bash, you might want to open an XML file on your terminal to view its contents. Whatever the case, we will see the different ways to print the XML pretty.

1. Pretty Print XML in Python

Before we dig in on the options, the following is the XML file that we will work with for our example:

In Python, you can work with XML in two main ways. Suppose you have the given XML file as an external file and you want to print it pretty. You can import the “xml.dom.minidom” library. Once imported, you can use the Python open() to read the contents of your external file. After that, you can use the toprettyxml() to print your external XML file pretty.

In the following example, we use our external “details.xml” file and create different variables in the code:

We can then run our Python script on the terminal to get the pretty formatted output as illustrated in the following figure. Note how using the DOM Python library helps to neatly format the XML, making it easily readable by anyone who runs the Python script:

Alternatively, if you only have a small XML file, you can incorporate it directly into your Python script. You still need the DOM Python library to format the internal XML in your script. The function that is used is the same as in the case of working with an external XML file, only that we include the XML directly.

Look at how we include the XML in the following image:

You can then run your Python script as we did earlier. You will get the pretty print version of your XML file in your output as what we obtained in our following output:

This option only applies when your XML file has a few lines. For a large XML file, consider opening it as an external file.

2. Pretty Print XML in Bash

On your terminal, it’s possible to view the contents of files including XML. For instance, you can use the “cat” command to display the contents of our “details.xml” file. However, the output is unreadable due to poor formatting.

To pretty print this XML file, there are different tools that you can use. Here, we will cover two of the common ones. First, you can use the “xmlstarlet” which comes installed on most Linux systems. If it is not available on your system, run the following command to install it:

sudo snap install xmlstarlet

 

With “xmlstarlet” on your Linux system, you can XML pretty print your file as illustrated in the following example. Your output will be neatly formatted which makes it easier to read:

Alternatively, you can use “xmllint” to achieve the same. Start by installing it on your system with the following command:

sudo apt install libxml2-utils

 

Once installed, you can quickly print your XML file using the following syntax:

xmllint --format details.xml

 

Your XML will be formatted and easily readable as shown in the following example:

Suppose you want to create an output of the formatted XML. You can redirect the output to create a new XML file as shown in the following example. When you open the new file, you will see that you have the pretty printed version of the XML:

Conclusion

It’s possible to pretty print the XML files in Linux Bash or Python. With Python, you can pretty print the XML by incorporating it into your code or reading it as an external file. For Linux Bash, you can use different command line tools. This post has detailed the best options to use in either case. Try them out!

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.