From one row xml to nice print xml in a jiffy with xmllint

When I get to work with xml they are usually all on one row. This makes it impossible (or at least very hard) to read them. One solution I found was the superb –format option. This option will make the xmllint program to output the one row xml to a multiple nice print xml document.

Example
Our ugly one row xml (ugly.xml):

<?xml version="1.0" encoding="utf-8"?><movies><movie><title>The Brave One</title><genre>Thriller</genre></movie><movie><title>Instinct</title><genre>Drama</genre></movie></movies>

Run the command:

xmllint --format ugly.xml

or(if no file):

echo '<?xml version="1.0" encoding="utf-8"?><movies><movie><title>The Brave One</title><genre>Thriller</genre></movie><movie><title>Instinct</title><genre>Drama</genre></movie></movies>' | xmllint --format -

And you get:

<?xml version="1.0" encoding="utf-8"?>
<movies>
  <movie>
    <title>The Brave One</title>
    <genre>Thriller</genre>
  </movie>
  <movie>
    <title>Instinct</title>
    <genre>Drama</genre>
  </movie>
</movies>

Much better!

Pipe result into Vim

xmllint --format ugly.xml | vim -

Note the last ‘-‘ sign as it tells vim to read input from standard input.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

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