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
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

Discussions

formatting - How to prettify/format an XML buffer? - Emacs Stack Exchange
For example, here is a command that will pretty-print the region, optionally with auto-fill enabled: (defun xml-pretty-print (beg end &optional arg) "Reformat the region between BEG and END. More on emacs.stackexchange.com
🌐 emacs.stackexchange.com
July 23, 2015
How to format XML document in Linux - Stack Overflow
I have following XML tags in a large number. sh_SEET15002GetReKeyDetails 642 I want to get this formatted in the More on stackoverflow.com
🌐 stackoverflow.com
an XML without LF want to make it pretty using sed command in shell - Unix & Linux Stack Exchange
I have a structure of a project in my git repository and would like to run a shell command in git bash to replace all the > with >\n (LF). I tried the following command: find . -name '*.xml'... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Pretty printing XML?
Hi, I have a buffer in form of a Vec containing XML. What is the easiest/best way to have it be pretty printed on stdout (or to a String)? I've had a look at xml-rs but I can't get my head around it...cry... Cheers, Toby More on users.rust-lang.org
🌐 users.rust-lang.org
1
0
June 1, 2022
🌐
Linux Handbook
linuxhandbook.com › pretty-print-xml
Pretty Print XML in Linux Command Line
October 16, 2022 - And to accomplish these results, I've compiled this guide involving three methods of formatting the XML and making it human readable.
🌐
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 - Below are the approaches to pretty print XML from the command line: ... 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.
🌐
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
🌐
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> ...
Find elsewhere
🌐
JSON Formatter
jsonformatter.org › xml-pretty-print
Best XML Pretty Print Online
Best and Secure XML Pretty Print works well in Windows, Mac, Linux, Chrome, Firefox, Safari and Edge.
🌐
TutorialsPoint
tutorialspoint.com › how-to-pretty-print-xml-from-command-line
How to Pretty-Print XML From Command Line?
March 23, 2023 - xmlstarlet is a command-line tool for processing XML data. It can be used to pretty-print XML as well as perform other tasks such as selecting nodes, updating values, and transforming XML data. xmlstarlet is available on most Linux and Unix ...
🌐
Linux Hint
linuxhint.com › xml-pretty-print-linux-bash-and-python
XML Pretty Print in Linux Bash and Python – Linux Hint
How to pretty print the XML files in Linux Bash or Python by incorporating it in your code, reading it as external file, or using different command line tools.
🌐
Zgadzaj
zgadzaj.com › development › linux › formatting-xml-from-command-line
Formatting XML from command line | Change(b)log
It doesn't make any real difference to XML reading libraries, but trying to read such XML yourself becomes huge pain. As it turns out, solution to this is pretty simple - xmllint command line tool.
🌐
JSON Formatter
jsonformatter.org › xml-formatter
Best XML Formatter and XML Beautifier
Best and Secure XML Formatter works well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge. Format XML File upload files and beautifies. Here are the steps: Step 1: Click on Load Data button. It will open model window / dialog.
🌐
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:
🌐
Superhero
superhero.ninja › 2013 › 04 › 24 › pretty-print-xml-on-linux-command-line-bash
Pretty print XML on Linux Command Line BASH – Superhero Ninja
April 24, 2013 - 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: Screenshot ...
🌐
Code Beautify
codebeautify.org › xml-pretty-print
XML Pretty Print
Pretty Print XML works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.