How to format XML document in Linux

Aditya picture Aditya · Jan 22, 2014 · Viewed 23.7k times · Source

I have following XML tags in a large number.

<SERVICE>
<NAME>
sh_SEET15002GetReKeyDetails
</NAME>
<ID>642</ID>
</SERVICE>

I want to get this formatted in the following manner. I have tried using xmllint but it is not working for me. Please provide help.

<SERVICE>
<NAME>sh_SEET15002GetReKeyDetails</NAME>
<ID>642</ID>
</SERVICE>

Answer

mturra picture mturra · Feb 7, 2017
xmllint -format -recover nonformatted.xml > formated.xml

For tab indentation:

export XMLLINT_INDENT=`echo -e '\t'`

For four space indentation:

export XMLLINT_INDENT=\ \ \ \