How to prettify XML documents at the terminal

Recently, we have published a post about prettifying JSON documents at the console window which is likely useful for many developers. In this post using some ideas from this question, we want to present the same purpose but the documents are now XML. The post won’t get long and please take your drink and enjoy it.

Using Unixed-based commands

To test all commands in this section, you can copy the sample emails.xml and save it in a folder of your interest.

cat emails.xml
<emails> <email> <from>Kai</from> <to>Amanda</to> <time>2018-03-05</time>
<subject>I am flying to you</subject></email> <email>
<from>Jerry</from> <to>Tom</to> <time>1992-08-08</time> <subject>Hey Tom, catch me if you can!</subject>
</email> </emails>

With the xmllink command of libxml2-utils package

The xmllint is a command line XML tool and is included in libxml2 (http://xmlsoft.org/).

With the tidy command

You can also use tidy, which may need to be installed first (e.g. on Ubuntu: sudo apt-get install tidy).

For this, you would issue something like the following:

tidy -xml -i your-file.xml > output.xml

Note: has many additional readability flags, but word-wrap behaviour is a bit annoying to untangle (http://tidy.sourceforge.net/docs/quickref.html).

With the xidel command

For examples:

$ xidel -s input.xml -e . --output-node-format=xml --output-node-indent

$ xidel -s input.xml -e 'serialize(.,{"indent":true()})'

$ echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | \
  xidel -se . --output-node-format=xml --output-node-indent

$ echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | \
  xidel -se 'serialize(.,{"indent":true()})'

Notes: on MacOS, we can install this command by running brew install xidel. It’s easy peasy.

With the xmlscarlet command

On MacOS, install this command by running brew install xmlstarlet and then using it as a standard command.

xmlstarlet format --indent-tab emails.xml
<?xml version="1.0"?>
<emails>
    <email>
        <from>Kai</from>
        <to>Amanda</to>
        <time>2018-03-05</time>
        <subject>I am flying to you</subject>
    </email>
    <email>
        <from>Jerry</from>
        <to>Tom</to>
        <time>1992-08-08</time>
        <subject>Hey Tom, catch me if you can!</subject>
    </email>
</emails>

With the yq command

On MacOS, install this command by running brew install yq  and then using it as a standard command.

yq -P emails.xml
<emails>
  <email>
    <from>Kai</from>
    <to>Amanda</to>
    <time>2018-03-05</time>
    <subject>I am flying to you</subject>
  </email>
  <email>
    <from>Jerry</from>
    <to>Tom</to>
    <time>1992-08-08</time>
    <subject>Hey Tom, catch me if you can!</subject>
  </email>
</emails>

With the xmlformat command

On MacOS, install this command by running brew install xmlformat  and then using it as a standard command.

cat emails.xml
<emails> <email> <from>Kai</from> <to>Amanda</to> <time>2018-03-05</time>
<subject>I am flying to you</subject></email> <email>
<from>Jerry</from> <to>Tom</to> <time>1992-08-08</time> <subject>Hey Tom, catch me if you can!</subject>
</email> </emails>

However, using xmlformatcommand displays this XML document nicer.

cat emails.xml| xmlformat
<emails>
 <email>
  <from>Kai</from>
  <to>Amanda</to>
  <time>2018-03-05</time>
  <subject>I am flying to you</subject>
 </email>
 <email>
  <from>Jerry</from>
  <to>Tom</to>
  <time>1992-08-08</time>
  <subject>Hey Tom, catch me if you can!</subject>
 </email>
</emails>

Using Python-based commands

Python’s xml.dom.minidom

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | python -c 'import sys, xml.dom.minidom; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml())'

More readings

[1] How to Pretty-Print XML From the Command Line, accessed on 24 Mar 2024

[2] Use XMLStarlet to parse XML in the Linux terminal, accessed on 24 Mar 2024

That’s it!

If you find other ways to prettify XML documents, please don’t hesitate to discuss your ideas in the comment box below this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

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