ChatGPT解决这个技术问题 Extra ChatGPT

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

相关:How can I pretty-print JSON in (unix) shell script?

是否有(unix)shell 脚本以人类可读的形式格式化 XML?

基本上,我希望它转换以下内容:

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

...变成这样的东西:

<root>
    <foo a="b">lorem</foo>
    <bar value="ipsum" />
</root>
要在 Debian 系统上使用 xmllint,您需要安装软件包 libxml2-utilslibxml2 不提供此工具,至少在 Debian 5.0 "Lenny" 和 6.0 "Squeeze" 上不提供)。
如今,Web 浏览器(例如 firefox / chrome)倾向于很好地打印 XML 文档。 (作为评论发布,因为这不是 CLI,而是一个非常方便的替代方案)

m
mmoya

xmllint

此实用程序附带 libxml2-utils

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

Perl 的 XML::Twig

此命令附带 XML::Twig 模块,有时是 xml-twig-tools 包:

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

xmlstarlet

此命令附带 xmlstarlet

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

tidy

检查 tidy 包:

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

Python

Python 的 xml.dom.minidom 可以格式化 XML(也适用于旧版 python2):

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

您需要 saxon-lint

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

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'

好,快速的回答。第一个选项似乎在现代 *nix 安装中会更加普遍。一个小问题;但是可以在不通过中间文件的情况下调用它吗?即echo '<xml .. />' | xmllint --some-read-from-stdn-option
包在我漂亮的 ubuntu 中是 libxml2-utils
请注意,“cat data.xml | xmllint --format - | tee data.xml”不起作用。在我的系统上,它有时适用于小文件,但总是截断大文件。如果您真的想做任何事情,请阅读 backreference.org/2011/01/29/in-place-editing-of-files
要在 python 版本中解决 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 805: ordinal not in range(128),您要定义 PYTHONIOENCODING="UTF-8"cat some.xml | PYTHONIOENCODING="UTF-8" python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()' > pretty.xml
请注意,tidy 也可以格式化没有根元素的 xml。这对于通过管道、xml 部分(例如从日志中提取)进行格式化很有用。 echo '<x></x><y></y>' | tidy -xml -iq
O
Orwellophile

xmllint --format yourxmlfile.xml

xmllint 是一个命令行 XML 工具,包含在 libxml2 (http://xmlsoft.org/) 中。

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

注意:如果您没有安装 libxml2,您可以通过执行以下操作来安装它:

中央操作系统

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

sudo apt-get install libxml2-utils

赛格温

apt-cyg install libxml2

苹果系统

要使用 Homebrew 在 MacOS 上安装它,只需执行以下操作:brew install libxml2

吉特

如果您想要代码,也可以在 Git 上找到:git clone git://git.gnome.org/libxml2


sputnick 的答案包含此信息,但 crmpicco 的答案是关于如何漂亮地打印 XML 的一般问题的最有用的答案。
我们可以将格式化的 xml 输出写入其他 xml 文件并使用它.. 例如 xmllint --format yourxmlfile.xml >> new-file.xml
在 Ubuntu 16.04 上,您可以使用以下内容:sudo apt-get install libxml2-utils
这也适用于 Windows; git for Windows download 甚至安装了最新版本的 xmllint。示例:"C:\Program Files\Git\usr\bin\xmllint.exe" --format QCScaper.test@borland.com.cds.xml > QCScaper.test@borland.com.pretty-printed.cds.xml
来自 MacOS,通过 brew 安装了 libxml2。要为我取消最小化 xml 并将其保存到一个新文件中,它可以使用此命令 xmllint --format in.xml > out.xml
m
matanster

您也可以使用 tidy,它可能需要先安装(例如在 Ubuntu 上:sudo apt-get install tidy)。

为此,您将发出如下内容:

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

注意:有许多额外的可读性标志,但自动换行行为有点烦人(http://tidy.sourceforge.net/docs/quickref.html)。


很有帮助,因为我无法让 xmllint 将换行符添加到单行 xml 文件中。谢谢!
tidy 也适合我。与 hxnormalize 不同,这样做实际上关闭了 <body> 标记。
顺便说一句,这里有一些我发现有用的选项:tidy --indent yes --indent-spaces 4 --indent-attributes yes --wrap-attributes yes --input-xml yes --output-xml yes < InFile.xml > OutFile.xml
很棒的提示@VictorYarema。我将它与 pygmentize 结合并将其添加到我的 .bashrc: alias prettyxml='tidy --indent yes --indent-spaces 4 --indent-attributes yes --wrap-attributes yes --input-xml yes --output-xml yes | pygmentize -l xml' 然后可以 curl url | prettyxml
j
jasonleonhard

无需在 macOS / 大多数 Unix 上安装任何东西。

使用tidy

cat filename.xml | tidy -xml -iq

使用 cat 重定向查看文件以整齐指定 xml 的文件类型并在安静输出时缩进将抑制错误输出。 JSON 也适用于 -json


您不需要 cat 步骤:tidy -xml -iq filename.xml。此外,您甚至可以使用 -m 选项执行 tidy -xml -iq filename.xml修改原始文件...
D
David

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

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

g
gavenkoa

xmllint support formatting in-place

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

正如 Daniel Veillard 所写:

我认为 xmllint -o tst.xml --format tst.xml 应该是安全的,因为解析器会在打开输出进行序列化之前将输入完全加载到树中。

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

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

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

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

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

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

S
Sridhar Sarnobat

我花了很长时间才找到可以在我的 Mac 上运行的东西。这对我有用:

brew install xmlformat
cat unformatted.html | xmlformat

n
nby

这种简单的(st)解决方案不提供压痕,但在人眼上却容易得多。它还允许通过简单的工具(如 grep、head、awk 等)更轻松地处理 xml。

使用 sed 替换 '<'本身前面有一个换行符。

正如 Gilles 所提到的,在生产中使用它可能不是一个好主意。

# check you are getting more than one line out
sed 's/</\n</g' sample.xml | wc -l

# check the output looks generally ok
sed 's/</\n</g' sample.xml | head

# capture the pretty xml in a different file
sed 's/</\n</g' sample.xml > prettySample.xml

感谢您使用无需下载的任何内容的回复。
L
Leon S.

编辑:

免责声明:您通常应该更喜欢安装像 xmllint 这样的成熟工具来完成这样的工作。 XML/HTML 可能是一个可怕的残缺不全的混乱。但是,在某些情况下,使用现有工具比手动安装新工具更可取,并且可以肯定的是,XML 的源代码是有效的(足够了)。我已经为其中一种情况编写了此脚本,但它们很少见,因此请谨慎行事。

我想添加一个纯 Bash 解决方案,因为手动完成并不难,有时您不想安装额外的工具来完成这项工作。

#!/bin/bash

declare -i currentIndent=0
declare -i nextIncrement=0
while read -r line ; do
  currentIndent+=$nextIncrement
  nextIncrement=0
  if [[ "$line" == "</"* ]]; then # line contains a closer, just decrease the indent
    currentIndent+=-1
  else
    dirtyStartTag="${line%%>*}"
    dirtyTagName="${dirtyStartTag%% *}"
    tagName="${dirtyTagName//</}"
    # increase indent unless line contains closing tag or closes itself
    if [[ ! "$line" =~ "</$tagName>" && ! "$line" == *"/>"  ]]; then
      nextIncrement+=1
    fi
  fi

  # print with indent
  printf "%*s%s" $(( $currentIndent * 2 )) # print spaces for the indent count
  echo $line
done <<< "$(cat - | sed 's/></>\n</g')" # separate >< with a newline

将其粘贴到脚本文件中,然后通过管道输入 xml。这假设 xml 都在一行上,并且任何地方都没有多余的空格。人们可以很容易地在正则表达式中添加一些额外的 \s* 来解决这个问题。


希望永远不会在某个地方作为系统管理员看到这个 -_-
@GillesQuenot 你是什么意思?是否存在我没有看到的安全风险?
因为用真正的解析器以外的任何东西解析 XML/HTML 是(或将很快)简单的错误。如果它是个人计算机上的一个小型个人脚本,由你决定,但对于生产来说,没办法。它会破裂!
我同意 XML/HTML 可能会被严重破坏,但它确实取决于来源。我为我们自己生成的一些 XML 编写了这个,所以这是一个非常安全的选择。
直到实习生改变 XML 的制作方式:)
N
Nicholas Saunders

我会:

nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ cat ugly.xml 


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

nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
> 
> create database pretty
Database 'pretty' created in 231.32 ms.
> 
> open pretty
Database 'pretty' was opened in 0.05 ms.
> 
> set parser xml
PARSER: xml
> 
> add ugly.xml
Resource(s) added in 161.88 ms.
> 
> xquery .
<root>
  <foo a="b">lorem</foo>
  <bar value="ipsum"/>
</root>
Query executed in 179.04 ms.
> 
> exit
Have fun.
nicholas@mordor:~/flwor$ 

如果只是因为它“在”一个数据库中,而不是“只是”一个文件。在我看来,更容易使用。

相信其他人已经解决了这个问题。如果您愿意,毫无疑问,eXist 甚至可能在格式化 xml 方面“更好”,或者一样好。

当然,您始终可以以各种不同的方式查询数据。我尽量保持简单。您也可以只使用 GUI,但您指定了控制台。


R
Reino

使用

xidel -s input.xml -se . --output-node-format=xml --output-node-indent
<root>
  <foo a="b">lorem</foo>
  <bar value="ipsum"/>
</root>

file:write("output.xml",.,{"indent":true()}) 保存到文件。