如何从命令行打印XML? [英] How to pretty print XML from the command line?

查看:168
本文介绍了如何从命令行打印XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:如何从命令中漂亮打印JSON line?



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



基本上,我想要转换以下内容:

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

...如下:

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


解决方案

尝试这样做:

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

此实用程序附带libxml2-utils



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

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





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

此命令带有 xmlstarlet



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

检查 tidy package



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



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

检查 saxon-lint



  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'

检查 saxon-HE


Related: How to pretty-print JSON from the command line?

Is there a (unix) shell script to format XML in human-readable form?

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>

解决方案

Try doing this :

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

This utility comes with 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.

or

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

this command comes with 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

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

这篇关于如何从命令行打印XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆