Skip to main content

Pretty Print XML in Linux Command Line

XML is human-readable markup language but reading it in the terminal might be difficult. Learn to format XML content in a structured way.

Sagar Sharma

Warp Terminal

While XML (Extensible Markup Language) is a human-readable markup language, you'll more likely find XML files unstructured enough to give you headaches!

But first, let me share end results that you'd get after this guide:

how to Pretty print XML in Linux command line
The end results

And to accomplish these results, I've compiled this guide involving three methods of formatting the XML and making it human readable.

Pretty print XML files in the terminal

Before going through the process, let me share unstructured XML which I'm going to use throughout the tutorial:

<emails> <email> <from>Sagar</from> <to>Milan</to> <time>2022-10-14</time>
<subject>Writing a lot these days</subject></email> <email>
<from>Sagar</from> <to>Arvind</to> <time>2022-10-13</time> <subject>Heyy, send me the Goa pictures ASAP!</subject>
</email> </emails>

The sample file email.xml contains two emails and I'm going to format this file to reduce the usual headache I get! So let's start with the first one:

Method 1: Using xmllint to pretty print XML

The xmllint utility avails us the --format option by which users can re-format their XML files. To reformat XML files using xmllint, you just have to follow the given syntax:

xmllint --format XML_FILE

And for demonstration, I'll be using email.xml file:

xmllint --format email.xml
how to use xmllint to pretty-print XML in linux

And the default intent is to add two spaces but what if you want to add more? you can use XMLLINT_INDENT environment variable.

For example, I'll be adding four spaces in my email.xml:

XMLLINT_INDENT="    " xmllint --format email.xml
change default intend of XML styling in linux

Saw the difference?

DigitalOcean – The developer cloud
Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.
Get started on DigitalOcean with a $100, 60-day credit for new users.

Method 2: Using XMLStarlet toolkit

The XMLStarlet is a set of command line utilities serving different purposes. And it avails the xml command by which you can perform pretty much anything related to XML files!

But it requires manual installation and there's a snap package available to get you started:  

sudo snap install xmlstarlet

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

xml format email.xml
use xml command to pretty print XML in linux

And as I mentioned earlier, it avails us various options and there are four formatting options:

  • -n will not intend the output. This means it will add no space and the results will be similar to when you align text to the left.
  • -t will intend the output with tabs for better visibility.
  • -o omits the XML declaration. It's nothing but adding <?xml version=”1.0″?> at the top of the XML file.
  • -s adds intend the space. So you get to decide the number of space stokes you want!

For example, I'll be adding the six space intends using fo to allow formatting:

xml fo -s 6 email.xml
use xml command to style XML files in linux
Get a free, secure, and private email with Proton Mail
Proton Mail is the world’s largest secure email service with over 70 million users. Available on Web, iOS, Android, and desktop. Protected by Swiss privacy law.

Method 3: Using the xml_pp Command

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

And this is the only option that modifies the direct file which itself is pro. Isn't it?

But it requires manual installation and if you're on a Debian-based distro, the given command will get your job done:

sudo apt install xml-twig-tools

For example, I'll be using the record style for formatting email.xml:

use xml_pp command to pretty print XML in linux

Here, the -i is used to make changes in the original file while it is purposed to attach an extension with it.

And the -s option is to use styling options and I went with the style name record.

Final Words

Through this guide, I explained how you can format your XML files making them more readable.

And if you have any queries, style them in the comments section.

Sagar Sharma