xmllint from libxml2

xmllint --format file.xml

(On Debian-based distributions, install the libxml2-utils package.)

xml_pp from the XML::Twig perl module

xml_pp < file.xml

(On Debian-based distributions, install the xml-twig-tools package.)

XMLStarlet

xmlstarlet format --indent-tab file.xml

Tidy

tidy -xml -i -q file.xml

Python's xml.dom.minidom

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
  python -c 'import sys, xml.dom.minidom; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml())'

saxon-lint (my own project)

saxon-lint --indent --xpath '/' file.xml

saxon-HE

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
  java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
       -s:- -qs:/ '!indent=yes'

xidel

xidel --output-node-format=xml --output-node-indent -se . -s file.xml

(Credit to Reino)

Output for all commands:

<root>
  <foo a="b">lorem</foo>
  <bar value="ipsum"/>
</root>
Answer from Gilles Quénot on Stack Overflow
🌐
Baeldung
baeldung.com › home › files › file viewing › how to pretty-print xml from the command line
How to Pretty-Print XML From the Command Line | Baeldung on Linux
March 18, 2024 - The xml_pp command is shipped with the Perl module XML::Twig. The name xml_pp stands for “XML Pretty-Printer”.
Top answer
1 of 13
1240

xmllint from libxml2

xmllint --format file.xml

(On Debian-based distributions, install the libxml2-utils package.)

xml_pp from the XML::Twig perl module

xml_pp < file.xml

(On Debian-based distributions, install the xml-twig-tools package.)

XMLStarlet

xmlstarlet format --indent-tab file.xml

Tidy

tidy -xml -i -q file.xml

Python's xml.dom.minidom

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
  python -c 'import sys, xml.dom.minidom; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml())'

saxon-lint (my own project)

saxon-lint --indent --xpath '/' file.xml

saxon-HE

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
  java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
       -s:- -qs:/ '!indent=yes'

xidel

xidel --output-node-format=xml --output-node-indent -se . -s file.xml

(Credit to Reino)

Output for all commands:

<root>
  <foo a="b">lorem</foo>
  <bar value="ipsum"/>
</root>
2 of 13
195

xmllint --format yourxmlfile.xml

xmllint is a command line XML tool and is included in libxml2 (http://xmlsoft.org/).

================================================

Note: If you don't have libxml2 installed you can install it by doing the following:

CentOS

cd /tmp
wget ftp://xmlsoft.org/libxml2/libxml2-2.8.0.tar.gz
tar xzf libxml2-2.8.0.tar.gz
cd libxml2-2.8.0/
./configure
make
sudo make install
cd

Ubuntu

sudo apt-get install libxml2-utils

Cygwin

apt-cyg install libxml2

macOS

To install this on macOS with Homebrew just do: brew install libxml2

Git

Also available on Git if you want the code: git clone git://git.gnome.org/libxml2

🌐
Linux Handbook
linuxhandbook.com › pretty-print-xml
Pretty Print XML in Linux Command Line
October 16, 2022 - 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:
🌐
GeeksforGeeks
geeksforgeeks.org › web tech › how-to-pretty-print-xml-from-the-command-line
How to Pretty Print XML from the Command Line? - GeeksforGeeks
July 23, 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.
🌐
Geekality
geekality.net › blog › pretty-print-xml-on-unix-command-line
Pretty print XML on Unix command line | Geekality
Needed to check some XML output from a CalDAV service so I used curl, which is nice and simple. Only problem was that all the XML came back on a single long unreadable line.
🌐
TutorialsPoint
tutorialspoint.com › how-to-pretty-print-xml-from-command-line
How to Pretty-Print XML From Command Line?
In this article, we will discuss three popular tools: xmllint, xmlstarlet, and tidy. xmllint is a command-line tool that is part of libxml2 library. It is available on most Linux and Unix systems, and can be installed ...
🌐
Alexwlchan
alexwlchan.net › 2015 › pretty-print
Pretty printing JSON and XML in the shell – alexwlchan
$ curl "http://xkcd.com/rss.xml" <?xml version="1.0" encoding="utf-8"?> <rss version="2.0"><channel><title>xkcd.com</title><link>http://xkcd.com/</link><description>xkcd.com: A webcomic of romance and math humor.</link></description><language>en</language><item><title>Red Car</title> ... $ curl "http://xkcd.com/rss.xml" | xmllint --format - <?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>xkcd.com</title> <link>http://xkcd.com/</link> <description>xkcd.com: A webcomic of romance and math humor.</description> <language>en</language> <item> <title>Red Car</title> ...
🌐
Linux Man Pages
linux.die.net › man › 1 › xml_pp
xml_pp(1): xml pretty-printer - Linux man page
xml_pp foo.xml > foo_pp.xml # pretty print foo.xml xml_pp < foo.xml > foo_pp.xml # pretty print from standard input xml_pp -v -i.bak *.xml # pretty print .xml files, with backups xml_pp -v -i'orig_*' *.xml # backups are named orig_<filename> xml_pp -i -p pre foo.xhtml # preserve spaces in pre tags xml_pp -i.bak -p 'pre code' foo.xml # preserve spaces in pre and code tags xml_pp -i.bak -p pre -p code foo.xml # same xml_pp -i -s record mydb_export.xml # pretty print using the record style xml_pp -e utf8 -i foo.xml # output will be in utf8 xml_pp -e iso-8859-1 -i foo.xml # output will be in iso-8859-1 xml_pp -v -i.bak -f lof # pretty print in place files from lof xml_pp -- -i.xml # pretty print the -i.xml file xml_pp -l foo.xml # loads the entire file in memory # before pretty printing it xml_pp -h # display help
Find elsewhere
🌐
Linux Hint
linuxhint.com › xml-pretty-print-linux-bash-and-python
XML Pretty Print in Linux Bash and Python – Linux Hint
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:
🌐
Linux Man Pages
linux.die.net › man › 1 › xmlpretty
xmlpretty(1): XML pretty printer - Linux man page
$ xmlpretty --PrettyWhiteNewline \ --PrettyWhiteIndent \ --CatchEmptyElement \ uglyfile.xml > prettyfile.xmlYou may use YAWriter to clean whitespace from XML documents. This may work in 99% of the cases where you want to get rid of ignorable whitespace caused by the various forms of pretty printing.
🌐
Codidact
linux.codidact.com › posts › 291099
Prettify XML in a shell - Linux Systems
I have command-line tool which outputs XML in a single line, totally unreadable. I would like something to pipe this into, to turn it into human readable XML with newlines and indentation. ... $ output-dirty-xml <outer><inner some="value">Hey there!</inner></outer> $ output-dirty-xml | prettify-xml <outer> <inner some="value">Hey there!</inner> </outer>
🌐
Ubuntu
manpages.ubuntu.com › manpages › questing › man1 › xml_pp.1p.html
Ubuntu Manpage: xml_pp - xml pretty-printer
xml_pp foo.xml > foo_pp.xml # pretty print foo.xml xml_pp < foo.xml > foo_pp.xml # pretty print from standard input xml_pp -v -i.bak *.xml # pretty print .xml files, with backups xml_pp -v -i'orig_*' *.xml # backups are named orig_<filename> xml_pp -i -p pre foo.xhtml # preserve spaces in pre tags xml_pp -i.bak -p 'pre code' foo.xml # preserve spaces in pre and code tags xml_pp -i.bak -p pre -p code foo.xml # same xml_pp -i -s record mydb_export.xml # pretty print using the record style xml_pp -e utf8 -i foo.xml # output will be in utf8 xml_pp -e iso-8859-1 -i foo.xml # output will be in iso-8859-1 xml_pp -v -i.bak -f lof # pretty print in place files from lof xml_pp -- -i.xml # pretty print the -i.xml file xml_pp -l foo.xml # loads the entire file in memory # before pretty printing it xml_pp -h # display help
🌐
Coderwall
coderwall.com › p › tsbudw › pretty-print-xml-in-command-line
Pretty print xml in command line (Example)
September 27, 2021 - #command line · #linux · #xml · cat my.xml | xmllint --format - #command line · #linux · #xml · Say Thanks · Respond · 688 · 0 · 72.95K · 6 · 39.25K · 0 · Post · Post a tip · Best #Command line Authors · austinkeeley · 72.86K ...
🌐
Superhero
superhero.ninja › 2013 › 04 › 24 › pretty-print-xml-on-linux-command-line-bash
Pretty print XML on Linux Command Line BASH – Superhero Ninja
It’s quite handy to indent XML when you need to read it with the human eye, but on a terminal it’s often not as easily readible. Fortunatly there’s a command which’ll indent it so you can actually read it; xmllint –format file.xml This will show you how ugly it could be:
🌐
LibreByte
librebyte.net › en › multimedia-en › how-to-pretty-print-xml-on-gnulinux
How to pretty print XML on GNU/Linux | LibreByte
April 22, 2016 - Tool 2: use xml_pp, xml_pp is part of the XML-Twig suite, to install it on Debian GNU/Linux type: ... If you want to edit the formatted file, you can use any text editor. Tool 3: Use Geany editor and prettyprinter plugin, to install it on Debian GNU/Linux type: $ sudo apt-get install geany geany-plugin-prettyprinter · After running the above command, you must perform the following steps to activate the plugin
🌐
SysTutorials
systutorials.com › docs › linux › man › 1-xml_pp
xml_pp: xml pretty-printer - Linux Manuals (1)
xml_pp foo.xml > foo_pp.xml # pretty print foo.xml xml_pp < foo.xml > foo_pp.xml # pretty print from standard input xml_pp -v -i.bak *.xml # pretty print .xml files, with backups xml_pp -v -i'orig_*' *.xml # backups are named orig_<filename> xml_pp -i -p pre foo.xhtml # preserve spaces in pre tags xml_pp -i.bak -p 'pre code' foo.xml # preserve spaces in pre and code tags xml_pp -i.bak -p pre -p code foo.xml # same xml_pp -i -s record mydb_export.xml # pretty print using the record style xml_pp -e utf8 -i foo.xml # output will be in utf8 xml_pp -e iso-8859-1 -i foo.xml # output will be in iso-8
🌐
codestudy
codestudy.net › blog › how-to-pretty-print-xml-from-the-command-line
How to Pretty Print XML from the Command Line: A Unix Shell Script Guide to Transform Minified XML into Readable Format — codestudy.net
xmllint is a command-line XML parser and validator included with libxml2, a popular XML library. It’s preinstalled on most Linux distributions and macOS, making it a go-to choice. ... xmlstarlet is a powerful XML command-line toolkit that supports validation, editing, transformation, and pretty printing (via xmlstarlet fo, short for "format output").