bash XML 格式化缩进标签匹配 - Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11965347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML formatting Indentation Tags Matching - Linux
提问by HardCode
I have an XML file whose format is quite compressed and all tags are stick together like
我有一个 XML 文件,它的格式非常压缩,所有标签都粘在一起,就像
<PersonalData><IndividualDetails><Title>Mr</Title><Gender>Male</Gender><FirstName>Hae</FirstName><Surname>JONES</Surname><Occupation>Banker</Occupation><DateofBirth>4/6/76</DateofBirth><LastKnownAddress></LastKnownAddress><LastKnownPostCode>00145</LastKnownPostCode><OtherNames></OtherNames></IndividualDetails><OccupationDetails><Company>SD Bank</Company><CompanyAddress>Sunset Boulevard NY</CompanyAddress><ContactNo>335698457</ContactNo></OccupationDetails></PersonalData>
Is there any command in shell that can properly format the tags. If not indentation only adding the tags to their own lines can also solve my problem.
shell 中是否有任何命令可以正确格式化标签。如果不是缩进,只将标签添加到自己的行中也可以解决我的问题。
回答by Jin Kwon
xmllint --format <your-xml-file>
example
例子
$ cat test.xml
<a><b>c</b></a>
$ xmllint --format test.xml
<a>
<b>c</b>
</a>
$ xmllint --format test.xml > test.formatted.xml
$ cat test.formatted.xml
<a>
<b>c</b>
</a>
$
回答by CyberDem0n
tidy -xml -i -q
-xml - specify the input is well formed XML
-xml - 指定输入格式良好的 XML
-q - suppress nonessential output
-q - 抑制不必要的输出
-i - indent element content
-i - 缩进元素内容
tidy can work with files and stdin/stdout
tidy 可以处理文件和标准输入/标准输出
echo '<a><b>c</b></a>' | tidy -xml -i -q
will produce
会产生
<a>
<b>c</b>
</a>

