首页 文章

如何从命令行中打印XML?

提问于
浏览
424

相关: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>

5 回答

  • 32

    libxml2-utils

    该实用程序随附libxml2-utils

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

    Perl's XML::Twig

    此命令附带XML::Twig perl模块,有时 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:

    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'
    
  • 6

    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
    apt-cyg install libxml2
    brew install libxml2
    
  • 726

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

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

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

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

  • 112

    xmllint --format yourxmlfile.xml

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

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

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

    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

    sudo apt-get install libxml2-utils

    MacOS

    要使用Homebrew在MacOS上安装它,只需: brew install libxml2

  • 11

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

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

相关问题