Eclipse XML 格式化程序

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

Eclipse XML formatter

xmleclipseplugins

提问by javamonkey79

What is a free alternative to the built-in Eclipse XML editor?

什么是内置 Eclipse XML 编辑器的免费替代品?

The one packaged with the IDE has two major problems with formatting:

与 IDE 一起打包的那个在格式化方面有两个主要问题:

  1. The formatter tries to parse escaped char's as though they were unescaped. E.g. '&lt;' is treated like '<' which causes the formatter to "stop".
  2. White space between elements is not honored:

    <foo>   text </foo>
    

    will be formatted to:

    <foo>text</foo>
    
  1. 格式化程序尝试解析转义的字符,就好像它们是未转义的一样。例如“<” 被当作​​ '<' 对待,这会导致格式化程序“停止”。
  2. 不尊重元素之间的空白:

    <foo>   text </foo>
    

    将被格式化为:

    <foo>text</foo>
    

We are using Eclipse 3.4.

我们正在使用 Eclipse 3.4。

Updates

更新

Issue #1 is a known bug: Formatting issues with entities in XML files.

问题 #1 是一个已知错误:XML 文件中实体的格式问题

Apparentlythe white space formatting is intended functionality. I have requested this be provided as an option or advise on fixing this in a plugin of my own, but as of yet, no answer.

显然,空白格式是预期的功能。我已要求将此作为选项或建议在我自己的插件中修复此问题,但到目前为止,还没有答案。

采纳答案by javamonkey79

This is way less than ideal, and I hope someone has a better solution, but it works:

这不太理想,我希望有人有更好的解决方案,但它有效:

Build a simple App using dom4j:

使用 dom4j 构建一个简单的应用程序:

public static void main( final String[] args ) throws Exception {
    String text = FileUtils.readFileToString( new File( args[ 0 ] ) );
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText( false );

    XMLWriter writer = new XMLWriter( System.out, format );
    writer.write( DocumentHelper.parseText( text ) );
}

Create a runnable jar, (optional) batch script, and add as an external tool. Of course, you could try XMLTidy or some other command line XML formatter, but I have had better results with dom4j.

创建可运行的 jar、(可选)批处理脚本,并添加为外部工具。当然,您可以尝试 XMLTidy 或其他一些命令行 XML 格式化程序,但是我使用 dom4j 获得了更好的结果。

回答by Metal Fan

You can try this oneif you like

如果你喜欢,你可以试试这个

回答by Stephen Friederichs

I like Notepad++ with the XML Tools plugin. It does XSLT, has an XPATH expression evaluator, and does DTD and schema validation. Toolchain download links:

我喜欢带有 XML 工具插件的 Notepad++。它执行 XSLT,具有 XPATH 表达式评估器,并执行 DTD 和模式验证。工具链下载链接:

回答by Deep Kapadia

I have used XMLBuddy in the past http://www.xmlbuddy.com/

我过去使用过 XMLBuddy http://www.xmlbuddy.com/

before I reverted to OxygenXML but you have to pay for Oxygen.

在我恢复到 OxygenXML 之前,但您必须为 Oxygen 付费。

回答by VonC

I do not see any of those problems when I format (CTRL+SHIFT+F) in an xml editor with WTP

当我CTRL+SHIFT+F带有WTPxml 编辑器中格式化 ( )时,我没有看到任何这些问题

So WTP should solve your issues.

所以 WTP 应该可以解决您的问题。


(source: springframework.org)


(来源:springframework.org



I stand corrected: spaces and escaped characters are indeed an issue with the "XML Editor and Tools" plugin (3.0.2 or 3.0.3) from WTP.

我更正了:空格和转义字符确实是 WTP 的“XML 编辑器和工具”插件(3.0.2 或 3.0.3)的问题。

However, <foo> text </foo>will not collapsed as <foo>text</foo>.

但是,<foo> text </foo>不会像<foo>text</foo>.

But

<foo>    text   text2      text3        </foo>

will be formatted as

将被格式化为

<foo> text text2 text3       </foo>

(the spaces at the end are preserved, curiously)

(奇怪的是,最后的空格被保留了)

And any escaped character sequence does stop the formatting (every lines until the one including the escaped character are formatted)

并且任何转义字符序列都会停止格式化(每一行直到包括转义字符的那一行都被格式化)

I will look deeper in this issue and turn this answer as a community one.

我会更深入地研究这个问题,并将这个答案作为社区答案。

回答by SpaceTrucker

The Eclipse XML Editor will honor CDATAsections and won't do any formatting within those sections. I tested this with kepler. This is slightly more to type, but one can use a template from preferences -> XML -> XML Files -> Editor -> Templatesto make this easier.

Eclipse XML 编辑器将尊重CDATA部分并且不会在这些部分中进行任何格式化。我用开普勒测试了这个。键入的内容稍多一些,但可以使用模板preferences -> XML -> XML Files -> Editor -> Templates来简化此操作。