如何在Linux中格式化XML文档

use*_*992 7 xml linux xml-formatting xmllint

我有大量的XML标签.

<SERVICE>
<NAME>
sh_SEET15002GetReKeyDetails
</NAME>
<ID>642</ID>
</SERVICE>
Run Code Online (Sandbox Code Playgroud)

我想以下面的方式格式化.我尝试过使用xmllint,但它对我不起作用.请提供帮助.

<SERVICE>
<NAME>sh_SEET15002GetReKeyDetails</NAME>
<ID>642</ID>
</SERVICE>
Run Code Online (Sandbox Code Playgroud)

小智 17

xmllint -format -recover nonformatted.xml > formated.xml
Run Code Online (Sandbox Code Playgroud)

对于制表符缩进:

export XMLLINT_INDENT=`echo -e '\t'`
Run Code Online (Sandbox Code Playgroud)

对于四个空格缩进:

export XMLLINT_INDENT=\ \ \ \ 
Run Code Online (Sandbox Code Playgroud)


Reu*_*ben 6

没有编程你可以使用Eclipse XML Source Editor.看看这个答案

顺便问一下你试过xmllint -format -recover nonformatted.xml > formated.xml吗?

编辑:

您可以尝试使用此XMLStarlet命令行XML工具包.

5. Formatting XML documents
====================================================

xml 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
   -h or --help                - print help
Run Code Online (Sandbox Code Playgroud)