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 Overflowxmllint 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>
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
Supercat and grcat (grc) can do piped syntax highlighting. You'll probably have to make or find configuration files for XML. They are both available in the Ubuntu repositories as well as at the links provided.
Pygmentize has XML highlighting included. It's available as "python-pygments" in the Ubuntu repositories or by using easy_install Pygments.
xmllint --format xmlfile.xml | pygmentize -l xml | less
This is how you do it using GNU source-highlight and less:
source-highlight -i /tmp/foo.xml -f esc | less -r