Auto-formatting XML files

When you work with XML as much as I do, quite often you get sent a list of files that are not pretty-printed.  By "pretty-printed", I mean nicely formatted with each new element on a new line and each nested element indented appropriately.

The files not being nicely formatted makes using command-line tools like grep nigh-on impossible, so quite often you have to format the files before you can get any real work done.

I've been using the following script to automatically format whatever XML files I pass it:

#!/bin/bash
for a
do
xmllint --format $a  -o $a
done
exit 0

As you can see it just passes whatever arguments I pass to xmllint and over-writes the original in-place.

I've called my version of this file prettify.sh, and when it's in the path you can invoke it like this:

[craigf@eleanor ~]prettify.sh *.xml

Update: I should've mentioned that xmllint is part of the libxml2 package. It should be available on most Linux distributions.

blog comments powered by Disqus