How To Pretty Print and Format XML In Command Line Linux?

XML or extensible markup language is popular data structure used to express data in a exchangeable format. It is mostly used for configuration and data exchange between web services. While inspecting some XML in Linux via command line we generally need to print in a more readable and structured way. In this tutorial we will look how to pretty print XML in Linux command line.

Xmllint

xmllint  is a tool provided by libxml2-utils  package. It can be installed like below for deb based distributions like Debian, Ubuntu, Mint and Kali

$ sudo apt install libxml2-utils

Or rpm based distributions like Fedora, CentOS, RedHat

$ sudo yum install libxml2-utils

We can format xml file named data.xml  like below by providing --format  option.

$ xmllint --format data
Xmllint
Xmllint

Xml_pp

xml_pp  is provided by xml-twig-tools  and can be installed like below.

$ sudo apt install xml-twig-tools

OR

$ sudo yum install xml-twig-tools

We can use xml_pp  wiothout providing any option just providing the XML file name like below.

$ xml_pp data.xml

Xmlstarlet

xmlstarlet  is single tool which can format XML in different ways. We can install xmlstarlet for deb based distributions like below.

$ sudo apt install xmlstarlet -y

Or rpm based distribuıtions

$ sudo yum install xmlstarlet -y

We will use format  and --indent-tab  for the command and tab indentation.

$ xmlstarlet format --indent-tab data.xml
Xmlstarlet
Xmlstarlet

Tidy

tidy  tool is developed to read and write cleaned-up markup for HTMLiXHTML and XML files. We can install for Ubuntu, Debian, Mint and Kali like below.

$ sudo apt install tidy -y

OR for Fedora, CentOS, RedHat

$ sudo yum install tidy -y

We will use -xml  to specify the file data format and -i  for indentation.

$ tidy -xml -i data.xml
Tidy
Tidy

As we can see tidy provides error or warning information before printing formatted xml.

Python XML Library

Yes python have a lot of libraries and one of them is xml  library which can be used to create or parse XML data. We can use xml.dom.minidom module parseString() functions like below. We will redirect data of the XML file named data.xml .

$ cat data.xml | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'
Python XML Library
Python XML Library

1 thought on “How To Pretty Print and Format XML In Command Line Linux?”

Leave a Comment