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.

Comments are closed.

Niklas Tech Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.