How to Pretty Print XML from the Command Line?
Last Updated :
16 May, 2024
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:
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.
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>
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.
For Installation of XMLStarlet Toolkit:
sudo apt install xmlstarlet
Example: The example shows the pretty print XML from the command line of the XMLStarlet Toolkit.
XML
<emails><email><from>Elon</from><to>Jeff</to>
<time>2024-04-21</time><subject>
Space Travel Partnership
</subject><content>Hey Jeff, I've been
exploring some fascinating articles on
GeeksforGeeks related
to space travel.
</content></email>
<email><from>Sundar</from><to>Tim</to><time>
2024-04-22</time><subject>AI Collaboration
</subject>
<content>Hi Tim, I believe our companies can
achieve great synergy through AI collaboration.
</content></email></emails>
Run the command to produce a formatted version of the XML file:
xml format emails.xml
Output:
<?xml version="1.0"?>
<emails>
<email>
<from>Elon</from>
<to>Jeff</to>
<time>2024-04-21</time>
<subject>Space Travel Partnership</subject>
<content>Hey Jeff, I've been exploring some fascinating articles on GeeksforGeeks related to space travel.</content>
</email>
<email>
<from>Sundar</from>
<to>Tim</to>
<time>2024-04-22</time>
<subject>AI Collaboration</subject>
<content>Hi Tim, I believe our companies can achieve great synergy through AI collaboration.</content>
</email>
</emails>
Similar Reads
Pretty Printing XML in Python XML stands for Extensible Markup Language and is used to encode a document that can be understandable by both humans and machines. Data stored in XML format is easy to understand and easy to modify. There are three main things that have to keep in mind when you are using XML â Simplicity, Generality
2 min read
How to Convert XML to DataFrame in R? A Data Frame is a two-dimensional and tabular data structure in the R that is similar to a table in the database or an Excel spreadsheet. It is one of the most commonly used data structures for the data analysis in R with the columns representing the various and rows representing the observations.XM
4 min read
How to Parse and Modify XML in Python? XML stands for Extensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. Thatâs why, the design goals of XML emphasize simplicity, generality, and usability across the Internet. Note: For more information, refer to XML | Basics H
4 min read
How to convert lists to XML in Python? In this article, the task is to convert a given list to XML in Python. But first, let's discuss what is an XML? It could also be terminology that defines a gaggle of rules for encoding documents during a format that's both human-readable and machine-readable. The design goals of XML specialize in si
3 min read
How to Convert Excel to XML Format in Python? Python proves to be a powerful language when the requirement is to convert a file from one format to the other. It supports tools that can be employed to easily achieve the functionality. In this article, we'll find out how we will convert from an Excel file to Extensible terminology (XML) files wit
3 min read
How to Read XML File in R? XML (Extensible Markup Language) can be a widely used format for storing and transporting data. It can be structured and allowing the both humans and machines to easily parse and interpret the data it contains. In R programming, reading and processing XML files is straightforward thanks to the vario
5 min read
How to open an XML file ? XML stands for eXtensible Markup Language. It defines the format of data and it is similar to HTML as both are markup languages but while HTML has a predefined set of standard tags, XML has user-defined tags, although has a starting standard tag: <?xml version=â1.0â encoding=âUTF-8â?>XML is a
3 min read
How to parse and process HTML/XML using PHP ? In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is use
2 min read
How to parse XML in Ruby? XML - Extensible Markup Language is used format on the platform for exchanging data and storing data on the web. Ruby, consists of a huge number of libraries and its syntax is flexible does provides several methods for parsing XML documents easily. In this article, we will look into various methods
2 min read
Test Execution Results in XML in Pytest The testing framework in Python which is used to write and execute test codes is called Pytest. There occur some circumstances in Pytest when we need to store the test execution results in an XML file apart from just displaying them in the terminal. In this article, we will discuss how to store the
2 min read