【问题标题】:format xml, pretty printxml格式,漂亮的打印
【发布时间】:2011-05-06 23:23:10
【问题描述】:

我知道“漂亮打印”或格式化 xml 的两种方法:

  1. shell tools
  2. Hack 38 Pretty-Print XML Using a Generic Identity Stylesheet and Xalan

还有哪些其他免费(如啤酒)格式化程序? (除了使用 javascript)

【问题讨论】:

标签: xml pretty-print prettify


【解决方案1】:

我喜欢用于 XML 操作的 java 库 XOM。它有一个不错的Pretty Printer,可以很好地控制输出。

【讨论】:

    【解决方案2】:

    嗯,您链接到的身份转换可以移植到任何 XSLT 处理器(Saxon、msxml 等)。

    此外,您可以查看xmllint,它是LibXML2 工具包的一部分。 --format 选项允许您漂亮地打印输入。 XMLStarlet(在 iirc 底层使用 LibXML2)中存在类似的功能。

    【讨论】:

    • 也适用于字符串:echo "<xml here>" | xmllint --format -
    【解决方案3】:

    xmlstarlet fo 是我用于漂亮打印的工具。 Xmlstarlet 有多种选择:

    $ xmlstarlet fo --help
    XMLStarlet Toolkit: Format XML document
    Usage: xml fo [<options>] <xml-file>
    where <options> are
      -n or --noindent            - do not indent
      -t or --indent-tab          - indent output with tabulation
      -s or --indent-spaces <num> - indent output with <num> spaces
      -o or --omit-decl           - omit xml declaration <?xml version="1.0"?>
      -R or --recover             - try to recover what is parsable
      -D or --dropdtd             - remove the DOCTYPE of the input docs
      -C or --nocdata             - replace cdata section with text nodes
      -N or --nsclean             - remove redundant namespace declarations
      -e or --encode <encoding>   - output in the given encoding (utf-8, unicode...)
      -H or --html                - input is HTML
    

    优秀的 XML 工程师应该能够使用 xmlstarlet。

    【讨论】:

      【解决方案4】:

      您可以使用http://prettydiff.com/?m=beautify 不幸的是,它是用 JavaScript 编写的,但它是一个完整的应用程序,因此您永远不必知道这一点。只需知道您可以在浏览器中运行,而无需下载或安装任何东西。

      【讨论】:

        【解决方案5】:

        在python中使用libxml2时:

        with open(pathToSaveResult, 'w') as fd:
           xmlParsed.saveTo(fd,format = libxml2.XML_SAVE_FORMAT)
        

        编辑:看起来 libxml2 中存在一些错误 ...漂亮的打印是使用标签 libxml2.XML_SAVE_NO_EMPTY 而不是 libxml2.XML_SAVE_FORMAT 完成的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-24
          • 1970-01-01
          • 2011-01-07
          • 2023-03-07
          • 2020-04-28
          • 1970-01-01
          相关资源
          最近更新 更多