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.
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:
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
xmllint --format email.xml
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
Saw the difference?
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
And as I mentioned earlier, it avails us various options and there are four formatting options:
-nwill 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.
-twill intend the output with tabs for better visibility.
-oomits the XML declaration. It's nothing but adding
<?xml version=”1.0″?>at the top of the XML file.
-sadds 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
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
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
-i is used to make changes in the original file while it is purposed to attach an extension with it.
-s option is to use styling options and I went with the style name
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.