📜 Beautiful XML output on Linux command line – IT is good

📜 Beautiful XML output on Linux command line – IT is good

Although XML (Extensible Markup Language) is a human readable markup language, you will most likely find XML files so unstructured that your head hurts!

This is what the output of the cat command will look like if we want to look at the XML document

Output XML files in the terminal

Before jumping into the process, let me share the unstructured XML that I’m going to be using throughout the tutorial.

The sample mydomain.xml file contains a lot of ips and FQDNs and I’m going to format this file to reduce the headache!

So let’s start with the first method:

Method 1: Using xmllint to Pretty XML Output

The xmllint utility provides us with a –format option with which users can reformat their XML files.

To reformat XML files with xmllint, you need to follow the following syntax:

xmllint --format XML_FILE

For demonstration, I will use mydomain.xml file:

xmllint --format mydomain.xml

By default, two spaces are added, but what if you want to add more?

You can use the XMLLINT_INDENT environment variable.

For example, I will add four spaces in my mydomain.xml:

XMLLINT_INDENT="    " xmllint --format mydomain.xml

Method 2: Using the XMLStarlet Toolkit

XMLStarlet is a set of command line utilities that serve a variety of purposes.

It has an xml command that can do just about anything to do with XML files!

But it requires manual installation, and there is a snap package to get started:

sudo snap install xmlstarlet

And to format XML files, you just need to pair the format option with the xml command, as shown below:

xml format email.xml

As I mentioned, it gives us various options and there are four formatting options:

  • -n will not intensify the output. This means that no space will be added and the result will be similar to left-aligned text.
  • -t will style the output with tabs for better visibility.
  • -o omits the XML declaration. This is nothing more than adding at the top of the XML file.
  • -s adds a space. So you decide how many spaces you need!

For example, I’ll add six spaces intend with fo to allow formatting:

xml fo -s 6 email.xml

Method 3: Using the xml_pp Command

This is the least flexible option when it comes to formatting XML files because xml_pp is part of a Perl module called XML::Twig.

And this is the only option that modifies the file directly.

But it requires manual installation, and if you’re on a Debian-based distribution, this command will help you get the job done:

sudo apt install xml-twig-tools

For example, I will use the entry style to format mydomain.xml:

Here, the -i option is used to make changes to the source file, while it is intended to attach an extension to it.

And the -s option is used to use the style options, and I chose the entry with the style name.


In this tutorial, I’ve explained how you can format your XML files to make them more readable.

And if you have any questions, ask them in the comments section.

HOWTO,AlmaLinux,Arch Linux,CentOS,Debian,Fedora:,Linux,OpenSUSE,Red Hat,RHEL,Rocky Linux,Scientific Linux,Ubuntu,

#Beautiful #XML #output #Linux #command #line #good

Leave a Comment

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