我知道两种“漂亮打印”或格式化xml的方法:
还有哪些免费(指免费)的格式化程序?(除了使用JavaScript)
嗯,您链接的身份转换对于任何XSLT处理器(Saxon、msxml等)都是可移植的。
此外,您可以查看LibXML2工具包中的xmllint
。 --format
选项允许您漂亮地打印输入。 XMLStarlet中存在类似的功能(在幕后使用LibXML2)。
echo "<xml here>" | xmllint --format -
- Mattxmlstarlet 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
您可以使用http://prettydiff.com/?m=beautify工具。
不幸的是,该工具是用JavaScript编写的,但它是一个完整的应用程序,因此您不必了解任何细节。只需知道您可以在浏览器内运行,无需下载或安装任何东西。
在使用Python中的libxml2时:
with open(pathToSaveResult, 'w') as fd:
xmlParsed.saveTo(fd,format = libxml2.XML_SAVE_FORMAT)
编辑:看起来libxml2中有一些bug...漂亮的打印是使用标签libxml2.XML_SAVE_NO_EMPTY而不是libxml2.XML_SAVE_FORMAT完成的。