How to use xmllint In XML

The xmllint utility, part of the xmllib2 package, is commonly pre-installed on Linux distributions. If you need a simple and widely available solution, go with xmllint. It offers the –format option to reformat and reindent XML files. It is a versatile tool commonly used for checking XML validity, parsing XML files, and evaluating XPath expressions.

Example: The example shows the pretty print XML from the command line using xmllint.

XML
<data>
  <record>
    <name>Alice</name> <age>30</age></record>
  <record>
    <name>Bob</name><age>25</age>
  </record></data>

Run the command to produce a formatted version of the XML file:

 xmllint --format data.xml 

Output:

<?xml version="1.0"?>
<data>
<record>
<name>Alice</name>
<age>30</age>
</record>
<record>
<name>Bob</name>
<age>25</age>
</record>
</data>

How to Pretty Print XML from the Command Line?

To pretty print XML from the command line, we can employ two methods, xmllint and XMLStarlet. With xmllint, included in the libxml2-utils package. Alternatively, XMLStarlet, a command-line XML toolkit, offers the format subcommand to achieve the same result, enhancing XML file presentation.

Below are the approaches to pretty print XML from the command line:

Table of Content

  • Using xmllint
  • Using the XMLStarlet Toolkit

Similar Reads

Using xmllint

The xmllint utility, part of the xmllib2 package, is commonly pre-installed on Linux distributions. If you need a simple and widely available solution, go with xmllint. It offers the –format option to reformat and reindent XML files. It is a versatile tool commonly used for checking XML validity, parsing XML files, and evaluating XPath expressions....

Using the XMLStarlet Toolkit

XMLStarlet offers a wider range of functionalities for XML processing. These include XML transformation, querying data within the XML, validating its structure, and even editing the contents. For pretty-printing, it utilizes the xml format subcommand. It offers other formatting options like omitting the XML declaration or using tabs for indentation....