【发布时间】:2011-05-06 23:23:10
【问题描述】:
我知道“漂亮打印”或格式化 xml 的两种方法:
还有哪些其他免费(如啤酒)格式化程序? (除了使用 javascript)
【问题讨论】:
-
为了完整性和更好的可发现性,我链接到pretty printing xml with javascript(这可能是关于这个问题的 javascript 解决方案的规范 SO 线程)
标签: xml pretty-print prettify
我知道“漂亮打印”或格式化 xml 的两种方法:
还有哪些其他免费(如啤酒)格式化程序? (除了使用 javascript)
【问题讨论】:
标签: xml pretty-print prettify
我喜欢用于 XML 操作的 java 库 XOM。它有一个不错的Pretty Printer,可以很好地控制输出。
【讨论】:
嗯,您链接到的身份转换可以移植到任何 XSLT 处理器(Saxon、msxml 等)。
此外,您可以查看xmllint,它是LibXML2 工具包的一部分。 --format 选项允许您漂亮地打印输入。 XMLStarlet(在 iirc 底层使用 LibXML2)中存在类似的功能。
【讨论】:
echo "<xml here>" | xmllint --format -
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。
【讨论】:
您可以使用http://prettydiff.com/?m=beautify 不幸的是,它是用 JavaScript 编写的,但它是一个完整的应用程序,因此您永远不必知道这一点。只需知道您可以在浏览器中运行,而无需下载或安装任何东西。
【讨论】:
在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 完成的
【讨论】: