相关:如何在(unix) shell脚本漂亮打印JSON ?

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

基本上,我想要它转换如下:

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

…变成这样:

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

当前回答

我想:

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,但您指定了控制台。

其他回答

Xmllint——格式化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安装libxml2-utils

Cygwin

Apt-cyg安装libxml2

操作系统

要在MacOS和Homebrew上安装此程序,请执行以下操作: 酿造安装libxml2

Git

如果你想要代码,也可以在Git上使用: Git克隆Git://git.gnome.org/libxml2

Xmllint支持就地格式化:

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

正如丹尼尔·维拉德所写:

我认为 Xmllint -o tst.xml——格式化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

——nclean,——nonet,——nocdata,——noblanks等可能有用。阅读手册页。

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

我想:

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,但您指定了控制台。

我花了很长时间才找到能在我的mac上运行的软件。以下是对我有效的方法:

brew install xmlformat
cat unformatted.html | xmlformat

这个简单的解决方案不提供缩进,但对人眼来说要容易得多。此外,它还允许使用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