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.
XML或可扩展标记语言是流行的数据结构,用于以可交换格式表示数据。 它主要用于Web服务之间的配置和数据交换。 通过命令行在Linux中检查某些XML时,我们通常需要以更具可读性和结构化的方式进行打印。 在本教程中,我们将研究如何在Linux命令行中漂亮地打印XML。
Xmllint (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
xmllint
是libxml2-utils
软件包提供的工具。 它可以像下面这样安装,用于基于Deb的发行版,如Debian,Ubuntu,Mint和Kali
$ sudo apt install libxml2-utils
Or rpm based distributions like Fedora, CentOS, RedHat
或基于rpm的发行版,如Fedora,CentOS,RedHat
$ sudo yum install libxml2-utils
We can format xml file named data.xml
like below by providing --format
option.
通过提供--format
选项,我们可以像下面这样格式化名为data.xml
xml文件。
$ xmllint --format data
Xml_pp (Xml_pp)
xml_pp
is provided by xml-twig-tools
and can be installed like below.
xml_pp
由xml-twig-tools
xml_pp
xml-twig-tools
,可以如下安装。
$ 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
提供任何选项,仅提供如下所示的XML文件名。
$ xml_pp data.xml
Xmlstarlet (Xmlstarlet)
xmlstarlet
is single tool which can format XML in different ways. We can install xmlstarlet for deb based distributions like below.
xmlstarlet
是可以以不同方式格式化XML的单个工具。 我们可以为基于deb的发行版安装xmlstarlet,如下所示。
$ sudo apt install xmlstarlet -y
Or rpm based distribuıtions
或基于rpm的分配
$ sudo yum install xmlstarlet -y
We will use format
and --indent-tab
for the command and tab indentation.
我们将format
和--indent-tab
用于命令和选项卡的缩进。
$ xmlstarlet format --indent-tab data.xml
整洁 (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.
tidy
工具开发用于读取和写入HTMLiXHTML和XML文件的清理标记。 我们可以像下面那样为Ubuntu,Debian,Mint和Kali安装。
$ sudo apt install tidy -y
OR for Fedora, CentOS, RedHat
或用于Fedora,CentOS,RedHat
$ sudo yum install tidy -y
We will use -xml
to specify the file data format and -i
for indentation.
我们将使用-xml
指定文件数据格式,并使用-i
进行缩进。
$ tidy -xml -i data.xml
As we can see tidy provides error or warning information before printing formatted xml.
如我们所见,tidy在打印格式化的xml之前会提供错误或警告信息。
Python 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
.
是的,python有很多库,其中之一是xml
库,可用于创建或解析XML数据。 我们可以使用xml.dom.minidom
模块的parseString()
函数,如下所示。 我们将重定向名为data.xml
的XML文件的数据。
$ cat data.xml | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'
翻译自: https://www.poftut.com/pretty-print-format-xml-command-line-linux/