How to Parse or View XML Code in Linux Command Line

XML is an abbreviation for Extensible Markup Language. Since XML is both a markup language and a file format, its usage is paramount in the storage, transmission, and reconstruction of arbitrary data. XML-defined set of rules makes it possible to encode documents in machine-readable and human-readable formats.

There is a downside to XML being attributed as a human-readable language. It is challenging to read and write due to its unfriendly format. For instance, you will find it difficult to visually comprehend a single long line of XML code when it lacks element indentations.

For instance, consider the view of the file XML code under a Linux terminal.

$ sudo nano mailing.xml
View XML Content in Linux
View XML Content in Linux

The screen capture above details a valid XML file. However, due to its irregular format, the human eye finds it difficult to read and understand it.
Throughout this tutorial, we will be referencing this file as our input file before pretty-printing it on our Linux command line shell environments.

Out of the many approaches to formatting, printing, and outputs an XML file on the Linux terminal, we will look into two ideal solutions:

1. Parsing XML Files Using xmllint Command

The xmllint command is part of the xmllib2 package with a primary role of checking XML files validity, evaluating XPath expressions, and parsing XML files.

The --format option in the xmllint command helps reformat and re-indent a targeted XML file as per the following syntax:

$ xmllint --format TARGETED_XML_FILE 

Let us use the xmllint command to reformat our sample mailing.xml file.

$ xmllint --format mailing.xml 
Parsing XML Files in Linux
Parsing XML Files in Linux

The command execution above has added an XML declaration (<?xml version=”1.0″) on top of making the file easy to read and understand through effective indentations.

2. View XML Data Using XMLStarlet Command

The xmlstarlet command-line-based XML toolkit is associated with a single xml executable. It first needs to be installed on your Linux distribution.

$ sudo apt install xmlstarlet         [On Debian, Ubuntu and Mint]
$ sudo yum install xmlstarlet         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a app-text/xmlstarlet  [On Gentoo Linux]
$ sudo pacman -S xmlstarlet           [On Arch Linux]
$ sudo zypper install xmlstarlet      [On OpenSUSE]    

The xmlstarlet command and its associated command options make it easier to validate, query, and transform XML files and documents. The reference syntax for the XMLStarlet Toolkit command is as follows:

$ xmlstarlet [<options>] <command> [<cmd-options>]

We can use the xmlstarlet command to pretty-print our mailing.xml file in the following manner.

$ xmlstarlet format mailing.xml 
View XML Content in Linux
View XML Content in Linux

The man page of the xmlstarlet toolkit provides more formatting options for your XML file.

$ man xmlstarlet

With these discussed approaches to pretty-printing XML files in Linux, you should have no problem expanding your knowledge on the usage of these commands after visiting their associated man pages.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.