有没有办法使用 TextWrangler 来清理/整理凌乱的 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3018209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:10:09  来源:igfitidea点击:

Is there a way to use TextWrangler to clean-up / tidy messy XML?

xml

提问by MW.

I don't need it to validate ( though that would be nice I don't think text wrangler does this) but to clean up messy .xml.

我不需要它来验证(虽然这很好,但我认为 text wrangler 不会这样做),而是清理凌乱的 .xml。

example this ...

例如这个...

<some><foo>
bar</foo></some>

to ...

到 ...

<some>
   <foo>bar</foo>
</some>

thanks -MW

谢谢-MW

回答by Reddog

If you're after a tool, most editors have some sort of "Tidy" feature.

如果您使用工具,大多数编辑器都有某种“整洁”功能。

  • In NotePad++: TestFX -> TestFX HTML Tidy -> Tidy: Reindent XML
  • In Visual Studio: Ctrl-K, Ctrl-D (or Edit -> Advanced -> Format Document)
  • 在 NotePad++ 中:TestFX -> TestFX HTML Tidy -> Tidy: Reindent XML
  • 在 Visual Studio 中:Ctrl-K、Ctrl-D(或编辑 -> 高级 -> 格式化文档)

A quick google for TextWrangler turns up this - http://magp.ie/2010/02/15/format-xml-with-textwrangler/

TextWrangler 的快速谷歌搜索结果 - http://magp.ie/2010/02/15/format-xml-with-textwrangler/

回答by nwinkler

As an update to the instructions from http://magp.ie/2010/02/15/format-xml-with-textwrangler/and the comment by @Cykoduck for getting this to work in TextWrangler version 4.

作为对http://magp.ie/2010/02/15/format-xml-with-textwrangler/ 中的说明以及@Cykoduck 的评论的更新,以使其在 TextWrangler 版本 4 中工作。

The script needs to be changed to take the input from STDIN instead of a temporary file, so the first invocation of xmllintneeds to be changed:

脚本需要更改为从 STDIN 获取输入而不是临时文件,因此xmllint需要更改第一次调用:

#!/bin/sh
xmllint --c14n - | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format -

This way it works for TextWrangler 4 as well. The menu item to call the script has been moved to the Textmenu in this version.

这样它也适用于 TextWrangler 4。Text在此版本中,调用脚本的菜单项已移至菜单中。

Reference link: https://groups.google.com/forum/?fromgroups#!topic/textwrangler/FePYfNKi4rs

参考链接:https: //groups.google.com/forum/?fromgroups#!topic/textwrangler/FePYfNKi4rs

回答by Dennis Ng

The script does not work still under 4.5.

该脚本在 4.5 下仍然不起作用。

I checked https://groups.google.com/forum/?fromgroups=#!topic/textwrangler/FePYfNKi4rsand it had a script that worked.

我检查了https://groups.google.com/forum/?fromgroups=#!topic/textwrangler/FePYfNKi4rs,它有一个有效的脚本。

#!/bin/sh 

XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -

回答by Cykoduck

I also used the method on http://magp.ie/2010/02/15/format-xml-with-textwrangler/

我还使用了http://magp.ie/2010/02/15/format-xml-with-textwrangler/上的方法

but I modified it because the errors I was getting regarding the xml I was attempting to format. My script is just:

但我修改了它,因为我遇到了关于我试图格式化的 xml 的错误。我的脚本只是:

#!/bin/sh
xmllint "$*" | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format -

I took out the formatting for the W3C canonical format to fix my errors like yours.

我取出了 W3C 规范格式的格式来修复我和你一样的错误。

回答by ecomware

If you're on a Mac, it may be easiest to create the script this way:

如果您使用的是 Mac,以这种方式创建脚本可能最简单:

#!/bin/bash
pbpaste | xmllint --c14n - | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format - | pbcopy

Right-click the file, click Get Info, and change "Open With" to terminal. This will let you process xml in the primary clipboard from anywhere by clicking the icon. ie. copy-click-paste. You can also wrap with another so that it's accessible both ways.

右键单击该文件,单击获取信息,并将“打开方式”更改为终端。这将使您可以通过单击图标从任何地方处理主剪贴板中的 xml。IE。复制-点击-粘贴。您也可以使用另一个包装,以便双向访问。

pbpaste | ./tidy.sh | pbcopy # where tidy.sh is available to TextWrangler