本文翻譯自:How to pretty print XML from the command line?
Related: How can I pretty-print JSON in (unix) shell script? 相關: 如何在(unix)shell腳本中漂亮地打印JSON?
Is there a (unix) shell script to format XML in human-readable form? 是否有一個(unix)shell腳本來以人類可讀的格式格式化XML?
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>
#1樓
參考:https://stackoom.com/question/15Vy9/如何從命令行漂亮地打印XML
#2樓
libxml2-utils
This utility comes with libxml2-utils
: 該實用程序隨附libxml2-utils
:
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
xmllint --format -
Perl's XML::Twig
Perl的XML::Twig
This command comes with XML::Twig perl module, sometimes xml-twig-tools
package: 此命令隨附XML :: Twig perl模塊,有時還包含xml-twig-tools
軟件包:
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
xml_pp
xmlstarlet
This command comes with xmlstarlet
: 該命令隨xmlstarlet
一起xmlstarlet
:
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
xmlstarlet format --indent-tab
tidy
Check the tidy
package: 檢查tidy
包裝:
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
tidy -xml -i -
Python 蟒蛇
Python's xml.dom.minidom
can format XML (both python2 and python3): Python的xml.dom.minidom
可以格式化XML(python2和python3):
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
You need saxon-lint
: 您需要saxon-lint
:
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
saxon-lint --indent --xpath '/' -
saxon-HE
You need 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'
#3樓
You didn't mention a file, so I assume you want to provide the XML string as standard input on the command line. 您沒有提到文件,所以我假設您想在命令行上提供XML字符串作爲標準輸入。 In that case, do the following: 在這種情況下,請執行以下操作:
$ echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | xmllint --format -
#4樓
xmllint --format yourxmlfile.xml
xmllint is a command line XML tool and is included in libxml2
( http://xmlsoft.org/ ). xmllint是命令行XML工具,包含在libxml2
( http://xmlsoft.org/ )中。
================================================ ===============================================
Note: If you don't have libxml2
installed you can install it by doing the following: 注意:如果尚未安裝libxml2
,則可以通過以下操作進行安裝:
CentOS 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 的Ubuntu
sudo apt-get install libxml2-utils
Cygwin 西格溫
apt-cyg install libxml2
MacOS 蘋果系統
To install this on MacOS with Homebrew just do: brew install libxml2
要使用Homebrew在MacOS上安裝,只需: brew install libxml2
Git 吉特
Also available on Git if you want the code: git clone git://git.gnome.org/libxml2
如果需要代碼,也可以在Git上使用: git clone git://git.gnome.org/libxml2
#5樓
You can also use tidy , which may need to be installed first (eg on Ubuntu: sudo apt-get install tidy
). 您也可以使用tidy ,它可能需要先安裝(例如在Ubuntu上:sudo apt-get install tidy
)。
For this, you would issue something like following: 爲此,您將發出如下內容:
tidy -xml -i your-file.xml > output.xml
Note: has many additional readability flags, but word-wrap behavior is a bit annoying to untangle ( http://tidy.sourceforge.net/docs/quickref.html ). 注意:具有許多其他可讀性標誌,但是自動換行的行爲有點令人討厭,無法解開( http://tidy.sourceforge.net/docs/quickref.html )。
#6樓
xmllint support formatting in-place : xmllint支持就地格式化 :
for f in *.xml; do xmllint -o $f --format $f; done
As Daniel Veillard has written: 正如Daniel Veillard所寫:
I think
xmllint -o tst.xml --format tst.xml
should be safe as the parser will fully load the input into a tree before opening the output to serialize it. 我認爲xmllint -o tst.xml --format tst.xml
應該是安全的,因爲解析器會在打開輸出以對其進行序列化之前將輸入完全加載到樹中。
Indent level is controlled by XMLLINT_INDENT
environment variable which is by default 2 spaces. 縮進級別由XMLLINT_INDENT
環境變量控制,默認情況下爲2個空格。 Example how to change indent to 4 spaces: 示例如何將縮進更改爲4個空格:
XMLLINT_INDENT=' ' xmllint -o out.xml --format in.xml
You may have lack with --recover
option when you XML documents are broken. 當XML文檔損壞時,可能缺少--recover
選項。 Or try weak HTML parser with strict XML output: 或者嘗試使用具有嚴格XML輸出的弱HTML解析器:
xmllint --html --xmlout <in.xml >out.xml
--nsclean
, --nonet
, --nocdata
, --noblanks
etc may be useful. --nsclean
,-- --nonet
,-- --nocdata
,-- --noblanks
等可能有用。 Read man page. 閱讀手冊頁。
apt-get install libxml2-utils
apt-cyg install libxml2
brew install libxml2