How to Pretty Print XML from the Command Line?

Last Updated : 23 Jul, 2025

To pretty print XML from the command line, we can employ two methods, xmllint and XMLStarlet. With xmllint, included in the libxml2-utils package. Alternatively, XMLStarlet, a command-line XML toolkit, offers the format subcommand to achieve the same result, enhancing XML file presentation.

Below are the approaches to pretty print XML from the command line:

Using xmllint

The xmllint utility, part of the xmllib2 package, is commonly pre-installed on Linux distributions. If you need a simple and widely available solution, go with xmllint. It offers the --format option to reformat and reindent XML files. It is a versatile tool commonly used for checking XML validity, parsing XML files, and evaluating XPath expressions.

Example: The example shows the pretty print XML from the command line using xmllint.

XML
<data>
  <record>
    <name>Alice</name> <age>30</age></record>
  <record>
    <name>Bob</name><age>25</age>
  </record></data>

Run the command to produce a formatted version of the XML file:

 xmllint --format data.xml 

Output:

<?xml version="1.0"?>
<data>
<record>
<name>Alice</name>
<age>30</age>
</record>
<record>
<name>Bob</name>
<age>25</age>
</record>
</data>

Using the XMLStarlet Toolkit

XMLStarlet offers a wider range of functionalities for XML processing. These include XML transformation, querying data within the XML, validating its structure, and even editing the contents. For pretty-printing, it utilizes the xml format subcommand. It offers other formatting options like omitting the XML declaration or using tabs for indentation.

For Installation of XMLStarlet Toolkit:

 sudo apt install xmlstarlet

Example: The example shows the pretty print XML from the command line of the XMLStarlet Toolkit.

XML
<emails><email><from>Elon</from><to>Jeff</to>
  <time>2024-04-21</time><subject>
      Space Travel Partnership
  </subject><content>Hey Jeff, I've been 
  exploring some fascinating articles on
  GeeksforGeeks related 
  to space travel. 
  </content></email>
  <email><from>Sundar</from><to>Tim</to><time>
    2024-04-22</time><subject>AI Collaboration
    </subject>
    <content>Hi Tim, I believe our companies can
     achieve great synergy through AI collaboration. 
      </content></email></emails>

Run the command to produce a formatted version of the XML file:

xml format emails.xml 

Output:

<?xml version="1.0"?>
<emails>
<email>
<from>Elon</from>
<to>Jeff</to>
<time>2024-04-21</time>
<subject>Space Travel Partnership</subject>
<content>Hey Jeff, I've been exploring some fascinating articles on GeeksforGeeks related to space travel.</content>
</email>
<email>
<from>Sundar</from>
<to>Tim</to>
<time>2024-04-22</time>
<subject>AI Collaboration</subject>
<content>Hi Tim, I believe our companies can achieve great synergy through AI collaboration.</content>
</email>
</emails>
Comment