Easily and Uniformly Indent XML Files using xmllint and a small hack

xmllint has the --format option using which you can indent XML files. However the indentation doesn’t always work. For eg, it refuses to indent a file in the below format

<?xml version="1.0"?>
<status>
  <xyz arg1="1">
 a 
</xyz>
  <abc arg2="2">
 p
 </abc>
  <pqr arg3="3" arg4="a phrase">
 x
 </pqr>
</status>

To make it indent all well-formed files uniformly, use this command: (Assuming the file you want to indent is called test.xml)

cat test.xml | tr '\n' ' ' | xmllint --format - > /tmp/test.xml; mv /tmp/test.xml test.xml

The indented output will look like:

<?xml version="1.0"?>
<status>
  <xyz arg1="1"> a </xyz>
  <abc arg2="2"> p </abc>
  <pqr arg3="3" arg4="a phrase"> x </pqr>
</status>