如何从命令行打印XML ?

时间:2022-05-26 21:42:59

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>

6 个解决方案

#1


655  

Try doing this :

试着这样做:

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

This utility comes with libxml2-utils

这个实用程序附带libxml2-utils

or

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

this command comes with XML::Twig module, sometimes xml-twig-tools package.

这个命令附带了XML::Twig perl模块,有时是XML - Twig工具包。

or

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

this command comes with xmlstarlet

此命令附带xmlstarlet

or

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

check tidy package

检查整理包

or

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()'

or

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

check saxon-lint

检查saxon-lint

or

 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'

Check saxon-HE

检查saxon-HE

#2


99  

xmllint --format yourxmlfile.xml

xmllint——格式yourxmlfile.xml

xmllint is a command line XML tool and is included in libxml2 (http://xmlsoft.org/).

xmllint是一个命令行XML工具,包含在libxml2 (http://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

sudo apt-get安装libxml2-utils

MacOS

MacOS

To install this on MacOS with Homebrew just do: brew install libxml2

要在带有Homebrew的MacOS上安装这个,只需安装libxml2

#3


28  

You can also use tidy, which may need to be installed first (e.g. 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)。

#4


9  

You didn't mention a file, so I assume you want to provide the XML string as standard input on the command line. In that case, do the following:

您没有提到文件,所以我假设您希望在命令行中提供XML字符串作为标准输入。在这种情况下,执行以下操作:

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

#5


9  

To have xmllint available on Debian systems, you need to install the package libxml2-utils (libxml2 does not provide this tool, at least not on Debian 5.0 "Lenny" and 6.0 "Squeeze").

要使Debian系统上的xmllint可用,您需要安装libxml2-utils包(libxml2不提供此工具,至少在Debian 5.0“Lenny”和6.0“Squeeze”上没有)。

#6


0  

xmllint support formatting in-place:

xmllint支持格式化就地:

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

As wrote Daniel Veillard:

正如丹尼尔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——格式结核菌素。xml应该是安全的,因为解析器将在打开输出以序列化输入之前将输入完全加载到树中。

Indent level is controlled by XMLLINT_INDENT environment variable which is by default 2 spaces. Example how to change indent to 4 spaces:

缩进级别由XMLLINT_INDENT环境变量控制,该变量默认为2个空格。如何将缩进更改为4个空格的示例:

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

You may have lack with --recover option when you XML documents are broken. Or try weak HTML parser with strict XML output:

当XML文档被破坏时,您可能缺少- recovery选项。或者尝试使用XML输出严格的弱HTML解析器:

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

--nsclean, --nonet, --nocdata, --noblanks etc may be useful. Read man page.

nsclean, nonet, nocdata, noblanks等都可能有用。阅读手册页。

apt-get install libxml2-utils
apt-cyg install libxml2
brew install libxml2

#1


655  

Try doing this :

试着这样做:

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

This utility comes with libxml2-utils

这个实用程序附带libxml2-utils

or

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

this command comes with XML::Twig module, sometimes xml-twig-tools package.

这个命令附带了XML::Twig perl模块,有时是XML - Twig工具包。

or

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

this command comes with xmlstarlet

此命令附带xmlstarlet

or

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

check tidy package

检查整理包

or

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()'

or

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

check saxon-lint

检查saxon-lint

or

 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'

Check saxon-HE

检查saxon-HE

#2


99  

xmllint --format yourxmlfile.xml

xmllint——格式yourxmlfile.xml

xmllint is a command line XML tool and is included in libxml2 (http://xmlsoft.org/).

xmllint是一个命令行XML工具,包含在libxml2 (http://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

sudo apt-get安装libxml2-utils

MacOS

MacOS

To install this on MacOS with Homebrew just do: brew install libxml2

要在带有Homebrew的MacOS上安装这个,只需安装libxml2

#3


28  

You can also use tidy, which may need to be installed first (e.g. 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)。

#4


9  

You didn't mention a file, so I assume you want to provide the XML string as standard input on the command line. In that case, do the following:

您没有提到文件,所以我假设您希望在命令行中提供XML字符串作为标准输入。在这种情况下,执行以下操作:

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

#5


9  

To have xmllint available on Debian systems, you need to install the package libxml2-utils (libxml2 does not provide this tool, at least not on Debian 5.0 "Lenny" and 6.0 "Squeeze").

要使Debian系统上的xmllint可用,您需要安装libxml2-utils包(libxml2不提供此工具,至少在Debian 5.0“Lenny”和6.0“Squeeze”上没有)。

#6


0  

xmllint support formatting in-place:

xmllint支持格式化就地:

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

As wrote Daniel Veillard:

正如丹尼尔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——格式结核菌素。xml应该是安全的,因为解析器将在打开输出以序列化输入之前将输入完全加载到树中。

Indent level is controlled by XMLLINT_INDENT environment variable which is by default 2 spaces. Example how to change indent to 4 spaces:

缩进级别由XMLLINT_INDENT环境变量控制,该变量默认为2个空格。如何将缩进更改为4个空格的示例:

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

You may have lack with --recover option when you XML documents are broken. Or try weak HTML parser with strict XML output:

当XML文档被破坏时,您可能缺少- recovery选项。或者尝试使用XML输出严格的弱HTML解析器:

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

--nsclean, --nonet, --nocdata, --noblanks etc may be useful. Read man page.

nsclean, nonet, nocdata, noblanks等都可能有用。阅读手册页。

apt-get install libxml2-utils
apt-cyg install libxml2
brew install libxml2