Format XML files on Linux


There are plenty of tools for dealing with XML on Linux. However the most popular one that's almost for sure is already installed on your Linux server (or a desktop?) is xmllint. That's a tool from libxml2 library. Still if you don't have it — just install libxml2. On CentOS it could be done with:

yum install libxml2

Now to format XML file just run:

xmllint --format document.xml

That will output formatted XML file into stdout. To output the result into a file instead of stdout just redirect the output into a file as you always do on Linux:

xmllint --format document.xml > formatted.xml

Also it's nice to notice that xmllint works pretty fast with huge XML files — it formats tens of gigabytes of XML ridiculously fast.

Rate this post:
Share this page:

See also how to:

How to extract files from zip archive in Linux command line
How to extract files from different tar archives in Linux command line
How to run your PHP or Bash script as root in the most secure way
How to create a well-crafted RSS feed for a website and configure it property
How to install PHP FastCGI Process Manager (FPM) on CentOS 7