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>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like

Error While Parsing Json using Python – TypeError: list indices must be integers, not str

Jotting this down for fellow googlers. I recently started learning a bit…

Makefile Tricks: Arithmetic – Addition, Subtraction, Multiplication, Division, Modulo, Comparison

While working on a project, I needed to do some calculations inside…