如何在命令行Linux中漂亮地打印和格式化XML?

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

xmllintlibxml2-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
Xmllint
Xmllint
Xmllint

Xml_pp (Xml_pp)

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

xml_ppxml-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
Xmlstarlet
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.

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
Tidy
Tidy
整洁

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

如我们所见,tidy在打印格式化的xml之前会提供错误或警告信息。

LEARN MORE  What Is RSS (Really Simple Syndication)?
了解更多什么是RSS(真正简单的联合组织)?

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()'
Python XML Library
Python XML Library
Python XML库

翻译自: https://www.poftut.com/pretty-print-format-xml-command-line-linux/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值