如何从命令行漂亮地打印XML?

本文翻译自:How to pretty print XML from the command line?

Related: How can I pretty-print JSON in (unix) shell script? 相关: 如何在(unix)shell脚本中漂亮地打印JSON?

Is there a (unix) shell script to format XML in human-readable form? 是否有一个(unix)shell脚本来以人类可读的格式格式化XML?

Basically, I want it to transform the following: 基本上,我希望它可以转换以下内容:

<root><foo a="b">lorem</foo><bar value="ipsum" /></root>

... into something like this: ...变成这样:

<root>
    <foo a="b">lorem</foo>
    <bar value="ipsum" />
</root>

#1楼

参考:https://stackoom.com/question/15Vy9/如何从命令行漂亮地打印XML


#2楼

libxml2-utils

This utility comes with libxml2-utils : 该实用程序随附libxml2-utils

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xmllint --format -

Perl's XML::Twig Perl的XML::Twig

This command comes with XML::Twig module, sometimes xml-twig-tools package: 此命令随附XML :: Twig 模块,有时还包含xml-twig-tools软件包:

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xml_pp

xmlstarlet

This command comes with xmlstarlet : 该命令随xmlstarlet一起xmlstarlet

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xmlstarlet format --indent-tab

tidy

Check the tidy package: 检查tidy包装:

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    tidy -xml -i -

Python 蟒蛇

Python's xml.dom.minidom can format XML (both python2 and python3): Python的xml.dom.minidom可以格式化XML(python2和python3):

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())'

saxon-lint

You need saxon-lint : 您需要saxon-lint

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    saxon-lint --indent --xpath '/' -

saxon-HE

You need saxon-HE : 您需要saxon-HE

 echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
    -s:- -qs:/ '!indent=yes'

#3楼

You didn't mention a file, so I assume you want to provide the XML string as standard input on the command line. 您没有提到文件,所以我假设您想在命令行上提供XML字符串作为标准输入。 In that case, do the following: 在这种情况下,请执行以下操作:

$ echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | xmllint --format -

#4楼

xmllint --format yourxmlfile.xml

xmllint is a command line XML tool and is included in libxml2 ( http://xmlsoft.org/ ). xmllint是命令行XML工具,包含在libxml2http://xmlsoft.org/ )中。

================================================ ===============================================

Note: If you don't have libxml2 installed you can install it by doing the following: 注意:如果尚未安装libxml2 ,则可以通过以下操作进行安装:

CentOS CentOS的

cd /tmp
wget ftp://xmlsoft.org/libxml2/libxml2-2.8.0.tar.gz
tar xzf libxml2-2.8.0.tar.gz
cd libxml2-2.8.0/
./configure
make
sudo make install
cd

Ubuntu 的Ubuntu

sudo apt-get install libxml2-utils

Cygwin 西格温

apt-cyg install libxml2

MacOS 苹果系统

To install this on MacOS with Homebrew just do: brew install libxml2 要使用Homebrew在MacOS上安装,只需: brew install libxml2

Git 吉特

Also available on Git if you want the code: git clone git://git.gnome.org/libxml2 如果需要代码,也可以在Git上使用: git clone git://git.gnome.org/libxml2


#5楼

You can also use tidy , which may need to be installed first (eg on Ubuntu: sudo apt-get install tidy ). 您也可以使用tidy ,它可能需要先安装(例如在Ubuntu上:sudo apt-get install tidy )。

For this, you would issue something like following: 为此,您将发出如下内容:

tidy -xml -i your-file.xml > output.xml

Note: has many additional readability flags, but word-wrap behavior is a bit annoying to untangle ( http://tidy.sourceforge.net/docs/quickref.html ). 注意:具有许多其他可读性标志,但是自动换行的行为有点令人讨厌,无法解开( http://tidy.sourceforge.net/docs/quickref.html )。


#6楼

xmllint support formatting in-place : xmllint支持就地格式化

for f in *.xml; do xmllint -o $f --format $f; done

As Daniel Veillard has written: 正如Daniel Veillard所写:

I think xmllint -o tst.xml --format tst.xml should be safe as the parser will fully load the input into a tree before opening the output to serialize it. 我认为xmllint -o tst.xml --format tst.xml应该是安全的,因为解析器会在打开输出以对其进行序列化之前将输入完全加载到树中。

Indent level is controlled by XMLLINT_INDENT environment variable which is by default 2 spaces. 缩进级别由XMLLINT_INDENT环境变量控制,默认情况下为2个空格。 Example how to change indent to 4 spaces: 示例如何将缩进更改为4个空格:

XMLLINT_INDENT='    '  xmllint -o out.xml --format in.xml

You may have lack with --recover option when you XML documents are broken. 当XML文档损坏时,可能缺少--recover选项。 Or try weak HTML parser with strict XML output: 或者尝试使用具有严格XML输出的弱HTML解析器:

xmllint --html --xmlout <in.xml >out.xml

--nsclean , --nonet , --nocdata , --noblanks etc may be useful. --nsclean ,-- --nonet ,-- --nocdata ,-- --noblanks等可能有用。 Read man page. 阅读手册页。

apt-get install libxml2-utils
apt-cyg install libxml2
brew install libxml2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值